filters.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from django.db.models import Avg, Count
  2. from django_filters import rest_framework as filters
  3. from glitchtip.filters import StartEndRelativeIsoDateTimeRangeFilter
  4. from projects.models import Project
  5. from .models import TransactionGroup
  6. class TransactionGroupFilter(filters.FilterSet):
  7. transaction_created = StartEndRelativeIsoDateTimeRangeFilter(
  8. field_name="transactionevent__created",
  9. label="Transaction created",
  10. )
  11. project = filters.ModelMultipleChoiceFilter(queryset=Project.objects.all())
  12. query = filters.CharFilter(
  13. field_name="transaction",
  14. lookup_expr="icontains",
  15. label="Transaction text search",
  16. )
  17. class Meta:
  18. model = TransactionGroup
  19. fields = ["project", "transaction_created"]
  20. def filter_queryset(self, queryset):
  21. queryset = super().filter_queryset(queryset)
  22. environments = self.request.query_params.getlist("environment")
  23. if environments:
  24. queryset = queryset.filter(tags__environment__has_any_keys=environments)
  25. # This annotation must be applied after any related transactionevent filter
  26. queryset = queryset.annotate(
  27. avg_duration=Avg("transactionevent__duration"),
  28. transaction_count=Count("transactionevent"),
  29. )
  30. return queryset