models.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from django.contrib.postgres.fields import HStoreField
  2. from django.contrib.postgres.search import SearchVectorField
  3. from django.db import models
  4. from events.models import AbstractEvent
  5. from glitchtip.base_models import CreatedModel
  6. from projects.tasks import update_transaction_event_project_hourly_statistic
  7. class TransactionGroup(CreatedModel):
  8. transaction = models.CharField(max_length=1024)
  9. project = models.ForeignKey("projects.Project", on_delete=models.CASCADE)
  10. op = models.CharField(max_length=255)
  11. method = models.CharField(max_length=255, null=True, blank=True)
  12. tags = models.JSONField(default=dict)
  13. search_vector = SearchVectorField(null=True, editable=False)
  14. class Meta:
  15. unique_together = (("transaction", "project", "op", "method"),)
  16. def __str__(self):
  17. return self.transaction
  18. class TransactionEvent(AbstractEvent):
  19. group = models.ForeignKey(TransactionGroup, on_delete=models.CASCADE)
  20. trace_id = models.UUIDField(db_index=True)
  21. start_timestamp = models.DateTimeField()
  22. duration = models.PositiveIntegerField(db_index=True, help_text="Milliseconds")
  23. tags = HStoreField(default=dict)
  24. class Meta:
  25. ordering = ["-created"]
  26. def __str__(self):
  27. return str(self.trace_id)
  28. def save(self, *args, **kwargs):
  29. is_new = self._state.adding
  30. super().save(*args, **kwargs)
  31. if is_new:
  32. update_transaction_event_project_hourly_statistic(
  33. args=[self.group.project_id, self.created], countdown=60
  34. )
  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, blank=True)
  41. description = models.CharField(max_length=2000, 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