oncall-engine/engine/apps/api/serializers/integration_heartbeat.py
Michael Derynck 6b40f95033 World, meet OnCall!
Co-authored-by: Eve832 <eve.meelan@grafana.com>
    Co-authored-by: Francisco Montes de Oca <nevermind89x@gmail.com>
    Co-authored-by: Ildar Iskhakov <ildar.iskhakov@grafana.com>
    Co-authored-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>
    Co-authored-by: Julia <ferril.darkdiver@gmail.com>
    Co-authored-by: maskin25 <kengurek@gmail.com>
    Co-authored-by: Matias Bordese <mbordese@gmail.com>
    Co-authored-by: Matvey Kukuy <motakuk@gmail.com>
    Co-authored-by: Michael Derynck <michael.derynck@grafana.com>
    Co-authored-by: Richard Hartmann <richih@richih.org>
    Co-authored-by: Robby Milo <robbymilo@fastmail.com>
    Co-authored-by: Timur Olzhabayev <timur.olzhabayev@grafana.com>
    Co-authored-by: Vadim Stepanov <vadimkerr@gmail.com>
    Co-authored-by: Yulia Shanyrova <yulia.shanyrova@grafana.com>
2022-06-03 08:09:47 -06:00

69 lines
2.5 KiB
Python

import humanize
from django.conf import settings
from django.template.loader import render_to_string
from django.utils import timezone
from rest_framework import serializers
from apps.alerts.models import AlertReceiveChannel
from apps.heartbeat.models import IntegrationHeartBeat
from common.api_helpers.custom_fields import OrganizationFilteredPrimaryKeyRelatedField
from common.api_helpers.mixins import EagerLoadingMixin
NO_INSTRUCTION_MESSAGE = "No instruction"
class IntegrationHeartBeatSerializer(EagerLoadingMixin, serializers.ModelSerializer):
id = serializers.CharField(read_only=True, source="public_primary_key")
alert_receive_channel = OrganizationFilteredPrimaryKeyRelatedField(queryset=AlertReceiveChannel.objects)
timeout_seconds = serializers.ChoiceField(
allow_null=False,
required=True,
choices=IntegrationHeartBeat.TIMEOUT_CHOICES,
)
last_heartbeat_time_verbal = serializers.SerializerMethodField()
instruction = serializers.SerializerMethodField()
SELECT_RELATED = ["alert_receive_channel"]
class Meta:
model = IntegrationHeartBeat
fields = [
"id",
"timeout_seconds",
"alert_receive_channel",
"link",
"last_heartbeat_time_verbal",
"status",
"instruction",
]
def validate_alert_receive_channel(self, alert_receive_channel):
if alert_receive_channel.is_available_for_integration_heartbeat:
return alert_receive_channel
else:
raise serializers.ValidationError(
{"alert_receive_channel": "Heartbeat is not available for this integration"}
)
def get_last_heartbeat_time_verbal(self, obj):
return self._last_heartbeat_time_verbal(obj) if obj.last_heartbeat_time else None
def get_instruction(self, obj):
rendered_instruction = render_to_string(
obj.alert_receive_channel.heartbeat_instruction_template,
{
"heartbeat_url": obj.link,
"service_url": settings.BASE_URL,
},
)
return rendered_instruction
@staticmethod
def _last_heartbeat_time_verbal(instance):
"""
This method simplifies testing.
To compare expected_payload with response.json() it is needed to calculate "now" same way in test and serializer.
It is easier to implement separate method and mock in tests.
"""
now = timezone.now()
return humanize.naturaldelta(now - instance.last_heartbeat_time)