models.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import uuid
  2. from django.contrib.postgres.indexes import GinIndex
  3. from django.contrib.postgres.search import SearchVectorField
  4. from django.db import models
  5. from psqlextra.models import PostgresPartitionedModel
  6. from psqlextra.types import PostgresPartitioningMethod
  7. from .constants import EventStatus, IssueEventType, LogLevel
  8. class Issue(models.Model):
  9. created = models.DateTimeField(auto_now_add=True)
  10. culprit = models.CharField(max_length=1024, blank=True, null=True)
  11. is_public = models.BooleanField(default=False)
  12. level = models.PositiveSmallIntegerField(
  13. choices=LogLevel.choices, default=LogLevel.ERROR
  14. )
  15. project = models.ForeignKey(
  16. "projects.Project", on_delete=models.CASCADE, related_name="issues"
  17. )
  18. title = models.CharField(max_length=255)
  19. type = models.PositiveSmallIntegerField(
  20. choices=IssueEventType.choices, default=IssueEventType.DEFAULT
  21. )
  22. status = models.PositiveSmallIntegerField(
  23. choices=EventStatus.choices, default=EventStatus.UNRESOLVED
  24. )
  25. short_id = models.PositiveIntegerField(null=True)
  26. class Meta:
  27. unique_together = (("project", "short_id"),)
  28. class IssueStats(models.Model):
  29. issue = models.OneToOneField(Issue, primary_key=True, on_delete=models.CASCADE)
  30. search_vector = SearchVectorField(null=True, editable=False)
  31. search_vector_created = models.DateTimeField(auto_now_add=True)
  32. count = models.PositiveIntegerField(default=1, editable=False)
  33. last_seen = models.DateTimeField(auto_now_add=True, db_index=True)
  34. class Meta:
  35. indexes = [GinIndex(fields=["search_vector"])]
  36. class IssueHash(models.Model):
  37. issue = models.ForeignKey(Issue, on_delete=models.CASCADE, related_name="hashes")
  38. # Redundant project allows for unique constraint
  39. project = models.ForeignKey(
  40. "projects.Project", on_delete=models.CASCADE, related_name="+"
  41. )
  42. value = models.UUIDField(db_index=True)
  43. class Meta:
  44. constraints = [
  45. models.UniqueConstraint(
  46. fields=["project", "value"], name="issue hash project"
  47. )
  48. ]
  49. class Comment(models.Model):
  50. created = models.DateTimeField(auto_now_add=True)
  51. issue = models.ForeignKey(Issue, on_delete=models.CASCADE, related_name="comments")
  52. user = models.ForeignKey(
  53. "users.User", null=True, on_delete=models.SET_NULL, related_name="+"
  54. )
  55. text = models.TextField(blank=True, null=True)
  56. class Meta:
  57. ordering = ("-created",)
  58. class IssueEvent(PostgresPartitionedModel, models.Model):
  59. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  60. issue = models.ForeignKey(Issue, on_delete=models.CASCADE)
  61. type = models.PositiveSmallIntegerField(default=0, choices=IssueEventType.choices)
  62. date_created = models.DateTimeField(
  63. auto_now_add=True, help_text="Time at which event happened"
  64. )
  65. date_received = models.DateTimeField(
  66. auto_now_add=True, help_text="Time at which GlitchTip accepted event"
  67. )
  68. data = models.JSONField()
  69. class PartitioningMeta:
  70. method = PostgresPartitioningMethod.RANGE
  71. key = ["date_received"]
  72. def __str__(self):
  73. return self.eventID
  74. @property
  75. def eventID(self):
  76. return self.id.hex