admin.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from django.contrib import admin
  2. from django.db.models import Avg, F
  3. from .models import Span, TransactionEvent, TransactionGroup
  4. class TransactionGroupAdmin(admin.ModelAdmin):
  5. search_fields = ["transaction", "op", "project__organization__name"]
  6. list_display = ["transaction", "project", "op", "method", "avg_duration"]
  7. list_filter = ["created", "op", "method"]
  8. autocomplete_fields = ["project"]
  9. def avg_duration(self, obj):
  10. return obj.avg_duration
  11. def get_queryset(self, request):
  12. return (
  13. super()
  14. .get_queryset(request)
  15. .annotate(avg_duration=Avg("transactionevent__duration"))
  16. )
  17. class SpanInline(admin.TabularInline):
  18. model = Span
  19. extra = 0
  20. readonly_fields = [
  21. "span_id",
  22. "parent_span_id",
  23. "op",
  24. "description",
  25. "start_timestamp",
  26. "timestamp",
  27. "tags",
  28. "data",
  29. ]
  30. def has_add_permission(self, request, *args, **kwargs):
  31. return False
  32. class TransactionEventAdmin(admin.ModelAdmin):
  33. search_fields = [
  34. "trace_id",
  35. "group__transaction",
  36. "group__project__organization__name",
  37. ]
  38. list_display = ["trace_id", "group", "timestamp", "duration"]
  39. list_filter = ["created"]
  40. inlines = [SpanInline]
  41. can_delete = False
  42. def duration(self, obj):
  43. return obj.duration
  44. def get_queryset(self, request):
  45. qs = super().get_queryset(request)
  46. qs = qs.annotate(duration=F("timestamp") - F("start_timestamp"))
  47. return qs
  48. class SpanAdmin(admin.ModelAdmin):
  49. search_fields = [
  50. "span_id",
  51. "op",
  52. "description",
  53. "transaction__trace_id",
  54. "transaction__group__project__organization__name",
  55. ]
  56. list_display = ["span_id", "transaction", "op", "description", "duration"]
  57. list_filter = ["created", "op"]
  58. def duration(self, obj):
  59. return obj.duration
  60. def get_queryset(self, request):
  61. qs = super().get_queryset(request)
  62. qs = qs.annotate(duration=F("timestamp") - F("start_timestamp"))
  63. return qs
  64. admin.site.register(TransactionGroup, TransactionGroupAdmin)
  65. admin.site.register(TransactionEvent, TransactionEventAdmin)
  66. admin.site.register(Span, SpanAdmin)