From 6dada51133f04f332f186a6be37057b06597709b Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Thu, 14 Dec 2023 15:25:34 -0300 Subject: [PATCH] Remove unneeded filter making query slower (#3570) There is no index for the `received_at` column, and the filter isn't really needed (aggregation will work in any case, considering only the entries for which we have data). --- engine/apps/alerts/tasks/check_escalation_finished.py | 2 +- .../alerts/tests/test_check_escalation_finished_task.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/engine/apps/alerts/tasks/check_escalation_finished.py b/engine/apps/alerts/tasks/check_escalation_finished.py index b6865373..211f8a57 100644 --- a/engine/apps/alerts/tasks/check_escalation_finished.py +++ b/engine/apps/alerts/tasks/check_escalation_finished.py @@ -106,7 +106,7 @@ def check_escalation_finished_task() -> None: ) total_alert_groups_count = alert_groups.count() - creation_deltas = alert_groups.filter(received_at__isnull=False).aggregate( + creation_deltas = alert_groups.aggregate( avg_delta=Avg(F("started_at") - F("received_at")), max_delta=Max(F("started_at") - F("received_at")), ) diff --git a/engine/apps/alerts/tests/test_check_escalation_finished_task.py b/engine/apps/alerts/tests/test_check_escalation_finished_task.py index 1735c5cb..389e0fd9 100644 --- a/engine/apps/alerts/tests/test_check_escalation_finished_task.py +++ b/engine/apps/alerts/tests/test_check_escalation_finished_task.py @@ -25,7 +25,8 @@ def make_alert_group_that_started_at_specific_date(make_alert_group): ): # we can't simply pass started_at to the fixture because started_at is being "auto-set" on the Model alert_group = make_alert_group(alert_receive_channel, **kwargs) - alert_group.received_at = started_at - timezone.timedelta(seconds=received_delta) + if received_delta is not None: + alert_group.received_at = started_at - timezone.timedelta(seconds=received_delta) alert_group.started_at = started_at alert_group.save() @@ -358,6 +359,7 @@ def test_check_escalation_finished_task_calls_audit_alert_group_escalation_for_e alert_group1 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=1) alert_group2 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=5) alert_group3 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=12) + alert_group3 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=None) def _mocked_audit_alert_group_escalation(alert_group): if not alert_group.id == alert_group3.id: @@ -376,7 +378,7 @@ def test_check_escalation_finished_task_calls_audit_alert_group_escalation_for_e assert "Alert group ingestion/creation avg delta seconds: 6" in caplog.text assert "Alert group ingestion/creation max delta seconds: 12" in caplog.text - assert "Alert group notifications success ratio: 33.33" in caplog.text + assert "Alert group notifications success ratio: 25.00" in caplog.text mocked_audit_alert_group_escalation.assert_any_call(alert_group1) mocked_audit_alert_group_escalation.assert_any_call(alert_group2)