admin.py 2.4 KB

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