Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/sentry/seer/explorer/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,51 @@ def get_repository_definition(*, organization_id: int, repo_full_name: str) -> d
}


def _get_issue_event_timeseries(
*,
organization: Organization,
project_id: int,
issue_short_id: str,
stats_period: str = "7d",
interval: str = "6h",
per_page: int = 50,
) -> dict[str, Any] | None:
"""
Get event counts over time for an issue by calling the events-stats endpoint.
"""

params: dict[str, Any] = {
"dataset": "issuePlatform",
"query": f"issue:{issue_short_id}",
"yAxis": "count()",
"partial": "1",
"statsPeriod": stats_period,
"interval": interval,
"per_page": per_page,
"project": project_id,
"referrer": Referrer.SEER_RPC,
}

resp = client.get(
auth=ApiKey(organization_id=organization.id, scope_list=["org:read", "project:read"]),
user=None,
path=f"/organizations/{organization.slug}/events-stats/",
params=params,
)
if resp.status_code != 200 or not (resp.data or {}).get("data"):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

The error condition check not (resp.data or {}).get("data") can fail if resp.data is not a dictionary. If the API returns a string, list, or None, calling .get() will raise an AttributeError, causing the function to crash instead of gracefully handling the error.

Suggested change
if resp.status_code != 200 or not (resp.data or {}).get("data"):
if resp.status_code != 200 or not isinstance(resp.data, dict) or not resp.data.get("data"):

Committable suggestion

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Context for Agents
[**BestPractice**]

The error condition check `not (resp.data or {}).get("data")` can fail if `resp.data` is not a dictionary. If the API returns a string, list, or None, calling `.get()` will raise an AttributeError, causing the function to crash instead of gracefully handling the error.

```suggestion
if resp.status_code != 200 or not isinstance(resp.data, dict) or not resp.data.get("data"):
```

⚡ **Committable suggestion**

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

File: src/sentry/seer/explorer/tools.py
Line: 356

logger.warning(
"Failed to get event counts for issue",
extra={
"organization_slug": organization.slug,
"project_id": project_id,
"issue_id": issue_short_id,
},
)
return None

return {"count()": {"data": resp.data["data"]}}


def get_issue_details(
*,
issue_id: str,
Expand Down Expand Up @@ -354,12 +399,12 @@ def get_issue_details(
)
return None

org_project_ids = Project.objects.filter(
organization=organization, status=ObjectStatus.ACTIVE
).values_list("id", flat=True)

try:
if issue_id.isdigit():
org_project_ids = Project.objects.filter(
organization=organization, status=ObjectStatus.ACTIVE
).values_list("id", flat=True)

group = Group.objects.get(project_id__in=org_project_ids, id=int(issue_id))
else:
group = Group.objects.by_qualified_short_id(organization_id, issue_id)
Expand Down Expand Up @@ -415,8 +460,15 @@ def get_issue_details(
)
tags_overview = None

event_timeseries = _get_issue_event_timeseries(
organization=organization,
project_id=group.project_id,
issue_short_id=group.qualified_short_id,
)

return {
"issue": serialized_group,
"event_timeseries": event_timeseries,
"tags_overview": tags_overview,
"event": serialized_event,
"event_id": event.event_id,
Expand Down
15 changes: 15 additions & 0 deletions tests/sentry/seer/explorer/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,21 @@ def _test_get_issue_details_success(
else:
assert result["event_trace_id"] is None

# Verify timeseries dict structure.
timeseries = result["event_timeseries"]
assert isinstance(timeseries, dict)
assert "count()" in timeseries
assert "data" in timeseries["count()"]
assert isinstance(timeseries["count()"]["data"], list)
for item in timeseries["count()"]["data"]:
assert len(item) == 2
assert isinstance(item[0], int)
assert isinstance(item[1], list)
assert len(item[1]) == 1
assert isinstance(item[1][0], dict)
assert "count" in item[1][0]
assert isinstance(item[1][0]["count"], int)

def test_get_issue_details_success_int_id(self):
self._test_get_issue_details_success(use_short_id=False)

Expand Down