views.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from django_filters.rest_framework import DjangoFilterBackend
  2. from rest_framework import viewsets
  3. from rest_framework.filters import OrderingFilter
  4. from projects.models import Project
  5. from .filters import TransactionGroupFilter
  6. from .models import Span, TransactionEvent, TransactionGroup
  7. from .serializers import (
  8. SpanSerializer,
  9. TransactionDetailSerializer,
  10. TransactionGroupSerializer,
  11. TransactionSerializer,
  12. )
  13. class TransactionGroupViewSet(viewsets.ReadOnlyModelViewSet):
  14. """Duration is in milliseconds"""
  15. queryset = TransactionGroup.objects.all()
  16. serializer_class = TransactionGroupSerializer
  17. filter_backends = [DjangoFilterBackend, OrderingFilter]
  18. filterset_class = TransactionGroupFilter
  19. ordering = ["-avg_duration"]
  20. ordering_fields = ["created", "avg_duration", "transaction_count"]
  21. def get_queryset(self):
  22. if not self.request.user.is_authenticated:
  23. return self.queryset.none()
  24. # Performance optimization, force two queries
  25. projects = Project.objects.filter(team__members__user=self.request.user)
  26. if "organization_slug" in self.kwargs:
  27. projects = projects.filter(
  28. organization__slug=self.kwargs["organization_slug"],
  29. )
  30. qs = (
  31. super()
  32. .get_queryset()
  33. .filter(project__pk__in=list(projects.values_list("pk", flat=True)))
  34. .defer("search_vector", "created")
  35. )
  36. return qs
  37. class TransactionViewSet(viewsets.ReadOnlyModelViewSet):
  38. queryset = TransactionEvent.objects.all()
  39. serializer_class = TransactionSerializer
  40. def get_serializer_class(self):
  41. if self.action in ["retrieve"]:
  42. return TransactionDetailSerializer
  43. return super().get_serializer_class()
  44. def get_queryset(self):
  45. if not self.request.user.is_authenticated:
  46. return self.queryset.none()
  47. # Performance optimization, force two queries
  48. projects = list(
  49. Project.objects.filter(team__members__user=self.request.user).values_list(
  50. "pk", flat=True
  51. )
  52. )
  53. qs = super().get_queryset().filter(group__project__pk__in=projects)
  54. if "organization_slug" in self.kwargs:
  55. qs = qs.filter(
  56. group__project__organization__slug=self.kwargs["organization_slug"],
  57. )
  58. qs = qs.select_related("group")
  59. return qs
  60. class SpanViewSet(viewsets.ReadOnlyModelViewSet):
  61. queryset = Span.objects.all()
  62. serializer_class = SpanSerializer
  63. def get_queryset(self):
  64. if not self.request.user.is_authenticated:
  65. return self.queryset.none()
  66. # Performance optimization, force two queries
  67. projects = list(
  68. Project.objects.filter(team__members__user=self.request.user).values_list(
  69. "pk", flat=True
  70. )
  71. )
  72. qs = super().get_queryset().filter(transaction__group__project__pk__in=projects)
  73. if "organization_slug" in self.kwargs:
  74. qs = qs.filter(
  75. transaction__group__project__organization__slug=self.kwargs[
  76. "organization_slug"
  77. ],
  78. )
  79. return qs