admin.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from django.conf import settings
  2. from django.contrib import admin
  3. from django.forms.models import BaseInlineFormSet
  4. from django.urls import reverse
  5. from django.utils import timezone
  6. from .models import Monitor, MonitorCheck, StatusPage
  7. class MonitorCheckInlineFormSet(BaseInlineFormSet):
  8. def get_queryset(self):
  9. if not hasattr(self, "_queryset"):
  10. # pylint: disable=attribute-defined-outside-init
  11. self._queryset = super().get_queryset()[:50] # noqa
  12. return self._queryset
  13. class MonitorCheckInlineAdmin(admin.TabularInline):
  14. model = MonitorCheck
  15. formset = MonitorCheckInlineFormSet
  16. can_delete = False
  17. def has_add_permission(self, request, obj=None):
  18. return False
  19. def has_change_permission(self, request, obj=None):
  20. return False
  21. class MonitorAdmin(admin.ModelAdmin):
  22. list_display = [
  23. "name",
  24. "is_up",
  25. "time_since",
  26. "monitor_type",
  27. "organization",
  28. "interval",
  29. ]
  30. readonly_fields = ["heartbeat_endpoint"]
  31. list_filter = ["monitor_type"]
  32. search_fields = ["name", "organization__name"]
  33. inlines = [MonitorCheckInlineAdmin]
  34. def get_queryset(self, request):
  35. qs = self.model.objects.with_check_annotations()
  36. ordering = self.get_ordering(request)
  37. if ordering:
  38. qs = qs.order_by(*ordering)
  39. return qs
  40. def is_up(self, obj):
  41. return obj.latest_is_up
  42. is_up.boolean = True
  43. def time_since(self, obj):
  44. if obj.last_change:
  45. now = timezone.now()
  46. return now - obj.last_change
  47. def heartbeat_endpoint(self, obj):
  48. if obj.endpoint_id:
  49. return settings.GLITCHTIP_URL.geturl() + reverse(
  50. "heartbeat-check",
  51. kwargs={
  52. "organization_slug": obj.organization.slug,
  53. "endpoint_id": obj.endpoint_id,
  54. },
  55. )
  56. class MonitorCheckAdmin(admin.ModelAdmin):
  57. list_filter = ["is_up", "reason", "start_check"]
  58. list_display = ["monitor", "is_up", "reason", "start_check", "response_time"]
  59. class StatusPageAdmin(admin.ModelAdmin):
  60. list_display = ["organization", "name", "is_public"]
  61. admin.site.register(Monitor, MonitorAdmin)
  62. admin.site.register(MonitorCheck, MonitorCheckAdmin)
  63. admin.site.register(StatusPage, StatusPageAdmin)