Skip to content

Commit 9a2532a

Browse files
authored
chore(ai trace queries): Support multiple queries (#91916)
- Support multiple query options as a list of query options if multiple are available - Still supports old format if we are not returning a list of options or are in a fly out - Specify flag for flyout so that the current frontend is still supported upon new Seer changes for generating queries
1 parent adeeb0a commit 9a2532a

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

src/sentry/api/endpoints/trace_explorer_ai_query.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ class TraceExplorerAIQuery(OrganizationEndpoint):
6363
@staticmethod
6464
def post(request: Request, organization: Organization) -> Response:
6565
"""
66-
Checks if we are able to run Autofix on the given group.
66+
Request to translate a natural language query into a sentry EQS query.
6767
"""
6868
if not request.user.is_authenticated:
6969
return Response(status=status.HTTP_400_BAD_REQUEST)
7070

7171
project_ids = [int(x) for x in request.data.get("project_ids", [])]
7272
natural_language_query = request.data.get("natural_language_query")
73+
limit = request.data.get("limit", 1)
74+
use_flyout = request.data.get("use_flyout", True)
7375

7476
if len(project_ids) == 0 or not natural_language_query:
7577
return Response(
@@ -102,15 +104,50 @@ def post(request: Request, organization: Organization) -> Response:
102104
)
103105
data = send_translate_request(organization.id, project_ids, natural_language_query)
104106

107+
# XXX: This is a fallback to support the old response format until we fully support using multiple queries on the frontend
108+
if "responses" in data and use_flyout:
109+
if not data["responses"]:
110+
logger.info("No results found for query")
111+
return Response(
112+
{"detail": "No results found for query"},
113+
status=status.HTTP_404_NOT_FOUND,
114+
)
115+
data = data["responses"][0]
116+
if "responses" not in data:
117+
return Response(
118+
{
119+
"status": "ok",
120+
"query": data["query"], # the sentry EQS query as a string
121+
"stats_period": data["stats_period"],
122+
"group_by": list(data.get("group_by", [])),
123+
"visualization": list(
124+
data.get("visualization")
125+
), # [{chart_type: 1, y_axes: ["count_message"]}, ...]
126+
"sort": data["sort"],
127+
}
128+
)
129+
130+
data = data["responses"][:limit]
131+
132+
if len(data) == 0:
133+
logger.info("No results found for query")
134+
return Response(
135+
{"detail": "No results found for query"},
136+
status=status.HTTP_404_NOT_FOUND,
137+
)
138+
105139
return Response(
106140
{
107141
"status": "ok",
108-
"query": data["query"], # the sentry EQS query as a string
109-
"stats_period": data["stats_period"],
110-
"group_by": list(data.get("group_by", [])),
111-
"visualization": list(
112-
data.get("visualization")
113-
), # [{chart_type: 1, y_axes: ["count_message"]}, ...]
114-
"sort": data["sort"],
142+
"queries": [
143+
{
144+
"query": query["query"],
145+
"stats_period": query["stats_period"],
146+
"group_by": list(query.get("group_by", [])),
147+
"visualization": list(query.get("visualization")),
148+
"sort": query["sort"],
149+
}
150+
for query in data
151+
],
115152
}
116153
)

0 commit comments

Comments
 (0)