|
| 1 | +from sentry.hybridcloud.models.outbox import outbox_context |
| 2 | +from sentry.models.dashboard_widget import ( |
| 3 | + DashboardWidgetDisplayTypes, |
| 4 | + DashboardWidgetTypes, |
| 5 | + DatasetSourcesTypes, |
| 6 | +) |
| 7 | +from sentry.models.organization import Organization |
| 8 | +from sentry.testutils.cases import SnubaTestCase, TestMigrations |
| 9 | +from sentry.testutils.helpers.datetime import before_now |
| 10 | +from sentry.utils.samples import load_data |
| 11 | + |
| 12 | + |
| 13 | +class SplitDiscoverDatasetDashboardsSelfHostedTest(TestMigrations, SnubaTestCase): |
| 14 | + migrate_from = "0912_make_organizationmemberteam_replica_is_active_true" |
| 15 | + migrate_to = "0913_split_discover_dataset_dashboards_self_hosted" |
| 16 | + |
| 17 | + def setup_before_migration(self, apps): |
| 18 | + User = apps.get_model("sentry", "User") |
| 19 | + Dashboard = apps.get_model("sentry", "Dashboard") |
| 20 | + DashboardWidget = apps.get_model("sentry", "DashboardWidget") |
| 21 | + DashboardWidgetQuery = apps.get_model("sentry", "DashboardWidgetQuery") |
| 22 | + |
| 23 | + with outbox_context(flush=False): |
| 24 | + self.organization = Organization.objects.create(name="test", slug="test") |
| 25 | + self.user = User.objects.create(email="test@test.com", is_superuser=False) |
| 26 | + self.project = self.create_project( |
| 27 | + name="test_project", slug="test_project", organization=self.organization |
| 28 | + ) |
| 29 | + self.environment = self.create_environment( |
| 30 | + name="test_environment", project=self.project, organization=self.organization |
| 31 | + ) |
| 32 | + |
| 33 | + self.dashboard = Dashboard.objects.create( |
| 34 | + title="test", |
| 35 | + organization_id=self.organization.id, |
| 36 | + created_by_id=self.user.id, |
| 37 | + ) |
| 38 | + |
| 39 | + self.discover_error_widget = DashboardWidget.objects.create( |
| 40 | + dashboard_id=self.dashboard.id, |
| 41 | + title="test discover widget", |
| 42 | + widget_type=DashboardWidgetTypes.DISCOVER, |
| 43 | + dataset_source=DatasetSourcesTypes.UNKNOWN.value, |
| 44 | + display_type=DashboardWidgetDisplayTypes.LINE_CHART, |
| 45 | + interval="1d", |
| 46 | + order=0, |
| 47 | + ) |
| 48 | + |
| 49 | + self.discover_error_widget_query = DashboardWidgetQuery.objects.create( |
| 50 | + widget_id=self.discover_error_widget.id, |
| 51 | + name="test discover widget query", |
| 52 | + fields=["count()"], |
| 53 | + aggregates=["count()"], |
| 54 | + columns=[], |
| 55 | + conditions="environment:foo", |
| 56 | + orderby=["-count()"], |
| 57 | + order=0, |
| 58 | + ) |
| 59 | + |
| 60 | + self.migrated_discover_widget = DashboardWidget.objects.create( |
| 61 | + dashboard_id=self.dashboard.id, |
| 62 | + title="test migrated discover widget", |
| 63 | + widget_type=DashboardWidgetTypes.DISCOVER, |
| 64 | + dataset_source=DatasetSourcesTypes.UNKNOWN.value, |
| 65 | + discover_widget_split=DashboardWidgetTypes.TRANSACTION_LIKE, |
| 66 | + display_type=DashboardWidgetDisplayTypes.LINE_CHART, |
| 67 | + interval="1d", |
| 68 | + order=1, |
| 69 | + ) |
| 70 | + |
| 71 | + self.migrated_discover_widget_query = DashboardWidgetQuery.objects.create( |
| 72 | + widget_id=self.migrated_discover_widget.id, |
| 73 | + name="test migrated discover widget query", |
| 74 | + fields=["count()"], |
| 75 | + aggregates=["count()"], |
| 76 | + columns=[], |
| 77 | + conditions="environment:foo", |
| 78 | + orderby=["-count()"], |
| 79 | + order=1, |
| 80 | + ) |
| 81 | + |
| 82 | + self.discover_transaction_widget = DashboardWidget.objects.create( |
| 83 | + dashboard_id=self.dashboard.id, |
| 84 | + title="test discover transaction widget", |
| 85 | + widget_type=DashboardWidgetTypes.DISCOVER, |
| 86 | + dataset_source=DatasetSourcesTypes.UNKNOWN.value, |
| 87 | + display_type=DashboardWidgetDisplayTypes.LINE_CHART, |
| 88 | + interval="1d", |
| 89 | + order=2, |
| 90 | + ) |
| 91 | + |
| 92 | + self.discover_transaction_widget_query = DashboardWidgetQuery.objects.create( |
| 93 | + widget_id=self.discover_transaction_widget.id, |
| 94 | + name="test discover transaction widget query", |
| 95 | + fields=["count()", "transaction.duration"], |
| 96 | + aggregates=["count()"], |
| 97 | + columns=[], |
| 98 | + conditions="environment:foo", |
| 99 | + orderby=["-count()"], |
| 100 | + order=2, |
| 101 | + ) |
| 102 | + |
| 103 | + self.discover_ambiguous_widget = DashboardWidget.objects.create( |
| 104 | + dashboard_id=self.dashboard.id, |
| 105 | + title="test discover ambiguous widget", |
| 106 | + widget_type=DashboardWidgetTypes.DISCOVER, |
| 107 | + dataset_source=DatasetSourcesTypes.UNKNOWN.value, |
| 108 | + display_type=DashboardWidgetDisplayTypes.LINE_CHART, |
| 109 | + interval="1d", |
| 110 | + order=3, |
| 111 | + ) |
| 112 | + |
| 113 | + self.discover_ambiguous_widget_query = DashboardWidgetQuery.objects.create( |
| 114 | + widget_id=self.discover_ambiguous_widget.id, |
| 115 | + name="test discover ambiguous widget query", |
| 116 | + fields=["count()", "transaction"], |
| 117 | + aggregates=["count()"], |
| 118 | + columns=[], |
| 119 | + conditions="environment:test_environment", |
| 120 | + orderby=["-count()"], |
| 121 | + order=3, |
| 122 | + ) |
| 123 | + |
| 124 | + # Now store test data that should only affect the ambiguous widget |
| 125 | + self.nine_mins_ago = before_now(minutes=9) |
| 126 | + self.ten_mins_ago = before_now(minutes=10) |
| 127 | + |
| 128 | + data = load_data("transaction", timestamp=self.ten_mins_ago) |
| 129 | + data["transaction"] = "/to_other/" |
| 130 | + data["environment"] = self.environment.name |
| 131 | + data["transaction.duration"] = 1000 |
| 132 | + self.store_event(data, project_id=self.project.id, assert_no_errors=False) |
| 133 | + |
| 134 | + data = load_data("transaction", timestamp=self.ten_mins_ago) |
| 135 | + data["transaction"] = "/to_other/2" |
| 136 | + data["environment"] = self.environment.name |
| 137 | + data["transaction.duration"] = 2000 |
| 138 | + self.store_event(data, project_id=self.project.id, assert_no_errors=False) |
| 139 | + |
| 140 | + def test(self): |
| 141 | + self.discover_error_widget.refresh_from_db() |
| 142 | + self.migrated_discover_widget.refresh_from_db() |
| 143 | + self.discover_transaction_widget.refresh_from_db() |
| 144 | + self.discover_ambiguous_widget.refresh_from_db() |
| 145 | + |
| 146 | + assert self.discover_error_widget.discover_widget_split == DashboardWidgetTypes.ERROR_EVENTS |
| 147 | + assert ( |
| 148 | + self.migrated_discover_widget.discover_widget_split |
| 149 | + == DashboardWidgetTypes.TRANSACTION_LIKE |
| 150 | + ) |
| 151 | + assert ( |
| 152 | + self.discover_transaction_widget.discover_widget_split |
| 153 | + == DashboardWidgetTypes.TRANSACTION_LIKE |
| 154 | + ) |
| 155 | + assert ( |
| 156 | + self.discover_ambiguous_widget.discover_widget_split |
| 157 | + == DashboardWidgetTypes.TRANSACTION_LIKE |
| 158 | + ) |
0 commit comments