models.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import uuid
  2. from django.contrib.postgres.search import SearchVectorField
  3. from django.db import models
  4. from glitchtip.base_models import CreatedModel
  5. from psqlextra.models import PostgresPartitionedModel
  6. from psqlextra.types import PostgresPartitioningMethod
  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(PostgresPartitionedModel, models.Model):
  19. event_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  20. group = models.ForeignKey(TransactionGroup, on_delete=models.CASCADE)
  21. trace_id = models.UUIDField(db_index=True)
  22. start_timestamp = models.DateTimeField(
  23. db_index=True,
  24. help_text="Datetime reported by client as the time the measurement started",
  25. )
  26. timestamp = models.DateTimeField(
  27. blank=True,
  28. null=True,
  29. help_text="Datetime reported by client as the time the measurement finished",
  30. )
  31. duration = models.PositiveIntegerField(db_index=True, help_text="Milliseconds")
  32. data = models.JSONField(help_text="General event data that is searchable")
  33. # This could be HStore, but jsonb is just as good and removes need for
  34. # 'django.contrib.postgres' which makes several unnecessary SQL calls
  35. tags = models.JSONField(default=dict)
  36. class Meta:
  37. ordering = ["-start_timestamp"]
  38. class PartitioningMeta:
  39. method = PostgresPartitioningMethod.RANGE
  40. key = ["start_timestamp"]
  41. def __str__(self):
  42. return str(self.trace_id)