From af939dc83a692e1fdfbbbe12566b903165e69d2e Mon Sep 17 00:00:00 2001 From: Yulya Artyukhina Date: Wed, 21 Jun 2023 10:16:35 +0200 Subject: [PATCH] Optimize getting response time for alert groups (#2296) Optimize getting response time for alert groups in calculation metrics task ## Checklist - [ ] Unit, integration, and e2e (if applicable) tests updated - [ ] Documentation added (or `pr:no public docs` PR label added if not required) - [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required) --- engine/apps/metrics_exporter/tasks.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/engine/apps/metrics_exporter/tasks.py b/engine/apps/metrics_exporter/tasks.py index 72ef066f..9fe47e79 100644 --- a/engine/apps/metrics_exporter/tasks.py +++ b/engine/apps/metrics_exporter/tasks.py @@ -113,17 +113,13 @@ def calculate_and_cache_metrics(organization_id, force=False): }, )[state] = integration.alert_groups.filter(alert_group_filter).count() - # calculate response time - all_response_time = [] - alert_groups = integration.alert_groups.filter(started_at__gte=response_time_period) - for alert_group in alert_groups: - if alert_group.response_time: - all_response_time.append(int(alert_group.response_time.total_seconds())) - elif alert_group.state != AlertGroupState.FIRING: - # get calculated value from current alert group information - response_time = alert_group._get_response_time() - if response_time: - all_response_time.append(int(response_time.total_seconds())) + # get response time + all_response_time = integration.alert_groups.filter( + started_at__gte=response_time_period, + response_time__isnull=False, + ).values_list("response_time", flat=True) + + all_response_time_seconds = [int(response_time.total_seconds()) for response_time in all_response_time] metric_alert_group_response_time[integration.id] = { "integration_name": integration.emojized_verbal_name, @@ -132,7 +128,7 @@ def calculate_and_cache_metrics(organization_id, force=False): "org_id": integration.organization.org_id, "slug": instance_slug, "id": instance_id, - "response_time": all_response_time, + "response_time": all_response_time_seconds, } metric_alert_groups_total_key = get_metric_alert_groups_total_key(organization_id)