-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add endpoint for alert group escalation snapshot (#3615)
# What this PR does Adds endpoint for alert group escalation snapshot ## Which issue(s) this PR fixes #3277 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required)
- Loading branch information
Showing
7 changed files
with
243 additions
and
0 deletions.
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
55 changes: 55 additions & 0 deletions
55
engine/apps/api/serializers/alert_group_escalation_snapshot.py
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from rest_framework import serializers | ||
|
||
from apps.api.serializers.custom_button import CustomButtonFastSerializer | ||
from apps.api.serializers.escalation_policy import EscalationPolicySerializer | ||
from apps.api.serializers.schedule_base import ScheduleFastSerializer | ||
from apps.api.serializers.user import FastUserSerializer | ||
from apps.api.serializers.user_group import UserGroupSerializer | ||
from apps.api.serializers.webhook import WebhookFastSerializer | ||
|
||
|
||
class EscalationPolicySnapshotAPISerializer(EscalationPolicySerializer): | ||
"""Serializes AlertGroup escalation policies snapshots for API endpoint""" | ||
|
||
notify_to_users_queue = FastUserSerializer(many=True, read_only=True) | ||
notify_schedule = ScheduleFastSerializer(read_only=True) | ||
notify_to_group = UserGroupSerializer(read_only=True) | ||
custom_button_trigger = CustomButtonFastSerializer(read_only=True) | ||
custom_webhook = WebhookFastSerializer(read_only=True) | ||
|
||
class Meta(EscalationPolicySerializer.Meta): | ||
fields = [ | ||
"step", | ||
"wait_delay", | ||
"notify_to_users_queue", | ||
"from_time", | ||
"to_time", | ||
"num_alerts_in_window", | ||
"num_minutes_in_window", | ||
"slack_integration_required", | ||
"custom_button_trigger", | ||
"custom_webhook", | ||
"notify_schedule", | ||
"notify_to_group", | ||
"important", | ||
] | ||
read_only_fields = fields | ||
|
||
|
||
class AlertGroupEscalationSnapshotAPISerializer(serializers.Serializer): | ||
"""Serializes AlertGroup escalation snapshot for API endpoint""" | ||
|
||
escalation_chain = serializers.SerializerMethodField() | ||
channel_filter = serializers.SerializerMethodField() | ||
escalation_policies = EscalationPolicySnapshotAPISerializer( | ||
source="escalation_policies_snapshots", many=True, read_only=True | ||
) | ||
|
||
class Meta: | ||
fields = ["escalation_chain", "channel_filter", "escalation_policies"] | ||
|
||
def get_escalation_chain(self, obj): | ||
return {"name": obj.escalation_chain_snapshot.name} | ||
|
||
def get_channel_filter(self, obj): | ||
return {"name": obj.channel_filter_snapshot.str_for_clients} |
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
124 changes: 124 additions & 0 deletions
124
engine/apps/api/tests/test_alert_group_escalation_snapshot.py
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import pytest | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
|
||
from apps.alerts.models import EscalationPolicy | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_alert_group_escalation_snapshot_with_important( | ||
make_organization_and_user_with_plugin_token, | ||
make_alert_receive_channel, | ||
make_escalation_chain, | ||
make_escalation_policy, | ||
make_channel_filter, | ||
make_alert_group, | ||
make_user_auth_headers, | ||
): | ||
organization, user, token = make_organization_and_user_with_plugin_token() | ||
alert_receive_channel = make_alert_receive_channel(organization) | ||
escalation_chain = make_escalation_chain(organization) | ||
notify_to_multiple_users_step = make_escalation_policy( | ||
escalation_chain=escalation_chain, | ||
escalation_policy_step=EscalationPolicy.STEP_NOTIFY_MULTIPLE_USERS, | ||
) | ||
notify_to_multiple_users_step.notify_to_users_queue.set([user]) | ||
notify_to_multiple_users_step_important = make_escalation_policy( | ||
escalation_chain=escalation_chain, | ||
escalation_policy_step=EscalationPolicy.STEP_NOTIFY_MULTIPLE_USERS_IMPORTANT, | ||
) | ||
notify_to_multiple_users_step_important.notify_to_users_queue.set([user]) | ||
channel_filter = make_channel_filter(alert_receive_channel, is_default=True, escalation_chain=escalation_chain) | ||
alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter) | ||
alert_group.raw_escalation_snapshot = alert_group.build_raw_escalation_snapshot() | ||
alert_group.save() | ||
|
||
client = APIClient() | ||
url = reverse("api-internal:alertgroup-escalation-snapshot", kwargs={"pk": alert_group.public_primary_key}) | ||
expected_result = { | ||
"escalation_chain": {"name": escalation_chain.name}, | ||
"channel_filter": {"name": "default"}, | ||
"escalation_policies": [ | ||
{ | ||
"step": 13, | ||
"wait_delay": None, | ||
"notify_to_users_queue": [{"pk": user.public_primary_key, "username": user.username}], | ||
"from_time": None, | ||
"to_time": None, | ||
"num_alerts_in_window": None, | ||
"num_minutes_in_window": None, | ||
"custom_button_trigger": None, | ||
"custom_webhook": None, | ||
"notify_schedule": None, | ||
"notify_to_group": None, | ||
"important": False, | ||
}, | ||
{ | ||
"step": 13, | ||
"wait_delay": None, | ||
"notify_to_users_queue": [{"pk": user.public_primary_key, "username": user.username}], | ||
"from_time": None, | ||
"to_time": None, | ||
"num_alerts_in_window": None, | ||
"num_minutes_in_window": None, | ||
"custom_button_trigger": None, | ||
"custom_webhook": None, | ||
"notify_schedule": None, | ||
"notify_to_group": None, | ||
"important": True, | ||
}, | ||
], | ||
} | ||
response = client.get(url, **make_user_auth_headers(user, token)) | ||
assert response.status_code == status.HTTP_200_OK | ||
assert response.json() == expected_result | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_alert_group_no_escalation_snapshot( | ||
make_organization_and_user_with_plugin_token, | ||
make_alert_receive_channel, | ||
make_channel_filter, | ||
make_alert_group, | ||
make_user_auth_headers, | ||
): | ||
organization, user, token = make_organization_and_user_with_plugin_token() | ||
alert_receive_channel = make_alert_receive_channel(organization) | ||
channel_filter = make_channel_filter(alert_receive_channel, is_default=True) | ||
alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter) | ||
client = APIClient() | ||
url = reverse("api-internal:alertgroup-escalation-snapshot", kwargs={"pk": alert_group.public_primary_key}) | ||
expected_result = {} | ||
response = client.get(url, **make_user_auth_headers(user, token)) | ||
assert response.status_code == status.HTTP_200_OK | ||
assert response.json() == expected_result | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_alert_group_escalation_snapshot_no_policies( | ||
make_organization_and_user_with_plugin_token, | ||
make_alert_receive_channel, | ||
make_escalation_chain, | ||
make_channel_filter, | ||
make_alert_group, | ||
make_user_auth_headers, | ||
): | ||
organization, user, token = make_organization_and_user_with_plugin_token() | ||
alert_receive_channel = make_alert_receive_channel(organization) | ||
escalation_chain = make_escalation_chain(organization) | ||
channel_filter = make_channel_filter(alert_receive_channel, is_default=True, escalation_chain=escalation_chain) | ||
alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter) | ||
alert_group.raw_escalation_snapshot = alert_group.build_raw_escalation_snapshot() | ||
alert_group.save() | ||
|
||
client = APIClient() | ||
url = reverse("api-internal:alertgroup-escalation-snapshot", kwargs={"pk": alert_group.public_primary_key}) | ||
expected_result = { | ||
"escalation_chain": {"name": escalation_chain.name}, | ||
"channel_filter": {"name": "default"}, | ||
"escalation_policies": [], | ||
} | ||
response = client.get(url, **make_user_auth_headers(user, token)) | ||
assert response.status_code == status.HTTP_200_OK | ||
assert response.json() == expected_result |
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