From a25fd429da61e3ae9e34dbeda1d47c7530df7833 Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Tue, 28 Feb 2023 14:12:56 +0000 Subject: [PATCH] Show 100 latest alerts on alert group page (#1417) # What this PR does Make internal API return 100 latest alerts for alert group. ## Which issue(s) this PR fixes https://github.com/grafana/oncall/issues/857 ## Checklist - [x] Tests updated - [x] `CHANGELOG.md` updated --- CHANGELOG.md | 1 + engine/apps/api/serializers/alert_group.py | 7 +--- engine/apps/api/tests/test_alert_group.py | 37 +++++++++++++++++++ .../src/pages/incident/Incident.tsx | 2 +- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dea6ad50cb..accd359e70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Moved reCAPTCHA to backend environment variable for more flexible configuration between different environments. - Add pagination to schedule listing +- Show 100 latest alerts on alert group page ([1417](https://github.com/grafana/oncall/pull/1417)) ## v1.1.29 (2023-02-23) diff --git a/engine/apps/api/serializers/alert_group.py b/engine/apps/api/serializers/alert_group.py index d171cd7b0d..2a933da561 100644 --- a/engine/apps/api/serializers/alert_group.py +++ b/engine/apps/api/serializers/alert_group.py @@ -204,12 +204,7 @@ def get_limited_alerts(self, obj): Overriding default alerts because there are alert_groups with thousands of them. It's just too slow, we need to cut here. """ - alerts = obj.alerts.all()[:100] - - if len(alerts) > 90: - for alert in alerts: - alert.title = str(alert.title) + " Only last 100 alerts are shown. Use OnCall API to fetch all of them." - + alerts = obj.alerts.order_by("-pk")[:100] return AlertSerializer(alerts, many=True).data def get_paged_users(self, obj): diff --git a/engine/apps/api/tests/test_alert_group.py b/engine/apps/api/tests/test_alert_group.py index 447cc647e2..a0b5e22e7b 100644 --- a/engine/apps/api/tests/test_alert_group.py +++ b/engine/apps/api/tests/test_alert_group.py @@ -1548,6 +1548,43 @@ def test_alert_group_preview_body_invalid_template_syntax( assert response.data["preview"] == "Template Error: No test named 'None' found." +@pytest.mark.django_db +def test_grouped_alerts( + make_organization_and_user_with_plugin_token, + make_alert_receive_channel, + 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) + alert_group = make_alert_group(alert_receive_channel) + + # create 101 alerts and check that only 100 are returned + for i in range(101): + make_alert( + alert_group=alert_group, + created_at=timezone.datetime.min + timezone.timedelta(minutes=i), + raw_request_data=alert_receive_channel.config.example_payload, + ) + + client = APIClient() + url = reverse("api-internal:alertgroup-detail", kwargs={"pk": alert_group.public_primary_key}) + + response = client.get(url, **make_user_auth_headers(user, token)) + + assert response.status_code == status.HTTP_200_OK + assert len(response.json()["alerts"]) == 100 + + first_alert_created_at = response.json()["alerts"][0]["created_at"] + last_alert_created_at = response.json()["alerts"][-1]["created_at"] + + first_alert_created_at = timezone.datetime.strptime(first_alert_created_at, "%Y-%m-%dT%H:%M:%S.%fZ") + last_alert_created_at = timezone.datetime.strptime(last_alert_created_at, "%Y-%m-%dT%H:%M:%S.%fZ") + + assert first_alert_created_at > last_alert_created_at + + @pytest.mark.django_db def test_alert_group_paged_users( make_user_for_organization, diff --git a/grafana-plugin/src/pages/incident/Incident.tsx b/grafana-plugin/src/pages/incident/Incident.tsx index 4cb11a177a..9c20cdef09 100644 --- a/grafana-plugin/src/pages/incident/Incident.tsx +++ b/grafana-plugin/src/pages/incident/Incident.tsx @@ -638,7 +638,7 @@ function GroupedIncidentsList({ return null; } - const latestAlert = alerts[alerts.length - 1]; + const latestAlert = alerts[0]; const latestAlertMoment = moment(latestAlert.created_at); return (