# What this PR does Adds a new filter on alert groups page that allows to filter alert groups by escalation chain. <img width="1204" alt="Screenshot 2023-03-13 at 22 42 00" src="https://user-images.githubusercontent.com/20116910/224848730-ef753856-a050-4acb-ba36-498d2bca2b4f.png"> ## Which issue(s) this PR fixes This should be useful on it's own as it's giving more filtering capabilities, but it also could be useful for https://github.com/grafana/oncall/issues/1300, if PD rulesets are migrated to a single integration with multiple escalation chains. ## Checklist - [x] Tests updated - [x] `CHANGELOG.md` updated
40 lines
1.6 KiB
Python
40 lines
1.6 KiB
Python
from rest_framework import serializers
|
|
|
|
from apps.alerts.models import EscalationChain
|
|
from common.api_helpers.custom_fields import TeamPrimaryKeyRelatedField
|
|
from common.api_helpers.utils import CurrentOrganizationDefault, CurrentTeamDefault
|
|
|
|
|
|
class EscalationChainSerializer(serializers.ModelSerializer):
|
|
id = serializers.CharField(read_only=True, source="public_primary_key")
|
|
organization = serializers.HiddenField(default=CurrentOrganizationDefault())
|
|
team = TeamPrimaryKeyRelatedField(allow_null=True, default=CurrentTeamDefault())
|
|
|
|
class Meta:
|
|
model = EscalationChain
|
|
fields = ("id", "name", "organization", "team")
|
|
|
|
|
|
class EscalationChainListSerializer(EscalationChainSerializer):
|
|
number_of_integrations = serializers.SerializerMethodField()
|
|
number_of_routes = serializers.SerializerMethodField()
|
|
|
|
class Meta(EscalationChainSerializer.Meta):
|
|
fields = [*EscalationChainSerializer.Meta.fields, "number_of_integrations", "number_of_routes"]
|
|
|
|
def get_number_of_integrations(self, obj):
|
|
# num_integrations param added in queryset via annotate. Check EscalationChainViewSet.get_queryset
|
|
return getattr(obj, "num_integrations")
|
|
|
|
def get_number_of_routes(self, obj):
|
|
# num_routes param added in queryset via annotate. Check EscalationChainViewSet.get_queryset
|
|
return getattr(obj, "num_routes")
|
|
|
|
|
|
class FilterEscalationChainSerializer(serializers.ModelSerializer):
|
|
value = serializers.CharField(source="public_primary_key")
|
|
display_name = serializers.CharField(source="name")
|
|
|
|
class Meta:
|
|
model = EscalationChain
|
|
fields = ["value", "display_name"]
|