Skip to content

Commit 1dd7f75

Browse files
author
Riccardo Busetti
authored
feat(ddm): Return correct data format when no queries are run in metrics/query (#68261)
1 parent 52dfe43 commit 1dd7f75

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

src/sentry/sentry_metrics/querying/data/api.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ def run_metrics_queries_plan(
3939
A MetricsQueriesPlanResult object which encapsulates the results of the plan and allows a QueryTransformer
4040
to be run on the data.
4141
"""
42-
# For now, if the query plan is empty, we return an empty dictionary. In the future, we might want to default
43-
# to a better data type.
44-
if metrics_queries_plan.is_empty():
45-
return MetricsQueriesPlanResult([])
46-
4742
# We build the basic query that contains the metadata which will be shared across all queries.
4843
base_query = MetricsQuery(
4944
start=start,

src/sentry/sentry_metrics/querying/data/execution.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,11 @@ def schedule(self, intermediate_query: IntermediateQuery, query_type: QueryType)
812812
if query_type == QueryType.TOTALS_AND_SERIES:
813813
series_query = replace(totals_query, type=ScheduledQueryType.SERIES)
814814

815-
final_query = replace(totals_query, next=series_query)
816-
817815
# We initialize the query by performing type-aware mutations that prepare the query to be executed correctly
818816
# (e.g., adding `totals` to a totals query...).
819-
self._scheduled_queries.append(
820-
final_query.initialize(
821-
self._organization, self._projects, self._blocked_metrics_for_projects
822-
)
817+
final_query = replace(totals_query, next=series_query).initialize(
818+
self._organization, self._projects, self._blocked_metrics_for_projects
823819
)
820+
821+
self._scheduled_queries.append(final_query)
824822
self._query_results.append(None)

src/sentry/sentry_metrics/querying/data/transformation/metrics_api.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,16 @@ def transform(self, query_results: list[QueryResult]) -> Mapping[str, Any]:
237237
Returns:
238238
A mapping containing the data transformed in the correct format.
239239
"""
240-
# If we have not run any queries, we won't return anything back.
240+
base_result: dict[str, Any] = {
241+
"data": [],
242+
"meta": [],
243+
"start": None,
244+
"end": None,
245+
"intervals": [],
246+
}
247+
241248
if not query_results:
242-
return {}
249+
return base_result
243250

244251
# We first build intermediate results that we can work efficiently with.
245252
queries_groups, queries_meta = self._build_intermediate_results(query_results)
@@ -277,13 +284,10 @@ def transform(self, query_results: list[QueryResult]) -> Mapping[str, Any]:
277284
for query_meta in queries_meta:
278285
transformed_queries_meta.append([meta.meta for meta in query_meta])
279286

280-
base_result = {
281-
"data": transformed_queries_groups,
282-
"meta": transformed_queries_meta,
283-
"start": start,
284-
"end": end,
285-
}
286-
287+
base_result["data"] = transformed_queries_groups
288+
base_result["meta"] = transformed_queries_meta
289+
base_result["start"] = start
290+
base_result["end"] = end
287291
if intervals is not None:
288292
base_result["intervals"] = intervals
289293

tests/sentry/api/endpoints/test_organization_metrics_query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def test_query_with_totals(self):
7474
"includeSeries": "false",
7575
},
7676
)
77-
assert "intervals" not in response.data
7877
assert response.data["data"] == [[{"by": {}, "totals": 18.0}]]
7978
assert response.data["meta"] == [
8079
[
@@ -90,6 +89,7 @@ def test_query_with_totals(self):
9089
},
9190
]
9291
]
92+
assert response.data["intervals"] == []
9393

9494
def test_query_with_disabled_org(self):
9595
with self.options({"custom-metrics-querying-disabled-orgs": [self.organization.id]}):

tests/sentry/sentry_metrics/querying/data/test_api.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,26 @@ def run_query(
133133
query_type,
134134
).apply_transformer(self.query_transformer)
135135

136+
@with_feature("organizations:ddm-metrics-api-unit-normalization")
137+
def test_query_with_no_formulas(self) -> None:
138+
query_1 = self.mql("sum", TransactionMRI.DURATION.value, "transaction:/bar")
139+
plan = MetricsQueriesPlan().declare_query("query_1", query_1)
140+
141+
results = self.run_query(
142+
metrics_queries_plan=plan,
143+
start=self.now() - timedelta(minutes=30),
144+
end=self.now() + timedelta(hours=1, minutes=30),
145+
interval=3600,
146+
organization=self.project.organization,
147+
projects=[self.project],
148+
environments=[],
149+
referrer="metrics.data.api",
150+
)
151+
assert len(results["data"]) == 0
152+
assert len(results["meta"]) == 0
153+
assert results["start"] is None
154+
assert results["end"] is None
155+
136156
@with_feature("organizations:ddm-metrics-api-unit-normalization")
137157
@pytest.mark.xfail
138158
def test_query_with_empty_results(self) -> None:
@@ -237,7 +257,7 @@ def test_query_with_one_aggregation_and_only_totals(self) -> None:
237257
referrer="metrics.data.api",
238258
query_type=QueryType.TOTALS,
239259
)
240-
assert "intervals" not in results
260+
assert results["intervals"] == []
241261
data = results["data"]
242262
assert len(data) == 1
243263
assert data[0][0]["by"] == {}
@@ -498,7 +518,7 @@ def test_query_with_group_by_and_order_by_and_only_totals(self) -> None:
498518
referrer="metrics.data.api",
499519
query_type=QueryType.TOTALS,
500520
)
501-
assert "intervals" not in results
521+
assert results["intervals"] == []
502522
data = results["data"]
503523
assert len(data) == 1
504524
assert len(data[0]) == 2

0 commit comments

Comments
 (0)