Skip to content
Merged
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
29 changes: 29 additions & 0 deletions src/sentry/api/endpoints/project_web_vitals_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from rest_framework import status
from rest_framework.request import Request
from rest_framework.response import Response

from sentry.api.api_owners import ApiOwner
from sentry.api.api_publish_status import ApiPublishStatus
from sentry.api.base import region_silo_endpoint
from sentry.api.bases.project import ProjectEndpoint
from sentry.models.project import Project
from sentry.tasks.web_vitals_issue_detection import dispatch_detection_for_project_ids


@region_silo_endpoint
class ProjectWebVitalsDetectionEndpoint(ProjectEndpoint):
owner = ApiOwner.DATA_BROWSING
publish_status = {
"GET": ApiPublishStatus.PRIVATE,
}

def get(self, request: Request, project: Project) -> Response:
results = dispatch_detection_for_project_ids([project.id])
if project.id not in results:
return Response({"status": "invalid_project"}, status=status.HTTP_400_BAD_REQUEST)
Copy link
Contributor

Choose a reason for hiding this comment

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

@edwardgou-sentry should we add better messaging on the not ok status? Bu t yeah can be done on a following PR.

if results[project.id].get("success", False):
return Response({"status": "dispatched"}, status=status.HTTP_202_ACCEPTED)
return Response(
{"status": results[project.id].get("reason", "rejected")},
status=status.HTTP_400_BAD_REQUEST,
)
6 changes: 6 additions & 0 deletions src/sentry/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from sentry.api.endpoints.project_statistical_detectors import ProjectStatisticalDetectors
from sentry.api.endpoints.project_template_detail import OrganizationProjectTemplateDetailEndpoint
from sentry.api.endpoints.project_templates_index import OrganizationProjectTemplatesIndexEndpoint
from sentry.api.endpoints.project_web_vitals_detection import ProjectWebVitalsDetectionEndpoint
from sentry.api.endpoints.release_thresholds.release_threshold import ReleaseThresholdEndpoint
from sentry.api.endpoints.release_thresholds.release_threshold_details import (
ReleaseThresholdDetailsEndpoint,
Expand Down Expand Up @@ -3092,6 +3093,11 @@ def create_group_urls(name_prefix: str) -> list[URLPattern | URLResolver]:
ProjectPerformanceGeneralSettingsEndpoint.as_view(),
name="sentry-api-0-project-performance-general-settings",
),
re_path(
r"^(?P<organization_id_or_slug>[^/]+)/(?P<project_id_or_slug>[^/]+)/web-vitals-detector/$",
ProjectWebVitalsDetectionEndpoint.as_view(),
name="sentry-api-0-project-web-vitals-detection",
),
# Load plugin project urls
re_path(
r"^(?P<organization_id_or_slug>[^/]+)/(?P<project_id_or_slug>[^/]+)/plugins/$",
Expand Down
11 changes: 10 additions & 1 deletion src/sentry/tasks/web_vitals_issue_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,30 @@ def run_web_vitals_issue_detection() -> None:
dispatch_detection_for_project_ids(project_ids_batch)


def dispatch_detection_for_project_ids(project_ids: list[int]) -> None:
# Returns a list of tuples of (project_id, success)
def dispatch_detection_for_project_ids(
project_ids: list[int],
) -> dict[int, dict[str, bool | str | None]]:
# Spawn a sub-task for each project
projects = Project.objects.filter(id__in=project_ids).select_related("organization")
projects_checked_count = 0
projects_dispatched_count = 0
results: dict[int, dict[str, bool | str | None]] = {}

for project in projects:
projects_checked_count += 1
if not check_seer_setup_for_project(project):
results[project.id] = {"success": False, "reason": "missing_seer_setup"}
continue

# Check if web vitals detection is enabled in the project's performance issue settings
performance_settings = get_merged_settings(project.id)
if not performance_settings.get("web_vitals_detection_enabled", False):
results[project.id] = {"success": False, "reason": "web_vitals_detection_not_enabled"}
continue

detect_web_vitals_issues_for_project.delay(project.id)
results[project.id] = {"success": True}
projects_dispatched_count += 1

metrics.incr(
Expand All @@ -111,6 +118,8 @@ def dispatch_detection_for_project_ids(project_ids: list[int]) -> None:
sample_rate=1.0,
)

return results


@instrumented_task(
name="sentry.tasks.web_vitals_issue_detection.detect_web_vitals_issues_for_project",
Expand Down
1 change: 1 addition & 0 deletions static/app/utils/api/knownSentryApiUrls.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ export type KnownSentryApiUrls =
| '/projects/$organizationIdOrSlug/$projectIdOrSlug/user-reports/'
| '/projects/$organizationIdOrSlug/$projectIdOrSlug/user-stats/'
| '/projects/$organizationIdOrSlug/$projectIdOrSlug/users/'
| '/projects/$organizationIdOrSlug/$projectIdOrSlug/web-vitals-detector/'
| '/projects/$organizationIdOrSlug/pr-comments/$repoName/$prNumber/'
| '/projects/$organizationIdOrSlug/pull-requests/size-analysis/$artifactId/'
| '/projects/$organizationIdOrSlug/pullrequest-details/$repoName/$prNumber/'
Expand Down
32 changes: 32 additions & 0 deletions tests/sentry/api/endpoints/test_project_web_vitals_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from unittest.mock import patch

from sentry.testutils.cases import APITestCase


class ProjectWebVitalsDetectionTest(APITestCase):
endpoint = "sentry-api-0-project-web-vitals-detection"
method = "get"

def setUp(self):
super().setUp()
self.login_as(user=self.user)

@patch("sentry.api.endpoints.project_web_vitals_detection.dispatch_detection_for_project_ids")
def test_get_success(self, mock_dispatch):
mock_dispatch.return_value = {self.project.id: {"success": True}}

response = self.get_success_response(
self.organization.slug, self.project.slug, status_code=202
)

assert response.status_code == 202
assert response.data == {"status": "dispatched"}
mock_dispatch.assert_called_once_with([self.project.id])

def test_get_requires_project_access(self):
other_user = self.create_user()
self.login_as(user=other_user)

response = self.get_error_response(self.organization.slug, self.project.slug)

assert response.status_code == 403
Loading