models.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from django.db import models
  2. from django.db.models import F, Avg
  3. from django.contrib.postgres.fields import HStoreField
  4. from django.contrib.postgres.search import SearchVectorField
  5. from glitchtip.base_models import CreatedModel
  6. from events.models import AbstractEvent
  7. avg_transactionevent_time = Avg(
  8. F("transactionevent__timestamp") - F("transactionevent__start_timestamp"),
  9. distinct=True,
  10. )
  11. class TransactionGroupManager(models.Manager):
  12. def with_avgs(self):
  13. return self.annotate(avg_duration=avg_transactionevent_time)
  14. class TransactionGroup(CreatedModel):
  15. transaction = models.CharField(max_length=1024)
  16. project = models.ForeignKey("projects.Project", on_delete=models.CASCADE)
  17. op = models.CharField(max_length=255)
  18. method = models.CharField(max_length=255, null=True, blank=True)
  19. tags = models.JSONField(default=dict)
  20. search_vector = SearchVectorField(null=True, editable=False)
  21. objects = TransactionGroupManager()
  22. class Meta:
  23. unique_together = (("transaction", "project", "op", "method"),)
  24. def __str__(self):
  25. return self.transaction
  26. class TransactionEvent(AbstractEvent):
  27. group = models.ForeignKey(TransactionGroup, on_delete=models.CASCADE)
  28. trace_id = models.UUIDField(db_index=True)
  29. start_timestamp = models.DateTimeField()
  30. tags = HStoreField(default=dict)
  31. class Meta:
  32. ordering = ["-created"]
  33. def __str__(self):
  34. return str(self.trace_id)
  35. class Span(CreatedModel):
  36. transaction = models.ForeignKey(TransactionEvent, on_delete=models.CASCADE)
  37. span_id = models.CharField(max_length=16)
  38. parent_span_id = models.CharField(max_length=16, null=True, blank=True)
  39. # same_process_as_parent bool - we don't use this currently
  40. op = models.CharField(max_length=255)
  41. description = models.CharField(max_length=1024, null=True, blank=True)
  42. start_timestamp = models.DateTimeField()
  43. timestamp = models.DateTimeField()
  44. tags = HStoreField(default=dict)
  45. data = models.JSONField(default=dict)
  46. def __str__(self):
  47. return self.span_id