Skip to content

Commit

Permalink
Handle multiple unpage event logs when getting paged users (#4698)
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasb authored Jul 18, 2024
1 parent dd84aa1 commit 7777a60
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions engine/apps/alerts/models/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def get_paged_users(self) -> typing.List[PagedUser]:

log_records = self.log_records.filter(
type__in=(AlertGroupLogRecord.TYPE_DIRECT_PAGING, AlertGroupLogRecord.TYPE_UNPAGE_USER)
)
).order_by("created_at")

for log_record in log_records:
# filter paging events, track still active escalations
Expand Down Expand Up @@ -592,7 +592,9 @@ def get_paged_users(self) -> typing.List[PagedUser]:
}
else:
# user was unpaged at some point, remove them
del users[user_id]
# there could be multiple unpage log records if API was hit several times
if user_id in users:
del users[user_id]

return list(users.values())

Expand Down
18 changes: 18 additions & 0 deletions engine/apps/alerts/tests/test_alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,24 @@ def _make_log_record(alert_group, user, log_type, important=False):
assert len(paged_users) == 1
assert alert_group.get_paged_users()[0]["pk"] == user.public_primary_key

# user was paged and then paged again, then unpaged - they should not show up
alert_group = make_alert_group(alert_receive_channel)
_make_log_record(alert_group, user, AlertGroupLogRecord.TYPE_DIRECT_PAGING)
_make_log_record(alert_group, user, AlertGroupLogRecord.TYPE_DIRECT_PAGING)
_make_log_record(alert_group, user, AlertGroupLogRecord.TYPE_UNPAGE_USER)

paged_users = alert_group.get_paged_users()
assert len(paged_users) == 0

# adding extra unpage events should not break things
_make_log_record(alert_group, user, AlertGroupLogRecord.TYPE_UNPAGE_USER)
_make_log_record(alert_group, user, AlertGroupLogRecord.TYPE_UNPAGE_USER)
_make_log_record(alert_group, user, AlertGroupLogRecord.TYPE_DIRECT_PAGING)

paged_users = alert_group.get_paged_users()
assert len(paged_users) == 1
assert alert_group.get_paged_users()[0]["pk"] == user.public_primary_key


@patch("apps.alerts.models.AlertGroup.start_unsilence_task", return_value=None)
@pytest.mark.django_db
Expand Down

0 comments on commit 7777a60

Please sign in to comment.