Skip to content

Commit

Permalink
ref(on-demand): Improve query builder integration (#54446)
Browse files Browse the repository at this point in the history
  • Loading branch information
iambriccardo authored Aug 10, 2023
1 parent 18553c0 commit f475e1b
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 110 deletions.
4 changes: 2 additions & 2 deletions src/sentry/incidents/serializers/alert_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.db import router, transaction
from django.utils import timezone
from rest_framework import serializers
from snuba_sdk import Column, Condition, Function, Limit, Op
from snuba_sdk import Column, Condition, Limit, Op

from sentry import features
from sentry.api.fields.actor import ActorField
Expand Down Expand Up @@ -303,7 +303,7 @@ def _validate_snql_query(self, data, entity_subscription, projects):
except (InvalidSearchQuery, ValueError) as e:
raise serializers.ValidationError(f"Invalid Query or Metric: {e}")

if not isinstance(query_builder.columns[0], Function):
if not query_builder.are_columns_resolved():
raise serializers.ValidationError(
"Invalid Metric: Please pass a valid function for aggregation"
)
Expand Down
5 changes: 5 additions & 0 deletions src/sentry/search/events/builder/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def __init__(
self.on_demand_metrics_enabled = on_demand_metrics_enabled
self.auto_fields = auto_fields
self.query = query
self.selected_columns = selected_columns
self.groupby_columns = groupby_columns
self.functions_acl = set() if functions_acl is None else functions_acl
self.equation_config = {} if equation_config is None else equation_config
Expand Down Expand Up @@ -309,12 +310,16 @@ def __init__(
orderby=orderby,
)

def are_columns_resolved(self) -> bool:
return self.columns and isinstance(self.columns[0], Function)

def get_default_converter(self) -> Callable[[event_search.SearchFilter], Optional[WhereType]]:
return self._default_filter_converter

def resolve_time_conditions(self) -> None:
if self.skip_time_conditions:
return

# start/end are required so that we can run a query in a reasonable amount of time
if self.params.start is None or self.params.end is None:
raise InvalidSearchQuery("Cannot query without a valid date range")
Expand Down
Loading

0 comments on commit f475e1b

Please sign in to comment.