-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat(seer): Add workflows list endpoint for Night Shift runs #113491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bc52ee3
feat(seer): Add workflows list endpoint for Night Shift runs
trevor-e 9d2a2de
:hammer_and_wrench: Sync API Urls to TypeScript
getsantry[bot] dad8b36
ref(seer): Use prefetch_related for night shift run issues
trevor-e fba8f91
ref(seer): Make SeerNightShiftRun serializer self-prefetch
trevor-e 9087dcc
feat(seer): Persist agent_run_id to SeerNightShiftRun extras
trevor-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping, Sequence | ||
| from typing import Any, TypedDict | ||
|
|
||
| from django.db.models import prefetch_related_objects | ||
|
|
||
| from sentry.api.serializers import Serializer, register | ||
| from sentry.seer.models.night_shift import SeerNightShiftRun, SeerNightShiftRunIssue | ||
|
|
||
|
|
||
| class SeerNightShiftRunIssueResponse(TypedDict): | ||
| id: str | ||
| groupId: str | ||
| action: str | ||
| seerRunId: str | None | ||
| dateAdded: str | ||
|
|
||
|
|
||
| class SeerNightShiftRunResponse(TypedDict): | ||
| id: str | ||
| dateAdded: str | ||
| triageStrategy: str | ||
| errorMessage: str | None | ||
| extras: dict[str, Any] | ||
| issues: list[SeerNightShiftRunIssueResponse] | ||
|
|
||
|
|
||
| @register(SeerNightShiftRun) | ||
| class SeerNightShiftRunSerializer(Serializer): | ||
| def get_attrs( | ||
| self, item_list: Sequence[SeerNightShiftRun], user: Any, **kwargs: Any | ||
| ) -> dict[SeerNightShiftRun, dict[str, Any]]: | ||
| prefetch_related_objects(item_list, "issues") | ||
| return {} | ||
|
|
||
| def serialize( | ||
| self, | ||
| obj: SeerNightShiftRun, | ||
| attrs: Mapping[str, Any], | ||
| user: Any, | ||
| **kwargs: Any, | ||
| ) -> SeerNightShiftRunResponse: | ||
| return { | ||
| "id": str(obj.id), | ||
| "dateAdded": obj.date_added.isoformat(), | ||
| "triageStrategy": obj.triage_strategy, | ||
| "errorMessage": obj.error_message, | ||
| "extras": obj.extras or {}, | ||
| "issues": [_serialize_issue(i) for i in obj.issues.all()], | ||
| } | ||
|
|
||
|
|
||
| def _serialize_issue(issue: SeerNightShiftRunIssue) -> SeerNightShiftRunIssueResponse: | ||
| return { | ||
| "id": str(issue.id), | ||
| "groupId": str(issue.group_id), | ||
| "action": issue.action, | ||
| "seerRunId": issue.seer_run_id, | ||
| "dateAdded": issue.date_added.isoformat(), | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from rest_framework.exceptions import NotFound | ||
| from rest_framework.request import Request | ||
| from rest_framework.response import Response | ||
|
|
||
| from sentry import features | ||
| from sentry.api.api_owners import ApiOwner | ||
| from sentry.api.api_publish_status import ApiPublishStatus | ||
| from sentry.api.base import cell_silo_endpoint | ||
| from sentry.api.bases.organization import OrganizationEndpoint, OrganizationPermission | ||
| from sentry.api.paginator import OffsetPaginator | ||
| from sentry.api.serializers import serialize | ||
| from sentry.api.serializers.models.seer_night_shift_run import ( # noqa: F401 -- registers serializer | ||
| SeerNightShiftRunSerializer, | ||
| ) | ||
| from sentry.models.organization import Organization | ||
| from sentry.seer.models.night_shift import SeerNightShiftRun | ||
|
|
||
|
|
||
| class OrganizationSeerWorkflowsPermission(OrganizationPermission): | ||
| scope_map = { | ||
| "GET": ["org:read"], | ||
| } | ||
|
|
||
|
|
||
| @cell_silo_endpoint | ||
| class OrganizationSeerWorkflowsEndpoint(OrganizationEndpoint): | ||
| publish_status = { | ||
| "GET": ApiPublishStatus.PRIVATE, | ||
| } | ||
| owner = ApiOwner.ML_AI | ||
| permission_classes = (OrganizationSeerWorkflowsPermission,) | ||
|
|
||
| def get(self, request: Request, organization: Organization) -> Response: | ||
| if not features.has("organizations:seer-night-shift", organization): | ||
| raise NotFound | ||
|
|
||
| queryset = SeerNightShiftRun.objects.filter(organization_id=organization.id) | ||
|
|
||
| return self.paginate( | ||
| request=request, | ||
| queryset=queryset, | ||
| order_by="-date_added", | ||
| on_results=lambda x: serialize(x, request.user), | ||
| paginator_cls=OffsetPaginator, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,6 +145,8 @@ def run_night_shift_for_org( | |
| candidates, agent_run_id = agentic_triage_strategy( | ||
| eligible_projects, organization, resolved_max_candidates | ||
| ) | ||
| if agent_run_id is not None: | ||
| run.update(extras={**run.extras, "agent_run_id": agent_run_id}) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add a real FK once our other Seer models are finally added. |
||
|
|
||
| if candidates: | ||
| SeerNightShiftRunIssue.objects.bulk_create( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
tests/sentry/seer/endpoints/test_organization_seer_workflows.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| from sentry.seer.models.night_shift import SeerNightShiftRun, SeerNightShiftRunIssue | ||
| from sentry.testutils.cases import APITestCase | ||
|
|
||
|
|
||
| class OrganizationSeerWorkflowsTest(APITestCase): | ||
| endpoint = "sentry-api-0-organization-seer-workflows" | ||
|
|
||
| def setUp(self) -> None: | ||
| super().setUp() | ||
| self.login_as(user=self.user) | ||
|
|
||
| def test_feature_flag_disabled_returns_404(self) -> None: | ||
| SeerNightShiftRun.objects.create( | ||
| organization=self.organization, | ||
| triage_strategy="agentic", | ||
| ) | ||
| self.get_error_response(self.organization.slug, status_code=404) | ||
|
|
||
| def test_returns_runs_for_org_with_nested_issues(self) -> None: | ||
| group = self.create_group() | ||
| run = SeerNightShiftRun.objects.create( | ||
| organization=self.organization, | ||
| triage_strategy="agentic", | ||
| extras={"foo": "bar"}, | ||
| ) | ||
| issue = SeerNightShiftRunIssue.objects.create( | ||
| run=run, | ||
| group=group, | ||
| action="autofix_triggered", | ||
| seer_run_id="seer-123", | ||
| ) | ||
|
|
||
| with self.feature("organizations:seer-night-shift"): | ||
| response = self.get_success_response(self.organization.slug) | ||
|
|
||
| assert len(response.data) == 1 | ||
| assert response.data[0]["id"] == str(run.id) | ||
| assert response.data[0]["triageStrategy"] == "agentic" | ||
| assert response.data[0]["errorMessage"] is None | ||
| assert response.data[0]["extras"] == {"foo": "bar"} | ||
| assert len(response.data[0]["issues"]) == 1 | ||
|
|
||
| issue_data = response.data[0]["issues"][0] | ||
| assert issue_data["id"] == str(issue.id) | ||
| assert issue_data["groupId"] == str(group.id) | ||
| assert issue_data["action"] == "autofix_triggered" | ||
| assert issue_data["seerRunId"] == "seer-123" | ||
|
|
||
| def test_runs_ordered_by_date_added_desc(self) -> None: | ||
| older = SeerNightShiftRun.objects.create( | ||
| organization=self.organization, | ||
| triage_strategy="agentic", | ||
| ) | ||
| newer = SeerNightShiftRun.objects.create( | ||
| organization=self.organization, | ||
| triage_strategy="simple", | ||
| ) | ||
|
|
||
| with self.feature("organizations:seer-night-shift"): | ||
| response = self.get_success_response(self.organization.slug) | ||
|
|
||
| assert [r["id"] for r in response.data] == [str(newer.id), str(older.id)] | ||
|
|
||
| def test_runs_scoped_to_requesting_org(self) -> None: | ||
| other_org = self.create_organization() | ||
| SeerNightShiftRun.objects.create( | ||
| organization=other_org, | ||
| triage_strategy="agentic", | ||
| ) | ||
| own_run = SeerNightShiftRun.objects.create( | ||
| organization=self.organization, | ||
| triage_strategy="agentic", | ||
| ) | ||
|
|
||
| with self.feature("organizations:seer-night-shift"): | ||
| response = self.get_success_response(self.organization.slug) | ||
|
|
||
| assert len(response.data) == 1 | ||
| assert response.data[0]["id"] == str(own_run.id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.