admin.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. class SpanAdmin(admin.ModelAdmin):
  43. search_fields = [
  44. "span_id",
  45. "op",
  46. "description",
  47. "transaction__trace_id",
  48. "transaction__group__project__organization__name",
  49. ]
  50. list_display = ["span_id", "transaction", "op", "description", "duration"]
  51. list_filter = ["created", "op"]
  52. def duration(self, obj):
  53. return obj.duration
  54. def get_queryset(self, request):
  55. qs = super().get_queryset(request)
  56. qs = qs.annotate(duration=F("timestamp") - F("start_timestamp"))
  57. return qs
  58. admin.site.register(TransactionGroup, TransactionGroupAdmin)
  59. admin.site.register(TransactionEvent, TransactionEventAdmin)
  60. admin.site.register(Span, SpanAdmin)