def test_sub_query(self) -> None: inner_query = (Query("discover", Entity("discover_events")).set_select( [Function("count", [], "count") ]).set_groupby([Column("project_id"), Column("tags[custom_tag]")]).set_where([ Condition(Column("type"), Op.NEQ, "transaction"), Condition(Column("project_id"), Op.EQ, self.project_id), Condition(Column("timestamp"), Op.GTE, self.base_time), Condition(Column("timestamp"), Op.LT, self.next_time), ])) query = (Query("discover", inner_query).set_select( [Function("avg", [Column("count")], "avg_count")]).set_orderby([ OrderBy(Function("avg", [Column("count")], "avg_count"), Direction.ASC) ]).set_limit(1000)) response = self.post("/discover/snql", data=query.snuba()) data = json.loads(response.data) assert response.status_code == 200, data assert data["data"] == [{"avg_count": 1.0}]
def run_metrics_query( *, entity_key: EntityKey, select: List[Column], where: List[Condition], groupby: List[Column], projects: Sequence[Project], org_id: int, referrer: str, ) -> Mapping[str, Any]: # Round timestamp to minute to get cache efficiency: now = datetime.now().replace(second=0, microsecond=0) query = Query( dataset=Dataset.Metrics.value, match=Entity(entity_key.value), select=select, groupby=groupby, where=[ Condition(Column("org_id"), Op.EQ, org_id), Condition(Column("project_id"), Op.IN, [p.id for p in projects]), Condition(Column(TS_COL_QUERY), Op.GTE, now - timedelta(hours=24)), Condition(Column(TS_COL_QUERY), Op.LT, now), ] + where, granularity=Granularity(GRANULARITY), ) result = raw_snql_query(query, referrer, use_cache=True) return result["data"]
def limiting_conditions(self) -> Optional[List[Condition]]: if not self.initialized or not self._groups: # First query may run without limiting conditions # When there are no groups there is nothing to limit return None group_columns = [ col for col in self._groupby if col not in self.skip_columns ] if not group_columns: return [] # Create conditions from the groups in group by group_values = [ Function("tuple", [row[column.name] for column in group_columns]) for row in self._groups ] return [ # E.g. (release, environment) IN [(1, 2), (3, 4), ...] Condition(Function("tuple", group_columns), Op.IN, group_values) ] + [ # These conditions are redundant but might lead to better query performance # Eg. [release IN [1, 3]], [environment IN [2, 4]] Condition(column, Op.IN, [row[column.name] for row in self._groups]) for column in group_columns ]
def _get_data(entity_key: EntityKey, metric_name: str) -> Tuple[int, int]: total = 0 crashed = 0 metric_id = try_get_string_index(org_id, metric_name) if metric_id is not None: where = conditions + [ Condition(Column("metric_id"), Op.EQ, metric_id), Condition(Column("timestamp"), Op.LT, end), ] data = raw_snql_query( Query( dataset=Dataset.Metrics.value, match=Entity(entity_key.value), select=[Column("value")], where=where, groupby=[Column(status_key)], ), referrer= "release_health.metrics.crash-free-breakdown.session", )["data"] for row in data: if row[status_key] == status_init: total = int(row["value"]) elif row[status_key] == status_crashed: crashed = int(row["value"]) return total, crashed
def test_join_query(self) -> None: ev = Entity("events", "ev") gm = Entity("groupedmessage", "gm") join = Join([Relationship(ev, "grouped", gm)]) query = (Query("discover", join).set_select([ Column("group_id", ev), Column("status", gm), Function("avg", [Column("retention_days", ev)], "avg"), ]).set_groupby([Column("group_id", ev), Column("status", gm)]).set_where([ Condition(Column("project_id", ev), Op.EQ, self.project_id), Condition(Column("project_id", gm), Op.EQ, self.project_id), Condition(Column("timestamp", ev), Op.GTE, self.base_time), Condition(Column("timestamp", ev), Op.LT, self.next_time), ])) response = self.post("/discover/snql", data=query.snuba()) data = json.loads(response.data) assert response.status_code == 200 assert data["data"] == []
def monitor_release_adoption(**kwargs): metrics.incr("sentry.tasks.monitor_release_adoption.start", sample_rate=1.0) # 1. Query snuba for all project ids that have sessions. with metrics.timer( "sentry.tasks.monitor_release_adoption.aggregate_projects.loop", sample_rate=1.0 ): aggregated_projects = defaultdict(list) start_time = time.time() offset = 0 while (time.time() - start_time) < MAX_SECONDS: query = ( Query( dataset="sessions", match=Entity("org_sessions"), select=[ Column("org_id"), Column("project_id"), ], groupby=[Column("org_id"), Column("project_id")], where=[ Condition( Column("started"), Op.GTE, datetime.utcnow() - timedelta(hours=6) ), Condition(Column("started"), Op.LT, datetime.utcnow()), ], granularity=Granularity(3600), orderby=[ OrderBy(Column("org_id"), Direction.ASC), OrderBy(Column("project_id"), Direction.ASC), ], ) .set_limit(CHUNK_SIZE + 1) .set_offset(offset) ) data = snuba.raw_snql_query(query, referrer="tasks.monitor_release_adoption")["data"] count = len(data) more_results = count > CHUNK_SIZE offset += CHUNK_SIZE if more_results: data = data[:-1] for row in data: aggregated_projects[row["org_id"]].append(row["project_id"]) if not more_results: break else: logger.info( "monitor_release_adoption.loop_timeout", sample_rate=1.0, extra={"offset": offset}, ) with metrics.timer( "sentry.tasks.monitor_release_adoption.process_projects_with_sessions", sample_rate=1.0 ): for org_id in aggregated_projects: process_projects_with_sessions.delay(org_id, aggregated_projects[org_id])
def test_invalid_time_conditions(self) -> None: query = (Query("events", Entity("events")).set_select( [Function("count", [], "count")]).set_where([ Condition(Column("project_id"), Op.EQ, self.project_id), Condition(Column("timestamp"), Op.GTE, self.next_time), Condition(Column("timestamp"), Op.LT, self.base_time), ])) response = self.post("/events/snql", data=query.snuba()) resp = json.loads(response.data) assert response.status_code == 400, resp
def test_escape_edge_cases(self) -> None: query = (Query("events", Entity("events")).set_select( [Function("count", [], "times_seen")]).set_where([ Condition(Column("project_id"), Op.EQ, self.project_id), Condition(Column("timestamp"), Op.GTE, self.base_time), Condition(Column("timestamp"), Op.LT, self.next_time), Condition(Column("environment"), Op.EQ, "\\' \n \\n \\"), ])) response = self.post("/events/snql", data=query.snuba()) data = json.loads(response.data) assert response.status_code == 200, data
def get_changed_project_release_model_adoptions( self, project_ids: Sequence[ProjectId], ) -> Sequence[ProjectRelease]: now = datetime.now(pytz.utc) start = now - timedelta(days=3) projects_ids = list(project_ids) if len(projects_ids) == 0: return [] org_id = self._get_org_id(project_ids) release_column_name = tag_key(org_id, "release") query_cols = [Column("project_id"), Column(release_column_name)] group_by = query_cols where_clause = [ Condition(Column("org_id"), Op.EQ, org_id), Condition(Column("project_id"), Op.IN, project_ids), Condition(Column("metric_id"), Op.EQ, metric_id(org_id, "session")), Condition(Column("timestamp"), Op.GTE, start), Condition(Column("timestamp"), Op.LT, now), ] query = Query( dataset=Dataset.Metrics.value, match=Entity("metrics_counters"), select=query_cols, where=where_clause, groupby=group_by, ) result = raw_snql_query( query, referrer= "release_health.metrics.get_changed_project_release_model_adoptions", use_cache=False, ) def extract_row_info( row: Mapping[str, Union[OrganizationId, str]]) -> ProjectRelease: return row.get("project_id"), reverse_tag_value( org_id, row.get(release_column_name)) # type: ignore return [extract_row_info(row) for row in result["data"]]
def resolve_tags(input_: Any) -> Any: """Translate tags in snuba condition This assumes that all strings are either tag names or tag values, so do not pass Column("metric_id") or Column("project_id") into this function. """ if isinstance(input_, list): return [resolve_tags(item) for item in input_] if isinstance(input_, Function): if input_.function == "ifNull": # This was wrapped automatically by QueryBuilder, remove wrapper return resolve_tags(input_.parameters[0]) return Function( function=input_.function, parameters=input_.parameters and [resolve_tags(item) for item in input_.parameters], ) if isinstance(input_, Condition): return Condition(lhs=resolve_tags(input_.lhs), op=input_.op, rhs=resolve_tags(input_.rhs)) if isinstance(input_, BooleanCondition): return input_.__class__(conditions=[resolve_tags(item) for item in input_.conditions]) if isinstance(input_, Column): # HACK: Some tags already take the form "tags[...]" in discover, take that into account: if input_.subscriptable == "tags": name = input_.key else: name = input_.name return Column(name=resolve_tag_key(name)) if isinstance(input_, str): return resolve_weak(input_) return input_
def get_single_metric(self, metric_name: str) -> MetricMetaWithTagKeys: """Get metadata for a single metric, without tag values""" metric_id = indexer.resolve(metric_name) if metric_id is None: raise InvalidParams for metric_type in ("counter", "set", "distribution"): # TODO: What if metric_id exists for multiple types / units? entity_key = METRIC_TYPE_TO_ENTITY[metric_type] data = self._get_data( entity_key=entity_key, select=[Column("metric_id"), Column("tags.key")], where=[Condition(Column("metric_id"), Op.EQ, metric_id)], groupby=[Column("metric_id"), Column("tags.key")], referrer="snuba.metrics.meta.get_single_metric", ) if data: tag_ids = {tag_id for row in data for tag_id in row["tags.key"]} return { "name": metric_name, "type": metric_type, "operations": _AVAILABLE_OPERATIONS[entity_key.value], "tags": sorted( ({"key": reverse_resolve(tag_id)} for tag_id in tag_ids), key=itemgetter("key"), ), "unit": None, } raise InvalidParams
def _build_filter( self, query_definition: QueryDefinition) -> Optional[BooleanCondition]: filter_ = query_definition.parsed_query if filter_ is None: return None def to_int(string): try: return indexer.resolve(self._project.organization_id, string) except KeyError: return None return self._build_logical( Or, [ self._build_logical( And, [ Condition( Column(f"tags[{to_int(tag)}]"), Op.EQ, to_int(value), ) for tag, value in or_operand["and"] ], ) for or_operand in filter_["or"] ], )
def _build_where( self, query_definition: QueryDefinition ) -> List[Union[BooleanCondition, Condition]]: assert self._projects org_id = self._projects[0].organization_id where: List[Union[BooleanCondition, Condition]] = [ Condition(Column("org_id"), Op.EQ, org_id), Condition(Column("project_id"), Op.IN, [p.id for p in self._projects]), Condition(Column(TS_COL_QUERY), Op.GTE, query_definition.start), Condition(Column(TS_COL_QUERY), Op.LT, query_definition.end), ] filter_ = resolve_tags(query_definition.parsed_query) if filter_: where.extend(filter_) return where
def _translate_conditions(org_id: int, input_: Any) -> Any: if isinstance(input_, Column): # The only filterable tag keys are release and environment. assert input_.name in ("release", "environment") # It greatly simplifies code if we just assume that they exist. # Alternative would be: # * if tag key or value does not exist in AND-clause, return no data # * if tag key or value does not exist in OR-clause, remove condition return Column(resolve_tag_key(input_.name)) if isinstance(input_, str): # Assuming this is the right-hand side, we need to fetch a tag value. # It's OK if the tag value resolves to None, the snuba query will then # return no results, as is intended behavior return indexer.resolve(input_) if isinstance(input_, Function): return Function(function=input_.function, parameters=_translate_conditions( org_id, input_.parameters)) if isinstance(input_, Condition): return Condition( lhs=_translate_conditions(org_id, input_.lhs), op=input_.op, rhs=_translate_conditions(org_id, input_.rhs), ) if isinstance(input_, (int, float)): return input_ assert isinstance(input_, (tuple, list)), input_ return [_translate_conditions(org_id, item) for item in input_]
def team_key_transaction_filter(builder: QueryBuilder, search_filter: SearchFilter) -> WhereType: value = search_filter.value.value key_transaction_expr = builder.resolve_field_alias(constants.TEAM_KEY_TRANSACTION_ALIAS) if search_filter.value.raw_value == "": return Condition( key_transaction_expr, Op.NEQ if search_filter.operator == "!=" else Op.EQ, 0 ) if value in ("1", 1): return Condition(key_transaction_expr, Op.EQ, 1) if value in ("0", 0): return Condition(key_transaction_expr, Op.EQ, 0) raise InvalidSearchQuery( "Invalid value for key_transaction condition. Accepted values are 1, 0" )
def test_attribution_tags(self) -> None: query = (Query("events", Entity("events")).set_select( [Function("count", [], "count")]).set_where([ Condition(Column("project_id"), Op.EQ, self.project_id), Condition(Column("timestamp"), Op.GTE, self.base_time), Condition(Column("timestamp"), Op.LT, self.next_time), ]).set_team("sns").set_feature("test")) response = self.post("/events/snql", data=query.snuba()) resp = json.loads(response.data) assert response.status_code == 200, resp metric_calls = get_recorded_metric_calls("increment", "snuba.attribution.log") assert metric_calls is not None assert len(metric_calls) == 1 assert metric_calls[0].value > 0 assert metric_calls[0].tags["app_id"] == "default"
def _get_snuba_query( org_id: int, query: QueryDefinition, entity_key: EntityKey, metric_id: int, columns: Sequence[str], series: bool, extra_conditions: List[Condition], remove_groupby: Set[Column], ) -> Query: """Build the snuba query""" conditions = [ Condition(Column("org_id"), Op.EQ, org_id), Condition(Column("project_id"), Op.IN, query.filter_keys["project_id"]), Condition(Column("metric_id"), Op.EQ, metric_id), Condition(Column(TS_COL_QUERY), Op.GTE, query.start), Condition(Column(TS_COL_QUERY), Op.LT, query.end), ] conditions += _get_filter_conditions(org_id, query.conditions) conditions += extra_conditions groupby_tags = [field for field in query.raw_groupby if field != "project"] tag_keys = {field: _resolve(field) for field in groupby_tags} groupby = { field: Column(f"tags[{tag_id}]") for field, tag_id in tag_keys.items() if tag_id is not None # exclude unresolved keys from groupby } if "project" in query.raw_groupby: groupby["project"] = Column("project_id") full_groupby = set(groupby.values()) - remove_groupby if series: full_groupby.add(Column(TS_COL_GROUP)) return Query( dataset=Dataset.Metrics.value, match=Entity(entity_key.value), select=[Column(column) for column in columns], groupby=list(full_groupby), where=conditions, granularity=Granularity(query.rollup), )
def _build_where( self, query_definition: QueryDefinition ) -> List[Union[BooleanCondition, Condition]]: where: List[Union[BooleanCondition, Condition]] = [ Condition(Column("org_id"), Op.EQ, self._project.organization_id), Condition(Column("project_id"), Op.EQ, self._project.id), Condition( Column("metric_id"), Op.IN, [indexer.resolve(name) for _, name in query_definition.fields.values()], ), Condition(Column(TS_COL_QUERY), Op.GTE, query_definition.start), Condition(Column(TS_COL_QUERY), Op.LT, query_definition.end), ] filter_ = self._build_filter(query_definition) if filter_: where.append(filter_) return where
def _get_crash_free_rate_data( org_id: int, project_ids: Sequence[int], start: datetime, end: datetime, rollup: int, ) -> Dict[int, Dict[str, float]]: data: Dict[int, Dict[str, float]] = {} session_status = tag_key(org_id, "session.status") count_query = Query( dataset=Dataset.Metrics.value, match=Entity(EntityKey.MetricsCounters.value), select=[Column("value")], where=[ Condition(Column("org_id"), Op.EQ, org_id), Condition(Column("project_id"), Op.IN, project_ids), Condition(Column("metric_id"), Op.EQ, metric_id(org_id, "session")), Condition(Column("timestamp"), Op.GTE, start), Condition(Column("timestamp"), Op.LT, end), ], groupby=[ Column("project_id"), Column(session_status), ], granularity=Granularity(rollup), ) count_data = raw_snql_query( count_query, referrer="release_health.metrics.get_crash_free_data", use_cache=False)["data"] for row in count_data: project_data = data.setdefault(row["project_id"], {}) tag_value = reverse_tag_value(org_id, row[session_status]) project_data[tag_value] = row["value"] return data
def test_sessions_query(self) -> None: query = (Query("sessions", Entity("sessions")).set_select( [Column("project_id"), Column("release")]).set_groupby( [Column("project_id"), Column("release")]).set_where([ Condition(Column("project_id"), Op.IN, [self.project_id]), Condition(Column("org_id"), Op.EQ, self.org_id), Condition( Column("started"), Op.GTE, datetime(2021, 1, 1, 17, 5, 59, 554860), ), Condition(Column("started"), Op.LT, datetime(2022, 1, 1, 17, 6, 0, 554981)), ]).set_orderby([OrderBy(Column("sessions"), Direction.DESC)]).set_limit(100)) response = self.post("/sessions/snql", data=query.snuba()) data = json.loads(response.data) assert response.status_code == 200 assert data["data"] == []
def test_tags_in_groupby(self) -> None: query = (Query("events", Entity("events")).set_select([ Function("count", [], "times_seen"), Function("min", [Column("timestamp")], "first_seen"), Function("max", [Column("timestamp")], "last_seen"), ]).set_groupby([Column("tags[k8s-app]")]).set_where([ Condition(Column("project_id"), Op.EQ, self.project_id), Condition(Column("timestamp"), Op.GTE, self.base_time), Condition(Column("timestamp"), Op.LT, self.next_time), Condition(Column("tags[k8s-app]"), Op.NEQ, ""), Condition(Column("type"), Op.NEQ, "transaction"), ]).set_orderby([ OrderBy( Function("max", [Column("timestamp")], "last_seen"), Direction.DESC, ) ]).set_limit(1000)) response = self.post("/events/snql", data=query.snuba()) data = json.loads(response.data) assert response.status_code == 200, data
def expected_query(match, select, extra_groupby, metric_name): function, column, alias = select return Query( dataset="metrics", match=Entity(match), select=[ Function( OP_TO_SNUBA_FUNCTION[match][alias], [ Column("value"), Function( "equals", [Column("metric_id"), resolve_weak(metric_name)]), ], alias=f"{alias}({metric_name})", ) ], groupby=[Column("tags[8]"), Column("tags[2]")] + extra_groupby, where=[ Condition(Column("org_id"), Op.EQ, 1), Condition(Column("project_id"), Op.IN, [1]), Condition(Column("timestamp"), Op.GTE, datetime(2021, 5, 28, 0, tzinfo=pytz.utc)), Condition(Column("timestamp"), Op.LT, datetime(2021, 8, 26, 0, tzinfo=pytz.utc)), Condition(Column("tags[6]"), Op.IN, [10]), Condition(Column("metric_id"), Op.IN, [resolve_weak(metric_name)]), ], limit=Limit(MAX_POINTS), offset=Offset(0), granularity=Granularity(query_definition.rollup), )
def expected_query(match, select, extra_groupby): function, column, alias = select return Query( dataset="metrics", match=Entity(match), select=[Function(function, [Column(column)], alias)], groupby=[ Column("metric_id"), Column("tags[8]"), Column("tags[2]") ] + extra_groupby, where=[ Condition(Column("org_id"), Op.EQ, 1), Condition(Column("project_id"), Op.IN, [1]), Condition(Column("metric_id"), Op.IN, [9, 11, 7]), Condition(Column("timestamp"), Op.GTE, datetime(2021, 5, 28, 0, tzinfo=pytz.utc)), Condition(Column("timestamp"), Op.LT, datetime(2021, 8, 26, 0, tzinfo=pytz.utc)), Condition(Column("tags[6]"), Op.IN, [10]), ], limit=Limit(MAX_POINTS), offset=Offset(0), granularity=Granularity(query_definition.rollup), )
def test_simple_query(self) -> None: query = (Query("discover", Entity("discover_events")).set_select( [Function("count", [], "count")]).set_groupby( [Column("project_id"), Column("tags[custom_tag]")]).set_where([ Condition(Column("type"), Op.NEQ, "transaction"), Condition(Column("project_id"), Op.EQ, self.project_id), Condition(Column("timestamp"), Op.GTE, self.base_time), Condition(Column("timestamp"), Op.LT, self.next_time), ]).set_orderby([ OrderBy(Function("count", [], "count"), Direction.ASC) ]).set_limit(1000).set_consistent(True).set_debug(True)) response = self.post("/discover/snql", data=query.snuba()) data = json.loads(response.data) assert response.status_code == 200, data assert data["stats"]["consistent"] assert data["data"] == [{ "count": 1, "tags[custom_tag]": "custom_value", "project_id": self.project_id, }]
def test_arrayjoin(self) -> None: query = (Query("events", Entity("events")).set_select([ Function("count", [], "times_seen"), Function("min", [Column("timestamp")], "first_seen"), Function("max", [Column("timestamp")], "last_seen"), ]).set_groupby([Column("exception_frames.filename")]).set_array_join( [Column("exception_frames.filename")]).set_where([ Condition(Column("exception_frames.filename"), Op.LIKE, "%.java"), Condition(Column("project_id"), Op.EQ, self.project_id), Condition(Column("timestamp"), Op.GTE, self.base_time), Condition(Column("timestamp"), Op.LT, self.next_time), ]).set_orderby([ OrderBy( Function("max", [Column("timestamp")], "last_seen"), Direction.DESC, ) ]).set_limit(1000)) response = self.post("/events/snql", data=query.snuba()) data = json.loads(response.data) assert response.status_code == 200, data assert len(data["data"]) == 6
def _get_metrics_filter( self, metric_names: Optional[Sequence[str]] ) -> Optional[List[Condition]]: """Add a condition to filter by metrics. Return None if a name cannot be resolved.""" where = [] if metric_names is not None: metric_ids = [] for name in metric_names: resolved = indexer.resolve(name) if resolved is None: # We are looking for tags that appear in all given metrics. # A tag cannot appear in a metric if the metric is not even indexed. return None metric_ids.append(resolved) where.append(Condition(Column("metric_id"), Op.IN, metric_ids)) return where
def get_single_metric_info(projects: Sequence[Project], metric_name: str) -> MetricMetaWithTagKeys: assert projects metric_id = indexer.resolve(metric_name) if metric_id is None: raise InvalidParams for metric_type in ("counter", "set", "distribution"): # TODO: What if metric_id exists for multiple types / units? entity_key = METRIC_TYPE_TO_ENTITY[metric_type] data = run_metrics_query( entity_key=entity_key, select=[Column("metric_id"), Column("tags.key")], where=[Condition(Column("metric_id"), Op.EQ, metric_id)], groupby=[Column("metric_id"), Column("tags.key")], referrer="snuba.metrics.meta.get_single_metric", projects=projects, org_id=projects[0].organization_id, ) if data: tag_ids = {tag_id for row in data for tag_id in row["tags.key"]} return { "name": metric_name, "type": metric_type, "operations": AVAILABLE_OPERATIONS[entity_key.value], "tags": sorted( ({ "key": reverse_resolve(tag_id) } for tag_id in tag_ids), key=itemgetter("key"), ), "unit": None, } raise InvalidParams(f"Raw metric {metric_name} does not exit")
def _count_users(total: bool, referrer: str) -> Dict[Any, int]: query = Query( dataset=Dataset.Metrics.value, match=Entity(EntityKey.MetricsSets.value), select=[Column("value")], where=_get_common_where(total) + [ Condition(Column("metric_id"), Op.EQ, metric_id(org_id, "user")), ], groupby=_get_common_groupby(total), ) return _convert_results( raw_snql_query( query, referrer=referrer, use_cache=False, )["data"], total=total, )
def test_build_snuba_query_orderby(mock_now, mock_now2, mock_indexer): mock_indexer.resolve = MockIndexer().resolve query_params = MultiValueDict({ "query": ["release:staging" ], # weird release but we need a string exising in mock indexer "groupBy": ["session.status", "environment"], "field": [ "sum(sentry.sessions.session)", ], "orderBy": ["-sum(sentry.sessions.session)"], "limit": [3], }) query_definition = QueryDefinition(query_params) snuba_queries = SnubaQueryBuilder([PseudoProject(1, 1)], query_definition).get_snuba_queries() counter_queries = snuba_queries.pop("metrics_counters") assert not snuba_queries assert counter_queries["series"] is None # No series because of orderBy assert counter_queries["totals"] == Query( dataset="metrics", match=Entity("metrics_counters"), select=[Function("sum", [Column("value")], "value")], groupby=[ Column("metric_id"), Column("tags[8]"), Column("tags[2]"), ], where=[ Condition(Column("org_id"), Op.EQ, 1), Condition(Column("project_id"), Op.IN, [1]), Condition(Column("metric_id"), Op.IN, [9]), Condition(Column("timestamp"), Op.GTE, datetime(2021, 5, 28, 0, tzinfo=pytz.utc)), Condition(Column("timestamp"), Op.LT, datetime(2021, 8, 26, 0, tzinfo=pytz.utc)), Condition(Column("tags[6]", entity=None), Op.IN, [10]), ], orderby=[OrderBy(Column("value"), Direction.DESC)], limit=Limit(3), offset=Offset(0), granularity=Granularity(query_definition.rollup), )
def check_releases_have_health_data( self, organization_id: OrganizationId, project_ids: Sequence[ProjectId], release_versions: Sequence[ReleaseName], start: datetime, end: datetime, ) -> Set[ReleaseName]: release_column_name = tag_key(organization_id, "release") releases_ids = [ release_id for release_id in [ try_get_string_index(organization_id, release) for release in release_versions ] if release_id is not None ] query = Query( dataset=Dataset.Metrics.value, match=Entity("metrics_counters"), select=[Column(release_column_name)], where=[ Condition(Column("org_id"), Op.EQ, organization_id), Condition(Column("project_id"), Op.IN, project_ids), Condition(Column("metric_id"), Op.EQ, metric_id(organization_id, "session")), Condition(Column(release_column_name), Op.IN, releases_ids), Condition(Column("timestamp"), Op.GTE, start), Condition(Column("timestamp"), Op.LT, end), ], groupby=[Column(release_column_name)], ) result = raw_snql_query( query, referrer="release_health.metrics.check_releases_have_health_data", use_cache=False, ) def extract_row_info( row: Mapping[str, Union[OrganizationId, str]]) -> ReleaseName: return reverse_tag_value( organization_id, row.get(release_column_name)) # type: ignore return {extract_row_info(row) for row in result["data"]}