serializers.py 3.8 KB

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