Browse Source

chore(typing): Add typing to datasets/discover.py (#82763)

Tony Xiao 2 months ago
parent
commit
19efdaef35
3 changed files with 81 additions and 83 deletions
  1. 0 1
      pyproject.toml
  2. 80 81
      src/sentry/search/events/datasets/discover.py
  3. 1 1
      src/sentry/search/events/filter.py

+ 0 - 1
pyproject.toml

@@ -276,7 +276,6 @@ module = [
     "sentry.scim.endpoints.utils",
     "sentry.search.events.builder.errors",
     "sentry.search.events.builder.metrics",
-    "sentry.search.events.datasets.discover",
     "sentry.search.events.datasets.filter_aliases",
     "sentry.search.events.datasets.function_aliases",
     "sentry.search.events.datasets.metrics",

+ 80 - 81
src/sentry/search/events/datasets/discover.py

@@ -1113,98 +1113,97 @@ class DiscoverDatasetConfig(DatasetConfig):
 
     @cached_property
     def _resolve_project_threshold_config(self) -> SelectType:
-        org_id = (
-            self.builder.params.organization.id
-            if self.builder.params.organization is not None
-            else None
-        )
+        project_thresholds = {}
+        project_threshold_config_keys = []
+        project_threshold_config_values = []
+
+        project_threshold_override_config_keys = []
+        project_threshold_override_config_values = []
+
+        org_id = self.builder.params.organization_id
         project_ids = self.builder.params.project_ids
 
-        project_threshold_configs = (
-            ProjectTransactionThreshold.objects.filter(
-                organization_id=org_id,
-                project_id__in=project_ids,
+        if org_id is not None:
+            project_threshold_configs = (
+                ProjectTransactionThreshold.objects.filter(
+                    organization_id=org_id,
+                    project_id__in=project_ids,
+                )
+                .order_by("project_id")
+                .values_list("project_id", "threshold", "metric")
             )
-            .order_by("project_id")
-            .values_list("project_id", "threshold", "metric")
-        )
 
-        transaction_threshold_configs = (
-            ProjectTransactionThresholdOverride.objects.filter(
-                organization_id=org_id,
-                project_id__in=project_ids,
+            transaction_threshold_configs = (
+                ProjectTransactionThresholdOverride.objects.filter(
+                    organization_id=org_id,
+                    project_id__in=project_ids,
+                )
+                .order_by("project_id")
+                .values_list("transaction", "project_id", "threshold", "metric")
             )
-            .order_by("project_id")
-            .values_list("transaction", "project_id", "threshold", "metric")
-        )
-
-        num_project_thresholds = project_threshold_configs.count()
-        sentry_sdk.set_tag("project_threshold.count", num_project_thresholds)
-        sentry_sdk.set_tag(
-            "project_threshold.count.grouped",
-            format_grouped_length(num_project_thresholds, [10, 100, 250, 500]),
-        )
-
-        num_transaction_thresholds = transaction_threshold_configs.count()
-        sentry_sdk.set_tag("txn_threshold.count", num_transaction_thresholds)
-        sentry_sdk.set_tag(
-            "txn_threshold.count.grouped",
-            format_grouped_length(num_transaction_thresholds, [10, 100, 250, 500]),
-        )
 
-        if (
-            num_project_thresholds + num_transaction_thresholds
-            > MAX_QUERYABLE_TRANSACTION_THRESHOLDS
-        ):
-            raise InvalidSearchQuery(
-                f"Exceeded {MAX_QUERYABLE_TRANSACTION_THRESHOLDS} configured transaction thresholds limit, try with fewer Projects."
+            num_project_thresholds = project_threshold_configs.count()
+            sentry_sdk.set_tag("project_threshold.count", num_project_thresholds)
+            sentry_sdk.set_tag(
+                "project_threshold.count.grouped",
+                format_grouped_length(num_project_thresholds, [10, 100, 250, 500]),
             )
 
-        # Arrays need to have toUint64 casting because clickhouse will define the type as the narrowest possible type
-        # that can store listed argument types, which means the comparison will fail because of mismatched types
-        project_thresholds = {}
-        project_threshold_config_keys = []
-        project_threshold_config_values = []
-        for project_id, threshold, metric in project_threshold_configs:
-            metric = TRANSACTION_METRICS[metric]
-            if (
-                threshold == DEFAULT_PROJECT_THRESHOLD
-                and metric == DEFAULT_PROJECT_THRESHOLD_METRIC
-            ):
-                # small optimization, if the configuration is equal to the default,
-                # we can skip it in the final query
-                continue
-
-            project_thresholds[project_id] = (metric, threshold)
-            project_threshold_config_keys.append(Function("toUInt64", [project_id]))
-            project_threshold_config_values.append((metric, threshold))
+            num_transaction_thresholds = transaction_threshold_configs.count()
+            sentry_sdk.set_tag("txn_threshold.count", num_transaction_thresholds)
+            sentry_sdk.set_tag(
+                "txn_threshold.count.grouped",
+                format_grouped_length(num_transaction_thresholds, [10, 100, 250, 500]),
+            )
 
-        project_threshold_override_config_keys = []
-        project_threshold_override_config_values = []
-        for transaction, project_id, threshold, metric in transaction_threshold_configs:
-            metric = TRANSACTION_METRICS[metric]
             if (
-                project_id in project_thresholds
-                and threshold == project_thresholds[project_id][1]
-                and metric == project_thresholds[project_id][0]
+                num_project_thresholds + num_transaction_thresholds
+                > MAX_QUERYABLE_TRANSACTION_THRESHOLDS
             ):
-                # small optimization, if the configuration is equal to the project
-                # configs, we can skip it in the final query
-                continue
-
-            elif (
-                project_id not in project_thresholds
-                and threshold == DEFAULT_PROJECT_THRESHOLD
-                and metric == DEFAULT_PROJECT_THRESHOLD_METRIC
-            ):
-                # small optimization, if the configuration is equal to the default
-                # and no project configs were set, we can skip it in the final query
-                continue
+                raise InvalidSearchQuery(
+                    f"Exceeded {MAX_QUERYABLE_TRANSACTION_THRESHOLDS} configured transaction thresholds limit, try with fewer Projects."
+                )
 
-            project_threshold_override_config_keys.append(
-                (Function("toUInt64", [project_id]), transaction)
-            )
-            project_threshold_override_config_values.append((metric, threshold))
+            # Arrays need to have toUint64 casting because clickhouse will define the type as the narrowest possible type
+            # that can store listed argument types, which means the comparison will fail because of mismatched types
+            for project_id, threshold, metric in project_threshold_configs:
+                metric_name = TRANSACTION_METRICS[metric]
+                if (
+                    threshold == DEFAULT_PROJECT_THRESHOLD
+                    and metric_name == DEFAULT_PROJECT_THRESHOLD_METRIC
+                ):
+                    # small optimization, if the configuration is equal to the default,
+                    # we can skip it in the final query
+                    continue
+
+                project_thresholds[project_id] = (metric_name, threshold)
+                project_threshold_config_keys.append(Function("toUInt64", [project_id]))
+                project_threshold_config_values.append((metric_name, threshold))
+
+            for transaction, project_id, threshold, metric in transaction_threshold_configs:
+                metric_name = TRANSACTION_METRICS[metric]
+                if (
+                    project_id in project_thresholds
+                    and threshold == project_thresholds[project_id][1]
+                    and metric_name == project_thresholds[project_id][0]
+                ):
+                    # small optimization, if the configuration is equal to the project
+                    # configs, we can skip it in the final query
+                    continue
+
+                elif (
+                    project_id not in project_thresholds
+                    and threshold == DEFAULT_PROJECT_THRESHOLD
+                    and metric_name == DEFAULT_PROJECT_THRESHOLD_METRIC
+                ):
+                    # small optimization, if the configuration is equal to the default
+                    # and no project configs were set, we can skip it in the final query
+                    continue
+
+                project_threshold_override_config_keys.append(
+                    (Function("toUInt64", [project_id]), transaction)
+                )
+                project_threshold_override_config_values.append((metric_name, threshold))
 
         project_threshold_config_index: SelectType = Function(
             "indexOf",
@@ -1807,7 +1806,7 @@ class DiscoverDatasetConfig(DatasetConfig):
         value = to_list(search_filter.value.value)
         # `unknown` is a special value for when there is no issue associated with the event
         group_short_ids = [v for v in value if v and v != "unknown"]
-        general_group_filter_values = ["" for v in value if not v or v == "unknown"]
+        general_group_filter_values = [0 for v in value if not v or v == "unknown"]
 
         if group_short_ids and self.builder.params.organization is not None:
             try:

+ 1 - 1
src/sentry/search/events/filter.py

@@ -73,7 +73,7 @@ def translate_transaction_status(val: str) -> str:
     return SPAN_STATUS_NAME_TO_CODE[val]
 
 
-def to_list(value: list[str] | str) -> list[str]:
+def to_list[T](value: list[T] | T) -> list[T]:
     if isinstance(value, list):
         return value
     return [value]