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: