Skip to content

Commit

Permalink
Show 100 latest alerts on alert group page (#1417)
Browse files Browse the repository at this point in the history
# What this PR does
Make internal API return 100 latest alerts for alert group.

## Which issue(s) this PR fixes
#857

## Checklist

- [x] Tests updated
- [x] `CHANGELOG.md` updated
  • Loading branch information
vstpme authored Feb 28, 2023
1 parent bee9943 commit a25fd42
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 1 addition & 6 deletions engine/apps/api/serializers/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
37 changes: 37 additions & 0 deletions engine/apps/api/tests/test_alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion grafana-plugin/src/pages/incident/Incident.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit a25fd42

Please sign in to comment.