serializers.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from django.conf import settings
  2. from django.urls import reverse
  3. from django.utils import timezone
  4. from rest_framework import serializers
  5. from rest_framework.fields import ChoiceField
  6. from .models import Monitor, MonitorCheck, MonitorType
  7. class MonitorCheckSerializer(serializers.ModelSerializer):
  8. isUp = serializers.BooleanField(source="is_up")
  9. startCheck = serializers.DateTimeField(source="start_check")
  10. responseTime = serializers.DurationField(source="response_time")
  11. class Meta:
  12. model = MonitorCheck
  13. fields = ("isUp", "startCheck", "reason", "responseTime")
  14. class HeartBeatCheckSerializer(MonitorCheckSerializer):
  15. start_check = serializers.DateTimeField(
  16. default=timezone.now, help_text="Optional, set server check start time."
  17. )
  18. class Meta(MonitorCheckSerializer.Meta):
  19. fields = ("is_up", "start_check")
  20. read_only_fields = ("is_up",)
  21. class MonitorSerializer(serializers.ModelSerializer):
  22. isUp = serializers.SerializerMethodField()
  23. lastChange = serializers.SerializerMethodField()
  24. monitorType = ChoiceField(choices=MonitorType.choices, source="monitor_type")
  25. expectedStatus = serializers.IntegerField(source="expected_status")
  26. heartbeatEndpoint = serializers.SerializerMethodField()
  27. projectName = serializers.SerializerMethodField()
  28. envName = serializers.SerializerMethodField()
  29. checks = MonitorCheckSerializer(many=True, read_only=True)
  30. def get_isUp(self, obj):
  31. if hasattr(obj, "latest_is_up"):
  32. return obj.latest_is_up
  33. def get_lastChange(self, obj):
  34. if hasattr(obj, "last_change"):
  35. if obj.last_change:
  36. return obj.last_change.isoformat().replace("+00:00", "Z")
  37. def get_heartbeatEndpoint(self, obj):
  38. if obj.endpoint_id:
  39. return settings.GLITCHTIP_URL.geturl() + reverse(
  40. "heartbeat-check",
  41. kwargs={
  42. "organization_slug": obj.organization.slug,
  43. "endpoint_id": obj.endpoint_id,
  44. },
  45. )
  46. def get_projectName(self, obj):
  47. if obj.project:
  48. return obj.project.name
  49. def get_envName(self, obj):
  50. if obj.environment:
  51. return obj.environment.name
  52. class Meta:
  53. model = Monitor
  54. fields = (
  55. "id",
  56. "monitorType",
  57. "endpoint_id",
  58. "created",
  59. "checks",
  60. "name",
  61. "url",
  62. "expectedStatus",
  63. "expected_body",
  64. "environment",
  65. "project",
  66. "organization",
  67. "interval",
  68. "isUp",
  69. "lastChange",
  70. "heartbeatEndpoint",
  71. "projectName",
  72. "envName",
  73. )
  74. read_only_fields = (
  75. "organization",
  76. "isUp",
  77. "lastChange",
  78. "endpoint_id",
  79. "created",
  80. "heartbeatEndpoint",
  81. "projectName",
  82. "envName",
  83. )
  84. def validate(self, data):
  85. if data["url"] == "" and data["monitor_type"] in [
  86. MonitorType.GET,
  87. MonitorType.PING,
  88. MonitorType.POST,
  89. MonitorType.SSL,
  90. ]:
  91. raise serializers.ValidationError(
  92. "URL is required for " + data["monitor_type"]
  93. )
  94. return data