File tree Expand file tree Collapse file tree 2 files changed +32
-2
lines changed
src/sentry/explore/endpoints
tests/sentry/explore/endpoints Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change 44from rest_framework .serializers import ListField
55
66from sentry .constants import ALL_ACCESS_PROJECTS
7+ from sentry .discover .arithmetic import is_equation
78from sentry .explore .models import ExploreSavedQueryDataset
89from sentry .utils .dates import parse_stats_period , validate_interval
910
@@ -222,9 +223,16 @@ def validate(self, data):
222223 raise serializers .ValidationError (
223224 "Metric field is only allowed for metrics dataset"
224225 )
225- if data ["dataset" ] == "metrics" and "metric" not in q :
226+
227+ # the metrics field is only required for non-equation queries
228+ has_equations = any (
229+ is_equation (y_axis )
230+ for aggregate_field in q .get ("aggregateField" , [])
231+ for y_axis in aggregate_field .get ("yAxes" , [])
232+ )
233+ if data ["dataset" ] == "metrics" and not has_equations and "metric" not in q :
226234 raise serializers .ValidationError (
227- "Metric field is required for metrics dataset"
235+ "Metric field is required for non-equation queries on the metrics dataset"
228236 )
229237 inner_query = {}
230238 for key in inner_query_keys :
Original file line number Diff line number Diff line change @@ -1391,3 +1391,25 @@ def test_post_with_empty_query_is_rejected(self) -> None:
13911391 },
13921392 )
13931393 assert response .status_code == 400
1394+
1395+ def test_post_with_equation_is_accepted (self ) -> None :
1396+ with self .feature (self .features ):
1397+ response = self .client .post (
1398+ self .url ,
1399+ {
1400+ "name" : "Equation query" ,
1401+ "projects" : self .project_ids ,
1402+ "dataset" : "metrics" ,
1403+ "query" : [
1404+ {
1405+ "aggregateField" : [{"yAxes" : ["equation|A + B" ], "chartType" : 1 }],
1406+ "mode" : "samples" ,
1407+ "fields" : ["A" , "B" ],
1408+ "orderby" : "-timestamp" ,
1409+ },
1410+ ],
1411+ },
1412+ )
1413+ assert response .status_code == 201 , response .content
1414+ data = response .data
1415+ assert data ["query" ][0 ].get ("metric" ) is None
You can’t perform that action at this time.
0 commit comments