views.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from django.db.models import Prefetch
  2. from django.shortcuts import get_object_or_404
  3. from django.utils import timezone
  4. from rest_framework import exceptions, permissions, viewsets
  5. from rest_framework.generics import CreateAPIView
  6. from organizations_ext.models import Organization
  7. from .models import Monitor, MonitorCheck
  8. from .serializers import (
  9. HeartBeatCheckSerializer,
  10. MonitorCheckSerializer,
  11. MonitorSerializer,
  12. )
  13. from .tasks import send_monitor_notification
  14. class HeartBeatCheckView(CreateAPIView):
  15. permission_classes = [permissions.AllowAny]
  16. serializer_class = HeartBeatCheckSerializer
  17. def perform_create(self, serializer):
  18. monitor = get_object_or_404(
  19. Monitor.objects.with_check_annotations(),
  20. organization__slug=self.kwargs.get("organization_slug"),
  21. endpoint_id=self.kwargs.get("endpoint_id"),
  22. )
  23. monitor_check = serializer.save(monitor=monitor, is_up=True)
  24. if monitor.latest_is_up is False:
  25. send_monitor_notification.delay(
  26. monitor_check.pk, False, monitor.last_change
  27. )
  28. class MonitorViewSet(viewsets.ModelViewSet):
  29. queryset = Monitor.objects.with_check_annotations()
  30. serializer_class = MonitorSerializer
  31. def get_queryset(self):
  32. if not self.request.user.is_authenticated:
  33. return self.queryset.none()
  34. queryset = self.queryset.filter(organization__users=self.request.user)
  35. organization_slug = self.kwargs.get("organization_slug")
  36. if organization_slug:
  37. queryset = queryset.filter(organization__slug=organization_slug)
  38. queryset = queryset.prefetch_related(
  39. Prefetch(
  40. "checks",
  41. queryset=MonitorCheck.objects.filter(
  42. created__gte=timezone.now() - timezone.timedelta(hours=1)
  43. ),
  44. )
  45. ).distinct()
  46. return queryset
  47. def perform_create(self, serializer):
  48. try:
  49. organization = Organization.objects.get(
  50. slug=self.kwargs.get("organization_slug"), users=self.request.user
  51. )
  52. except Organization.DoesNotExist:
  53. raise exceptions.ValidationError("Organization not found")
  54. serializer.save(organization=organization)
  55. class MonitorCheckViewSet(viewsets.ReadOnlyModelViewSet):
  56. queryset = MonitorCheck.objects.all()
  57. serializer_class = MonitorCheckSerializer
  58. def get_queryset(self):
  59. if not self.request.user.is_authenticated:
  60. return self.queryset.none()
  61. queryset = self.queryset.filter(monitor__organization__users=self.request.user)
  62. organization_slug = self.kwargs.get("organization_slug")
  63. if organization_slug:
  64. queryset = queryset.filter(monitor__organization__slug=organization_slug)
  65. monitor_pk = self.kwargs.get("monitor_pk")
  66. if monitor_pk:
  67. queryset = queryset.filter(monitor__pk=monitor_pk)
  68. return queryset