|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from django.db import models |
| 6 | +from django.db.models import F, Q |
| 7 | + |
| 8 | +from sentry.backup.scopes import RelocationScope |
| 9 | +from sentry.db.models import FlexibleForeignKey, cell_silo_model, sane_repr |
| 10 | +from sentry.db.models.base import DefaultFieldsModel |
| 11 | +from sentry.db.models.fields.bounded import BoundedPositiveIntegerField |
| 12 | +from sentry.db.models.fields.hybrid_cloud_foreign_key import HybridCloudForeignKey |
| 13 | + |
| 14 | + |
| 15 | +class InvestigationCellKind(models.TextChoices): |
| 16 | + TEXT = "text", "Text" |
| 17 | + QUERY = "query", "Query" |
| 18 | + |
| 19 | + |
| 20 | +@cell_silo_model |
| 21 | +class InvestigationCell(DefaultFieldsModel): |
| 22 | + """A user-composed cell whose content may be produced by a Seer execution.""" |
| 23 | + |
| 24 | + __relocation_scope__ = RelocationScope.Excluded |
| 25 | + |
| 26 | + investigation = FlexibleForeignKey( |
| 27 | + "investigations.Investigation", on_delete=models.CASCADE, related_name="cells" |
| 28 | + ) |
| 29 | + created_by_id = HybridCloudForeignKey("sentry.User", null=True, on_delete="SET_NULL") |
| 30 | + last_edited_by_id = HybridCloudForeignKey("sentry.User", null=True, on_delete="SET_NULL") |
| 31 | + |
| 32 | + position = BoundedPositiveIntegerField() |
| 33 | + kind = models.CharField(max_length=32, choices=InvestigationCellKind.choices) |
| 34 | + title = models.CharField(max_length=255, default="", blank=True, db_default="") |
| 35 | + # The canonical, editable body rendered or evaluated by this cell. |
| 36 | + content = models.TextField(default="", blank=True, db_default="") |
| 37 | + # The latest prompt used to generate content. This remains editable so a |
| 38 | + # future execution can regenerate the cell. |
| 39 | + prompt = models.TextField(default="", blank=True, db_default="") |
| 40 | + # The unedited output of the latest generation. Human edits update content |
| 41 | + # without erasing the generated source. |
| 42 | + generated_content = models.TextField(default="", blank=True, db_default="") |
| 43 | + |
| 44 | + # Kind-specific execution and authoring behavior, such as automatic execution |
| 45 | + # or a dataset hint. Presentation-only state belongs in `display`. |
| 46 | + config: models.Field[dict[str, Any], dict[str, Any]] = models.JSONField( |
| 47 | + default=dict, db_default={} |
| 48 | + ) |
| 49 | + # Versioned presentation state, such as table/chart selection or whether a |
| 50 | + # prompt is collapsed. It must not affect how a cell executes. |
| 51 | + display: models.Field[dict[str, Any], dict[str, Any]] = models.JSONField( |
| 52 | + default=dict, db_default={} |
| 53 | + ) |
| 54 | + |
| 55 | + version = BoundedPositiveIntegerField(default=1, db_default=1) |
| 56 | + current_execution = FlexibleForeignKey( |
| 57 | + "investigations.InvestigationCellExecution", |
| 58 | + null=True, |
| 59 | + on_delete=models.SET_NULL, |
| 60 | + related_name="+", |
| 61 | + ) |
| 62 | + # The successful execution that produced the currently rendered text body. |
| 63 | + # This remains stable while a newer generation is pending or fails so the |
| 64 | + # existing Markdown keeps its original project-access requirements. |
| 65 | + content_execution = FlexibleForeignKey( |
| 66 | + "investigations.InvestigationCellExecution", |
| 67 | + null=True, |
| 68 | + on_delete=models.SET_NULL, |
| 69 | + related_name="+", |
| 70 | + ) |
| 71 | + # The latest successful execution that produced a query result. It remains |
| 72 | + # stable while a replacement run is pending, stopped, or fails. |
| 73 | + result_execution = FlexibleForeignKey( |
| 74 | + "investigations.InvestigationCellExecution", |
| 75 | + null=True, |
| 76 | + on_delete=models.SET_NULL, |
| 77 | + related_name="+", |
| 78 | + ) |
| 79 | + # Set when an input changes so the UI can distinguish a valid old output |
| 80 | + # from a current one before a replacement execution finishes. |
| 81 | + stale_at = models.DateTimeField(null=True) |
| 82 | + |
| 83 | + # Cells are hidden rather than hard-deleted so execution history remains |
| 84 | + # inspectable and stale references retain a stable target. |
| 85 | + deleted_at = models.DateTimeField(null=True) |
| 86 | + |
| 87 | + class Meta: |
| 88 | + app_label = "investigations" |
| 89 | + db_table = "investigations_investigationcell" |
| 90 | + indexes = [ |
| 91 | + models.Index(fields=["investigation", "deleted_at", "position"]), |
| 92 | + models.Index(fields=["investigation", "-date_updated"]), |
| 93 | + ] |
| 94 | + |
| 95 | + __repr__ = sane_repr("investigation_id", "kind", "position") |
| 96 | + |
| 97 | + |
| 98 | +@cell_silo_model |
| 99 | +class InvestigationCellDependency(DefaultFieldsModel): |
| 100 | + """A directed edge from a cell to one of its upstream dependencies.""" |
| 101 | + |
| 102 | + __relocation_scope__ = RelocationScope.Excluded |
| 103 | + |
| 104 | + cell = FlexibleForeignKey( |
| 105 | + "investigations.InvestigationCell", |
| 106 | + on_delete=models.CASCADE, |
| 107 | + related_name="dependency_links", |
| 108 | + ) |
| 109 | + depends_on = FlexibleForeignKey( |
| 110 | + "investigations.InvestigationCell", |
| 111 | + on_delete=models.CASCADE, |
| 112 | + related_name="dependent_links", |
| 113 | + ) |
| 114 | + |
| 115 | + class Meta: |
| 116 | + app_label = "investigations" |
| 117 | + db_table = "investigations_investigationcelldependency" |
| 118 | + constraints = [ |
| 119 | + models.UniqueConstraint( |
| 120 | + fields=["cell", "depends_on"], name="investigation_unique_cell_dependency" |
| 121 | + ), |
| 122 | + models.CheckConstraint( |
| 123 | + condition=~Q(cell=F("depends_on")), name="investigation_no_self_dependency" |
| 124 | + ), |
| 125 | + ] |
0 commit comments