From e9d94ebc1ebc36d82a33f5ef7a5890474de6640b Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Tue, 1 Oct 2024 15:01:38 +0100 Subject: [PATCH] fix alert group page bug (#5105) # What this PR does fixes a bug where it's not possible to view an alert group older than 30 days Screenshot 2024-10-01 at 14 36 00 ## Which issue(s) this PR closes the bug was introduced in https://github.com/grafana/oncall/pull/5067 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. --- engine/apps/api/tests/test_alert_group.py | 38 +++++++++++++++++++++++ engine/apps/api/views/alert_group.py | 3 +- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/engine/apps/api/tests/test_alert_group.py b/engine/apps/api/tests/test_alert_group.py index 42808e85fe..a015fccc3e 100644 --- a/engine/apps/api/tests/test_alert_group.py +++ b/engine/apps/api/tests/test_alert_group.py @@ -2344,3 +2344,41 @@ def test_alert_group_external_urls( "url": "https://some-url", } assert response.json()["external_urls"] == [expected] + + +@pytest.mark.django_db +def test_filter_default_started_at( + make_organization_and_user_with_plugin_token, + make_alert_receive_channel, + make_channel_filter, + make_alert_group, + make_alert, + make_user_auth_headers, +): + organization, user, token = make_organization_and_user_with_plugin_token() + alert_receive_channel = make_alert_receive_channel(organization) + channel_filter = make_channel_filter(alert_receive_channel, is_default=True) + + old_alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter) + old_alert_group.started_at = timezone.now() - timezone.timedelta(days=31) + old_alert_group.save(update_fields=["started_at"]) + make_alert(alert_group=old_alert_group, raw_request_data=alert_raw_request_data) + + new_alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter) + make_alert(alert_group=new_alert_group, raw_request_data=alert_raw_request_data) + + client = APIClient() + + # by default, no alert groups older than 30 days should be listed + response = client.get(reverse("api-internal:alertgroup-list"), **make_user_auth_headers(user, token)) + assert response.status_code == status.HTTP_200_OK + assert len(response.json()["results"]) == 1 + assert response.json()["results"][0]["pk"] == new_alert_group.public_primary_key + + # but it should still be possible to retrieve an old alert group by its ID + response = client.get( + reverse("api-internal:alertgroup-detail", kwargs={"pk": old_alert_group.public_primary_key}), + **make_user_auth_headers(user, token), + ) + assert response.status_code == status.HTTP_200_OK + assert response.json()["pk"] == old_alert_group.public_primary_key diff --git a/engine/apps/api/views/alert_group.py b/engine/apps/api/views/alert_group.py index 8e9cf00516..35ef836a68 100644 --- a/engine/apps/api/views/alert_group.py +++ b/engine/apps/api/views/alert_group.py @@ -332,8 +332,7 @@ def get_queryset(self, ignore_filtering_by_available_teams=False): alert_receive_channels_ids = list(alert_receive_channels_qs.values_list("id", flat=True)) queryset = AlertGroup.objects.filter(channel__in=alert_receive_channels_ids) - # This is a quick fix to speed up requests from mobile app by adding default `started_at` filter value - if not self.request.query_params.get("started_at"): + if self.action in ("list", "stats") and not self.request.query_params.get("started_at"): queryset = queryset.filter(started_at__gte=timezone.now() - timezone.timedelta(days=30)) if self.action in ("list", "stats") and settings.ALERT_GROUPS_DISABLE_PREFER_ORDERING_INDEX: