Skip to content

Commit

Permalink
fix alert group page bug (#5105)
Browse files Browse the repository at this point in the history
# What this PR does

fixes a bug where it's not possible to view an alert group older than 30
days

<img width="623" alt="Screenshot 2024-10-01 at 14 36 00"
src="https://github.com/user-attachments/assets/c06c923e-4c1c-4873-bf4b-9296cfbbb312">

## Which issue(s) this PR closes

the bug was introduced in #5067

<!--
*Note*: If you want the issue to be auto-closed once the PR is merged,
change "Related to" to "Closes" in the line above.
If you have more than one GitHub issue that this PR closes, be sure to
preface
each issue link with a [closing
keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue).
This ensures that the issue(s) are auto-closed once the PR has been
merged.
-->

## 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.
  • Loading branch information
vadimkerr authored Oct 1, 2024
1 parent 8754f60 commit e9d94eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
38 changes: 38 additions & 0 deletions engine/apps/api/tests/test_alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions engine/apps/api/views/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e9d94eb

Please sign in to comment.