Browse Source

feat(discover): Add dataset to Discover Saved Queries (#71379)

This is meant to allow us to track whether a Discover Saved Query
queries the Errors or Transactions in CH. We want to do this so that
eventually any queries going to Transactions can be routed to the
Indexed spans dataset and Errors still go to Errors.
Shruthi 9 months ago
parent
commit
3075318901

+ 1 - 1
migrations_lockfile.txt

@@ -9,5 +9,5 @@ feedback: 0004_index_together
 hybridcloud: 0016_add_control_cacheversion
 nodestore: 0002_nodestore_no_dictfield
 replays: 0004_index_together
-sentry: 0723_project_template_models
+sentry: 0724_discover_saved_query_dataset
 social_auth: 0002_default_auto_field

+ 24 - 1
src/sentry/discover/models.py

@@ -8,8 +8,9 @@ from sentry import features
 from sentry.backup.scopes import RelocationScope
 from sentry.db.models import BaseManager, FlexibleForeignKey, Model, region_silo_model, sane_repr
 from sentry.db.models.fields import JSONField
-from sentry.db.models.fields.bounded import BoundedBigIntegerField
+from sentry.db.models.fields.bounded import BoundedBigIntegerField, BoundedPositiveIntegerField
 from sentry.db.models.fields.hybrid_cloud_foreign_key import HybridCloudForeignKey
+from sentry.models.dashboard_widget import TypesClass
 from sentry.models.projectteam import ProjectTeam
 from sentry.tasks.relay import schedule_invalidate_project_config
 
@@ -17,6 +18,25 @@ MAX_KEY_TRANSACTIONS = 10
 MAX_TEAM_KEY_TRANSACTIONS = 100
 
 
+class DiscoverSavedQueryTypes(TypesClass):
+    DISCOVER = 0
+    ERROR_EVENTS = 1
+    """
+     Error side of the split from Discover.
+    """
+    TRANSACTION_LIKE = 2
+    """
+    This targets transaction-like data from the split from discover.
+    """
+
+    TYPES = [
+        (DISCOVER, "discover"),
+        (ERROR_EVENTS, "error-events"),
+        (TRANSACTION_LIKE, "transaction-like"),
+    ]
+    TYPE_NAMES = [t[1] for t in TYPES]
+
+
 @region_silo_model
 class DiscoverSavedQueryProject(Model):
     __relocation_scope__ = RelocationScope.Excluded
@@ -49,6 +69,9 @@ class DiscoverSavedQuery(Model):
     visits = BoundedBigIntegerField(null=True, default=1)
     last_visited = models.DateTimeField(null=True, default=timezone.now)
     is_homepage = models.BooleanField(null=True, blank=True)
+    dataset = BoundedPositiveIntegerField(
+        choices=DiscoverSavedQueryTypes.as_choices(), default=DiscoverSavedQueryTypes.DISCOVER
+    )
 
     class Meta:
         app_label = "sentry"

+ 1 - 1
src/sentry/migrations/0522_migrate_discover_savedquery_worldmaps.py

@@ -4,13 +4,13 @@ from django.db import migrations
 from django.db.backends.base.schema import BaseDatabaseSchemaEditor
 from django.db.migrations.state import StateApps
 
-from sentry.discover.models import DiscoverSavedQuery
 from sentry.new_migrations.migrations import CheckedMigration
 
 
 def migrate_savedquery_worldmap_display_to_totalPeriod(
     apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
 ) -> None:
+    DiscoverSavedQuery = apps.get_model("sentry", "DiscoverSavedQuery")
     savedQueries = DiscoverSavedQuery.objects.filter(query__contains={"display": "worldmap"})
 
     for savedQuery in savedQueries:

+ 49 - 0
src/sentry/migrations/0724_discover_saved_query_dataset.py

@@ -0,0 +1,49 @@
+# Generated by Django 5.0.6 on 2024-05-23 14:12
+
+from django.db import migrations
+
+import sentry.db.models.fields.bounded
+from sentry.new_migrations.migrations import CheckedMigration
+
+
+class Migration(CheckedMigration):
+    # This flag is used to mark that a migration shouldn't be automatically run in production.
+    # This should only be used for operations where it's safe to run the migration after your
+    # code has deployed. So this should not be used for most operations that alter the schema
+    # of a table.
+    # Here are some things that make sense to mark as post deployment:
+    # - Large data migrations. Typically we want these to be run manually so that they can be
+    #   monitored and not block the deploy for a long period of time while they run.
+    # - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
+    #   run this outside deployments so that we don't block them. Note that while adding an index
+    #   is a schema change, it's completely safe to run the operation after the code has deployed.
+    # Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment
+
+    is_post_deployment = False
+
+    dependencies = [
+        ("sentry", "0723_project_template_models"),
+    ]
+
+    operations = [
+        migrations.SeparateDatabaseAndState(
+            database_operations=[
+                migrations.RunSQL(
+                    """
+                    ALTER TABLE "sentry_discoversavedquery" ADD COLUMN "dataset" integer NOT NULL DEFAULT 0;
+                    """,
+                    reverse_sql="""
+                    ALTER TABLE "sentry_discoversavedquery" DROP COLUMN "dataset";
+                    """,
+                    hints={"tables": ["sentry_discoversavedquery"]},
+                ),
+            ],
+            state_operations=[
+                migrations.AddField(
+                    model_name="discoversavedquery",
+                    name="dataset",
+                    field=sentry.db.models.fields.bounded.BoundedPositiveIntegerField(default=0),
+                ),
+            ],
+        )
+    ]