def test_get_entity_subscription_for_metrics_dataset_for_users( self) -> None: aggregate = "percentage(users_crashed, users) AS _crash_rate_alert_aggregate" entity_subscription = get_entity_subscription_for_dataset( dataset=QueryDatasets.METRICS, aggregate=aggregate, time_window=3600, extra_fields={"org_id": self.organization.id}, ) assert isinstance(entity_subscription, MetricsSetsEntitySubscription) assert entity_subscription.aggregate == aggregate groupby = [resolve_tag_key("session.status")] assert entity_subscription.get_entity_extra_params() == { "organization": self.organization.id, "groupby": groupby, "granularity": 10, } assert entity_subscription.entity_key == EntityKey.MetricsSets assert entity_subscription.time_col == ENTITY_TIME_COLUMNS[ EntityKey.MetricsSets] assert entity_subscription.dataset == QueryDatasets.METRICS session_status = resolve_tag_key("session.status") session_status_tag_values = resolve_many_weak(["crashed", "init"]) snuba_filter = entity_subscription.build_snuba_filter("", None, None) assert snuba_filter assert snuba_filter.aggregations == [["uniq(value)", None, "value"]] assert snuba_filter.conditions == [ ["metric_id", "=", resolve(SessionMetricKey.USER.value)], [session_status, "IN", session_status_tag_values], ] assert snuba_filter.groupby == groupby assert snuba_filter.rollup == entity_subscription.get_granularity()
def test_simple_users_for_metrics(self): org_id = self.organization.id for tag in [ SessionMetricKey.USER.value, "session.status", "crashed", "init" ]: indexer.record(org_id, tag) entity_subscription = get_entity_subscription_for_dataset( dataset=QueryDatasets.METRICS, time_window=3600, aggregate= "percentage(users_crashed, users) AS _crash_rate_alert_aggregate", extra_fields={"org_id": org_id}, ) snuba_filter = build_snuba_filter( entity_subscription, query="", environment=None, ) session_status = resolve_tag_key("session.status") session_status_tag_values = resolve_many_weak(["crashed", "init"]) assert snuba_filter assert snuba_filter.aggregations == [["uniq(value)", None, "value"]] assert snuba_filter.conditions == [ ["metric_id", "=", resolve(SessionMetricKey.USER.value)], [session_status, "IN", session_status_tag_values], ] assert snuba_filter.groupby == [session_status]
def test_query_and_environment_users_metrics(self): env = self.create_environment(self.project, name="development") org_id = self.organization.id for tag in [ SessionMetricKey.USER.value, "session.status", "environment", "development", "init", "crashed", "release", "[email protected]", ]: indexer.record(org_id, tag) entity_subscription = get_entity_subscription_for_dataset( dataset=QueryDatasets.METRICS, time_window=3600, aggregate= "percentage(users_crashed, users) AS _crash_rate_alert_aggregate", extra_fields={"org_id": org_id}, ) snuba_filter = build_snuba_filter( entity_subscription, query="release:[email protected]", environment=env, ) assert snuba_filter assert snuba_filter.aggregations == [["uniq(value)", None, "value"]] assert snuba_filter.groupby == [resolve_tag_key("session.status")] assert snuba_filter.conditions == [ ["metric_id", "=", resolve(SessionMetricKey.USER.value)], [ resolve_tag_key("session.status"), "IN", resolve_many_weak(["crashed", "init"]), ], [resolve_tag_key("environment"), "=", resolve_weak("development")], [resolve_tag_key("release"), "=", resolve_weak("[email protected]")], ]
def build_snuba_filter( self, query: str, environment: Optional[Environment], params: Optional[Mapping[str, Any]] = None, ) -> Filter: snuba_filter = get_filter(query, params=params) conditions = copy(snuba_filter.conditions) session_status_tag_values = resolve_many_weak(["crashed", "init"]) snuba_filter.update_with({ "aggregations": [[f"{self.aggregation_func}(value)", None, "value"]], "conditions": [ ["metric_id", "=", resolve(self.metric_key.value)], [self.session_status, "IN", session_status_tag_values], ], "groupby": self.get_query_groupby(), "rollup": self.get_granularity(), }) if environment: snuba_filter.conditions.append([ resolve_tag_key("environment"), "=", resolve_weak(environment.name) ]) if query and len(conditions) > 0: release_conditions = [ condition for condition in conditions if condition[0] == "release" ] for release_condition in release_conditions: snuba_filter.conditions.append([ resolve_tag_key(release_condition[0]), release_condition[1], resolve_weak(release_condition[2]), ]) return snuba_filter