def test_hourly_rounded_start(): query = _make_query("statsPeriod=30m&interval=1m&field=sum(session)") actual_timestamps = get_timestamps(query) assert actual_timestamps[0] == "2021-03-08T09:00:00Z" assert actual_timestamps[-1] == "2021-03-08T09:34:00Z" assert len(actual_timestamps) == 35 # in this case "45m" means from 08:49:00-09:34:00, but since we round start/end # to hours, we extend the start time to 08:00:00. query = _make_query("statsPeriod=45m&interval=1m&field=sum(session)") actual_timestamps = get_timestamps(query) assert actual_timestamps[0] == "2021-03-08T08:00:00Z" assert actual_timestamps[-1] == "2021-03-08T09:34:00Z" assert len(actual_timestamps) == 95
def test_rounded_end(): query = _make_query( "field=sum(session)&interval=1h&start=2021-02-24T00:00:00Z&end=2021-02-25T00:00:00Z" ) expected_timestamps = [ "2021-02-24T00:00:00Z", "2021-02-24T01:00:00Z", "2021-02-24T02:00:00Z", "2021-02-24T03:00:00Z", "2021-02-24T04:00:00Z", "2021-02-24T05:00:00Z", "2021-02-24T06:00:00Z", "2021-02-24T07:00:00Z", "2021-02-24T08:00:00Z", "2021-02-24T09:00:00Z", "2021-02-24T10:00:00Z", "2021-02-24T11:00:00Z", "2021-02-24T12:00:00Z", "2021-02-24T13:00:00Z", "2021-02-24T14:00:00Z", "2021-02-24T15:00:00Z", "2021-02-24T16:00:00Z", "2021-02-24T17:00:00Z", "2021-02-24T18:00:00Z", "2021-02-24T19:00:00Z", "2021-02-24T20:00:00Z", "2021-02-24T21:00:00Z", "2021-02-24T22:00:00Z", "2021-02-24T23:00:00Z", "2021-02-25T00:00:00Z", ] actual_timestamps = get_timestamps(query) assert len(actual_timestamps) == 25 assert actual_timestamps == expected_timestamps
def test_timestamps(): query = _make_query("statsPeriod=1d&interval=12h&field=sum(session)") expected_timestamps = ["2020-12-17T12:00:00Z", "2020-12-18T00:00:00Z"] actual_timestamps = get_timestamps(query) assert actual_timestamps == expected_timestamps
def _get_snuba_query( org_id: int, query: QueryDefinition, entity_key: EntityKey, metric_id: int, columns: List[SelectableExpression], series: bool, limit_state: _LimitState, extra_conditions: List[Condition], ) -> Optional[Query]: """Build the snuba query Return None if the results from the initial totals query was empty. """ 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 = {} for field in query.raw_groupby: if field == "session.status": # This will be handled by conditional aggregates continue if field == "project": groupby["project"] = Column("project_id") continue try: groupby[field] = Column(resolve_tag_key(field)) except MetricIndexNotFound: # exclude unresolved keys from groupby pass full_groupby = list(set(groupby.values())) if series: full_groupby.append(Column(TS_COL_GROUP)) query_args = dict( dataset=Dataset.Metrics.value, match=Entity(entity_key.value), select=columns, groupby=full_groupby, where=conditions, granularity=Granularity(query.rollup), ) # In case of group by, either set a limit or use the groups from the # first query to limit the results: if query.raw_groupby: if not limit_state.initialized: # Set limit and order by to be consistent with sessions_v2 max_groups = SNUBA_LIMIT // len(get_timestamps(query)) query_args["limit"] = Limit(max_groups) query_args["orderby"] = [OrderBy(columns[0], Direction.DESC)] else: if limit_state.limiting_conditions is None: # Initial query returned no results, no need to run any more queries return None query_args["where"] += limit_state.limiting_conditions query_args["limit"] = Limit(SNUBA_LIMIT) return Query(**query_args)