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
This commit is contained in:
Vadim Stepanov 2023-02-28 14:12:56 +00:00 committed by GitHub
parent bee9943706
commit a25fd429da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 7 deletions

View file

@ -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)

View file

@ -204,12 +204,7 @@ class AlertGroupSerializer(AlertGroupListSerializer):
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):

View file

@ -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,

View file

@ -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 (