models.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import uuid
  2. from django.contrib.postgres.fields import HStoreField
  3. from django.contrib.postgres.search import SearchVectorField
  4. from django.db import models
  5. from apps.projects.tasks import update_transaction_event_project_hourly_statistic
  6. from glitchtip.base_models import CreatedModel
  7. class AbstractEvent(CreatedModel):
  8. event_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  9. data = models.JSONField(help_text="General event data that is searchable")
  10. timestamp = models.DateTimeField(
  11. blank=True,
  12. null=True,
  13. help_text="Date created as claimed by client it came from",
  14. )
  15. class Meta:
  16. abstract = True
  17. def __str__(self):
  18. return self.event_id_hex
  19. @property
  20. def event_id_hex(self):
  21. """The public key without dashes"""
  22. if self.event_id:
  23. if isinstance(self.event_id, str):
  24. return self.event_id
  25. return self.event_id.hex
  26. class TransactionGroup(CreatedModel):
  27. transaction = models.CharField(max_length=1024)
  28. project = models.ForeignKey("projects.Project", on_delete=models.CASCADE)
  29. op = models.CharField(max_length=255)
  30. method = models.CharField(max_length=255, null=True, blank=True)
  31. tags = models.JSONField(default=dict)
  32. search_vector = SearchVectorField(null=True, editable=False)
  33. class Meta:
  34. unique_together = (("transaction", "project", "op", "method"),)
  35. def __str__(self):
  36. return self.transaction
  37. class TransactionEvent(AbstractEvent):
  38. group = models.ForeignKey(TransactionGroup, on_delete=models.CASCADE)
  39. trace_id = models.UUIDField(db_index=True)
  40. start_timestamp = models.DateTimeField()
  41. duration = models.PositiveIntegerField(db_index=True, help_text="Milliseconds")
  42. tags = HStoreField(default=dict)
  43. class Meta:
  44. ordering = ["-created"]
  45. def __str__(self):
  46. return str(self.trace_id)
  47. def save(self, *args, **kwargs):
  48. is_new = self._state.adding
  49. super().save(*args, **kwargs)
  50. if is_new:
  51. update_transaction_event_project_hourly_statistic(
  52. args=[self.group.project_id, self.created], countdown=60
  53. )
  54. class Span(CreatedModel):
  55. transaction = models.ForeignKey(TransactionEvent, on_delete=models.CASCADE)
  56. span_id = models.CharField(max_length=16)
  57. parent_span_id = models.CharField(max_length=16, null=True, blank=True)
  58. # same_process_as_parent bool - we don't use this currently
  59. op = models.CharField(max_length=255, blank=True)
  60. description = models.CharField(max_length=2000, null=True, blank=True)
  61. start_timestamp = models.DateTimeField()
  62. timestamp = models.DateTimeField()
  63. tags = HStoreField(default=dict)
  64. data = models.JSONField(default=dict)
  65. def __str__(self):
  66. return self.span_id