serializers.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. )