models.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import uuid
  2. from django.contrib.postgres.search import SearchVectorField
  3. from django.db import models
  4. from apps.projects.tasks import update_transaction_event_project_hourly_statistic
  5. from glitchtip.base_models import CreatedModel
  6. from psqlextra.models import PostgresPartitionedModel
  7. from psqlextra.types import PostgresPartitioningMethod
  8. class TransactionGroup(CreatedModel):
  9. transaction = models.CharField(max_length=1024)
  10. project = models.ForeignKey("projects.Project", on_delete=models.CASCADE)
  11. op = models.CharField(max_length=255)
  12. method = models.CharField(max_length=255, null=True, blank=True)
  13. tags = models.JSONField(default=dict)
  14. search_vector = SearchVectorField(null=True, editable=False)
  15. class Meta:
  16. unique_together = (("transaction", "project", "op", "method"),)
  17. def __str__(self):
  18. return self.transaction
  19. class TransactionEvent(PostgresPartitionedModel, models.Model):
  20. event_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  21. group = models.ForeignKey(TransactionGroup, on_delete=models.CASCADE)
  22. trace_id = models.UUIDField(db_index=True)
  23. start_timestamp = models.DateTimeField(
  24. db_index=True,
  25. help_text="Datetime reported by client as the time the measurement started",
  26. )
  27. timestamp = models.DateTimeField(
  28. blank=True,
  29. null=True,
  30. help_text="Datetime reported by client as the time the measurement finished",
  31. )
  32. duration = models.PositiveIntegerField(db_index=True, help_text="Milliseconds")
  33. data = models.JSONField(help_text="General event data that is searchable")
  34. # This could be HStore, but jsonb is just as good and removes need for
  35. # 'django.contrib.postgres' which makes several unnecessary SQL calls
  36. tags = models.JSONField(default=dict)
  37. class Meta:
  38. ordering = ["-start_timestamp"]
  39. class PartitioningMeta:
  40. method = PostgresPartitioningMethod.RANGE
  41. key = ["start_timestamp"]
  42. def __str__(self):
  43. return str(self.trace_id)
  44. def save(self, *args, **kwargs):
  45. is_new = self._state.adding
  46. super().save(*args, **kwargs)
  47. if is_new:
  48. update_transaction_event_project_hourly_statistic(
  49. args=[self.group.project_id, self.start_timestamp], countdown=60
  50. )