-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add responders improvements #3128
Merged
Merged
Changes from 14 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
4d6794b
WIP direct paging changes
joeyorlando 4bfd384
Merge branch 'dev' into jorlando/direct-paging
joeyorlando 0d8b632
update tests
joeyorlando 4a74bff
WIP
joeyorlando dc058c2
WIP
joeyorlando 3d80dbf
WIP
joeyorlando 25c7912
WIP
joeyorlando c8581b0
WIP
joeyorlando 748791c
WIP
joeyorlando 50bd4e7
update some unit tests
joeyorlando cd64267
add some more tests
joeyorlando 8146c47
add more tests
joeyorlando 0f86f12
allow filtering users by is_currently_oncall
joeyorlando 4bb90c3
allow filtering by is_currently_oncall=false
joeyorlando 2f72920
WIP
joeyorlando 2ba681a
WIP
joeyorlando 0bf9323
WIP
joeyorlando a46aede
WIP
joeyorlando 5d98949
WIP
joeyorlando 3f30e76
style add responders popup
joeyorlando 8899845
update backend unit tests
joeyorlando 4bcc713
update some more backend tests
joeyorlando 8a1c17b
add title attribute to direct paging endpoint
joeyorlando 2019eee
add more backend tests
joeyorlando fd5f5d8
more tests + UI styling changes
joeyorlando 2b932c8
Merge branch 'dev' into jorlando/direct-paging
joeyorlando f51c735
add more frontend unit tests
joeyorlando 28f33f4
update changelog
joeyorlando d2c2ad5
address PR comments
joeyorlando fc89fe8
revert change to GForm
joeyorlando 0ac4521
add teams to objects in paged_users
joeyorlando a05e691
update public documentation
joeyorlando 3808099
Merge branch 'dev' into jorlando/direct-paging
joeyorlando 88fbfc6
Merge branch 'jorlando/direct-paging' of github.com:grafana/oncall in…
joeyorlando a21492a
disable submit button if form is not valid or user
joeyorlando 8d80c8e
simplify optional prop null check
joeyorlando d0b0266
final frontend changes + update e2e tests
joeyorlando 7140308
remove test.only
joeyorlando 9acab65
update swagger UI in a subsequent PR
joeyorlando c5ee288
Merge branch 'dev' into jorlando/direct-paging
joeyorlando 51fdb26
address failing build
joeyorlando d0c5b0e
Merge branch 'jorlando/direct-paging' of github.com:grafana/oncall in…
joeyorlando 02d73c3
add unit tests for DirectPagingStore
joeyorlando cc5388a
address PR comments
joeyorlando a3b69fc
address frontend PR comments
joeyorlando 3ac88ad
address more PR comments
joeyorlando 9855f6f
remove unused method
joeyorlando 500a696
final PR comments
joeyorlando File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,20 @@ | ||
import enum | ||
import typing | ||
from uuid import uuid4 | ||
|
||
from django.db import transaction | ||
from django.db.models import Q | ||
|
||
from apps.alerts.models import ( | ||
Alert, | ||
AlertGroup, | ||
AlertGroupLogRecord, | ||
AlertReceiveChannel, | ||
ChannelFilter, | ||
EscalationChain, | ||
UserHasNotification, | ||
) | ||
from apps.alerts.tasks.notify_user import notify_user_task | ||
from apps.schedules.ical_utils import list_users_to_notify_from_ical | ||
from apps.schedules.models import OnCallSchedule | ||
from apps.user_management.models import Organization, Team, User | ||
|
||
|
||
class PagingError(enum.StrEnum): | ||
USER_HAS_NO_NOTIFICATION_POLICY = "USER_HAS_NO_NOTIFICATION_POLICY" | ||
USER_IS_NOT_ON_CALL = "USER_IS_NOT_ON_CALL" | ||
|
||
|
||
# notifications: (User|Schedule, important) | ||
UserNotifications = list[tuple[User, bool]] | ||
ScheduleNotifications = list[tuple[OnCallSchedule, bool]] | ||
|
||
|
||
class NoNotificationPolicyWarning(typing.TypedDict): | ||
error: typing.Literal[PagingError.USER_HAS_NO_NOTIFICATION_POLICY] | ||
data: typing.Dict | ||
|
||
|
||
ScheduleWarnings = typing.Dict[str, typing.List[str]] | ||
|
||
|
||
class _NotOnCallWarningData(typing.TypedDict): | ||
schedules: ScheduleWarnings | ||
|
||
|
||
class NotOnCallWarning(typing.TypedDict): | ||
error: typing.Literal[PagingError.USER_IS_NOT_ON_CALL] | ||
data: _NotOnCallWarningData | ||
|
||
|
||
AvailabilityWarning = NoNotificationPolicyWarning | NotOnCallWarning | ||
|
||
|
||
class DirectPagingAlertGroupResolvedError(Exception): | ||
|
@@ -56,6 +23,12 @@ class DirectPagingAlertGroupResolvedError(Exception): | |
DETAIL = "Cannot add responders for a resolved alert group" # Returned in BadRequest responses and Slack warnings | ||
|
||
|
||
class DirectPagingUserTeamValidationError(Exception): | ||
"""Raised when trying to use direct paging and no team or user is specified.""" | ||
|
||
DETAIL = "No team or user(s) specified" # Returned in BadRequest responses and Slack warnings | ||
|
||
|
||
class _OnCall(typing.TypedDict): | ||
title: str | ||
message: str | ||
|
@@ -68,14 +41,7 @@ class DirectPagingAlertPayload(typing.TypedDict): | |
oncall: _OnCall | ||
|
||
|
||
def _trigger_alert( | ||
organization: Organization, | ||
team: Team | None, | ||
title: str, | ||
message: str, | ||
from_user: User, | ||
escalation_chain: EscalationChain = None, | ||
) -> AlertGroup: | ||
def _trigger_alert(organization: Organization, team: Team | None, message: str, from_user: User) -> AlertGroup: | ||
"""Trigger manual integration alert from params.""" | ||
alert_receive_channel = AlertReceiveChannel.get_or_create_manual_integration( | ||
organization=organization, | ||
|
@@ -87,37 +53,24 @@ def _trigger_alert( | |
"verbal_name": f"Direct paging ({team.name if team else 'No'} team)", | ||
}, | ||
) | ||
|
||
channel_filter = None | ||
if alert_receive_channel.default_channel_filter is None: | ||
ChannelFilter.objects.create( | ||
channel_filter = ChannelFilter.objects.create( | ||
alert_receive_channel=alert_receive_channel, | ||
notify_in_slack=True, | ||
is_default=True, | ||
) | ||
|
||
channel_filter = None | ||
if escalation_chain is not None: | ||
channel_filter, _ = ChannelFilter.objects.get_or_create( | ||
alert_receive_channel=alert_receive_channel, | ||
escalation_chain=escalation_chain, | ||
is_default=False, | ||
defaults={ | ||
"filtering_term": f"escalate to {escalation_chain.name}", | ||
"notify_in_slack": True, | ||
}, | ||
) | ||
|
||
permalink = None | ||
if not title: | ||
title = "Message from {}".format(from_user.username) | ||
|
||
title = "Direct page from {}".format(from_user.username) | ||
payload: DirectPagingAlertPayload = { | ||
# Custom oncall property in payload to simplify rendering | ||
"oncall": { | ||
"title": title, | ||
"message": message, | ||
"uid": str(uuid4()), # avoid grouping | ||
"author_username": from_user.username, | ||
"permalink": permalink, | ||
"permalink": None, | ||
}, | ||
} | ||
|
||
|
@@ -134,107 +87,40 @@ def _trigger_alert( | |
return alert.group | ||
|
||
|
||
def check_user_availability(user: User) -> typing.List[AvailabilityWarning]: | ||
"""Check user availability to be paged. | ||
|
||
Return a warnings list indicating `error` and any additional related `data`. | ||
""" | ||
warnings: typing.List[AvailabilityWarning] = [] | ||
if not user.notification_policies.exists(): | ||
warnings.append( | ||
{ | ||
"error": PagingError.USER_HAS_NO_NOTIFICATION_POLICY, | ||
"data": {}, | ||
} | ||
) | ||
|
||
is_on_call = False | ||
schedules = OnCallSchedule.objects.filter( | ||
Q(cached_ical_file_primary__contains=user.username) | Q(cached_ical_file_primary__contains=user.email), | ||
organization=user.organization, | ||
) | ||
schedules_data: ScheduleWarnings = {} | ||
for s in schedules: | ||
# keep track of schedules and on call users to suggest if needed | ||
oncall_users = list_users_to_notify_from_ical(s) | ||
schedules_data[s.name] = set(u.public_primary_key for u in oncall_users) | ||
if user in oncall_users: | ||
is_on_call = True | ||
break | ||
|
||
if not is_on_call: | ||
# user is not on-call | ||
# TODO: check working hours | ||
warnings.append( | ||
{ | ||
"error": PagingError.USER_IS_NOT_ON_CALL, | ||
"data": {"schedules": schedules_data}, | ||
} | ||
) | ||
|
||
return warnings | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we no longer need this (nor the related API endpoint) since there is now an attribute on the |
||
|
||
def direct_paging( | ||
organization: Organization, | ||
team: Team | None, | ||
from_user: User, | ||
title: str = None, | ||
message: str = None, | ||
message: str, | ||
team: Team | None = None, | ||
users: UserNotifications | None = None, | ||
schedules: ScheduleNotifications | None = None, | ||
escalation_chain: EscalationChain | None = None, | ||
alert_group: AlertGroup | None = None, | ||
) -> AlertGroup | None: | ||
"""Trigger escalation targeting given users/schedules. | ||
"""Trigger escalation targeting given team/users. | ||
|
||
If an alert group is given, update escalation to include the specified users. | ||
Otherwise, create a new alert using given title and message. | ||
|
||
Otherwise, create a new alert using given message. | ||
""" | ||
|
||
if users is None: | ||
users = [] | ||
|
||
if schedules is None: | ||
schedules = [] | ||
|
||
if escalation_chain is not None and alert_group is not None: | ||
raise ValueError("Cannot change an existing alert group escalation chain") | ||
if not users and team is None: | ||
raise DirectPagingUserTeamValidationError | ||
|
||
# Cannot add responders to a resolved alert group | ||
if alert_group and alert_group.resolved: | ||
raise DirectPagingAlertGroupResolvedError | ||
|
||
# create alert group if needed | ||
if alert_group is None: | ||
alert_group = _trigger_alert(organization, team, title, message, from_user, escalation_chain=escalation_chain) | ||
|
||
# initialize direct paged users (without a schedule) | ||
users = [(u, important, None) for u, important in users] | ||
|
||
# get on call users, add log entry for each schedule | ||
for s, important in schedules: | ||
oncall_users = list_users_to_notify_from_ical(s) | ||
users += [(u, important, s) for u in oncall_users] | ||
alert_group.log_records.create( | ||
type=AlertGroupLogRecord.TYPE_DIRECT_PAGING, | ||
author=from_user, | ||
reason=f"{from_user.username} paged schedule {s.name}", | ||
step_specific_info={"schedule": s.public_primary_key}, | ||
) | ||
alert_group = _trigger_alert(organization, team, message, from_user) | ||
|
||
for u, important, schedule in users: | ||
reason = f"{from_user.username} paged user {u.username}" | ||
if schedule: | ||
reason += f" (from schedule {schedule.name})" | ||
for u, important in users: | ||
alert_group.log_records.create( | ||
type=AlertGroupLogRecord.TYPE_DIRECT_PAGING, | ||
author=from_user, | ||
reason=reason, | ||
reason=f"{from_user.username} paged user {u.username}", | ||
step_specific_info={ | ||
"user": u.public_primary_key, | ||
"schedule": schedule.public_primary_key if schedule else None, | ||
"important": important, | ||
}, | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this
important
key here is really the only reason I modified this method to returntyping.List[PagedUser]
instead of the originaltyping.List[User]
(obviously don't have the "important" context in the prior approach)