Add routes_count and connected_escalations_chains_count to alert_rece… (#1874)

…ive_channels endpoint

# What this PR does

## Which issue(s) this PR fixes

## Checklist

- [ ] Unit, integration, and e2e (if applicable) tests updated
- [ ] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
This commit is contained in:
Ildar Iskhakov 2023-05-04 13:31:48 +08:00 committed by GitHub
parent 7cf0c4f693
commit be35e579ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,6 +12,7 @@ from rest_framework.fields import SerializerMethodField, set_value
from apps.alerts.grafana_alerting_sync_manager.grafana_alerting_sync import GrafanaAlertingSyncManager
from apps.alerts.models import AlertReceiveChannel
from apps.alerts.models.channel_filter import ChannelFilter
from apps.base.messaging import get_messaging_backends
from common.api_helpers.custom_fields import TeamPrimaryKeyRelatedField
from common.api_helpers.exceptions import BadRequest
@ -49,6 +50,8 @@ class AlertReceiveChannelSerializer(EagerLoadingMixin, serializers.ModelSerializ
allow_delete = serializers.SerializerMethodField()
description_short = serializers.CharField(max_length=250, required=False)
demo_alert_payload = serializers.SerializerMethodField()
routes_count = serializers.SerializerMethodField()
connected_escalations_chains_count = serializers.SerializerMethodField()
# integration heartbeat is in PREFETCH_RELATED not by mistake.
# With using of select_related ORM builds strange join
@ -83,6 +86,8 @@ class AlertReceiveChannelSerializer(EagerLoadingMixin, serializers.ModelSerializ
"is_available_for_integration_heartbeat",
"allow_delete",
"demo_alert_payload",
"routes_count",
"connected_escalations_chains_count",
]
read_only_fields = [
"created_at",
@ -94,6 +99,8 @@ class AlertReceiveChannelSerializer(EagerLoadingMixin, serializers.ModelSerializ
"demo_alert_enabled",
"maintenance_mode",
"demo_alert_payload",
"routes_count",
"connected_escalations_chains_count",
]
extra_kwargs = {"integration": {"required": True}}
@ -163,6 +170,14 @@ class AlertReceiveChannelSerializer(EagerLoadingMixin, serializers.ModelSerializ
return "{}"
return None
def get_routes_count(self, obj) -> int:
return obj.channel_filters.count()
def get_connected_escalations_chains_count(self, obj) -> int:
return len(
set(ChannelFilter.objects.filter(alert_receive_channel=obj).values_list("escalation_chain", flat=True))
)
class AlertReceiveChannelUpdateSerializer(AlertReceiveChannelSerializer):
class Meta(AlertReceiveChannelSerializer.Meta):