|
| 1 | +from drf_spectacular.utils import extend_schema |
| 2 | +from rest_framework.request import Request |
| 3 | +from rest_framework.response import Response |
| 4 | + |
| 5 | +from sentry import features |
| 6 | +from sentry.api.api_owners import ApiOwner |
| 7 | +from sentry.api.api_publish_status import ApiPublishStatus |
| 8 | +from sentry.api.base import region_silo_endpoint |
| 9 | +from sentry.api.bases.organization_events import OrganizationEventsV2EndpointBase |
| 10 | +from sentry.api.exceptions import ResourceDoesNotExist |
| 11 | +from sentry.api.paginator import OffsetPaginator |
| 12 | +from sentry.api.serializers.base import serialize |
| 13 | +from sentry.apidocs.constants import ( |
| 14 | + RESPONSE_BAD_REQUEST, |
| 15 | + RESPONSE_FORBIDDEN, |
| 16 | + RESPONSE_NOT_FOUND, |
| 17 | + RESPONSE_UNAUTHORIZED, |
| 18 | +) |
| 19 | +from sentry.apidocs.examples.organization_examples import OrganizationExamples |
| 20 | +from sentry.apidocs.parameters import GlobalParams |
| 21 | +from sentry.apidocs.utils import inline_sentry_response_serializer |
| 22 | +from sentry.models.organization import Organization |
| 23 | +from sentry.seer.anomaly_detection.get_historical_anomalies import ( |
| 24 | + get_historical_anomaly_data_from_seer_preview, |
| 25 | +) |
| 26 | +from sentry.seer.anomaly_detection.types import DetectAnomaliesResponse, TimeSeriesPoint |
| 27 | + |
| 28 | + |
| 29 | +@region_silo_endpoint |
| 30 | +class OrganizationEventsAnomaliesEndpoint(OrganizationEventsV2EndpointBase): |
| 31 | + owner = ApiOwner.ALERTS_NOTIFICATIONS |
| 32 | + publish_status = { |
| 33 | + "POST": ApiPublishStatus.EXPERIMENTAL, |
| 34 | + } |
| 35 | + |
| 36 | + @extend_schema( |
| 37 | + operation_id="Identify anomalies in historical data", |
| 38 | + parameters=[GlobalParams.ORG_ID_OR_SLUG], |
| 39 | + responses={ |
| 40 | + 200: inline_sentry_response_serializer( |
| 41 | + "ListAlertRuleAnomalies", DetectAnomaliesResponse |
| 42 | + ), |
| 43 | + 400: RESPONSE_BAD_REQUEST, |
| 44 | + 401: RESPONSE_UNAUTHORIZED, |
| 45 | + 403: RESPONSE_FORBIDDEN, |
| 46 | + 404: RESPONSE_NOT_FOUND, |
| 47 | + }, |
| 48 | + examples=OrganizationExamples.GET_HISTORICAL_ANOMALIES, |
| 49 | + ) |
| 50 | + def _format_historical_data(self, data) -> list[TimeSeriesPoint] | None: |
| 51 | + """ |
| 52 | + Format EventsStatsData into the format that the Seer API expects. |
| 53 | + EventsStatsData is a list of lists with this format: |
| 54 | + [epoch timestamp, {'count': count}] |
| 55 | + Convert the data to this format: |
| 56 | + list[TimeSeriesPoint] |
| 57 | + """ |
| 58 | + if data is None: |
| 59 | + return data |
| 60 | + |
| 61 | + formatted_data: list[TimeSeriesPoint] = [] |
| 62 | + for datum in data: |
| 63 | + ts_point = TimeSeriesPoint(timestamp=datum[0], value=datum[1].get("count", 0)) |
| 64 | + formatted_data.append(ts_point) |
| 65 | + return formatted_data |
| 66 | + |
| 67 | + def post(self, request: Request, organization: Organization) -> Response: |
| 68 | + """ |
| 69 | + Return a list of anomalies for a time series of historical event data. |
| 70 | + """ |
| 71 | + if not features.has("organizations:anomaly-detection-alerts", organization): |
| 72 | + raise ResourceDoesNotExist("Your organization does not have access to this feature.") |
| 73 | + |
| 74 | + historical_data = self._format_historical_data(request.data.get("historical_data")) |
| 75 | + current_data = self._format_historical_data(request.data.get("current_data")) |
| 76 | + |
| 77 | + config = request.data.get("config") |
| 78 | + project_id = request.data.get("project_id") |
| 79 | + |
| 80 | + if project_id is None or not config or not historical_data or not current_data: |
| 81 | + return Response( |
| 82 | + "Unable to get historical anomaly data: missing required argument(s) project, start, and/or end", |
| 83 | + status=400, |
| 84 | + ) |
| 85 | + |
| 86 | + anomalies = get_historical_anomaly_data_from_seer_preview( |
| 87 | + current_data, historical_data, project_id, config |
| 88 | + ) |
| 89 | + # NOTE: returns None if there's a problem with the Seer response |
| 90 | + if anomalies is None: |
| 91 | + return Response("Unable to get historical anomaly data", status=400) |
| 92 | + # NOTE: returns empty list if there is not enough event data |
| 93 | + return self.paginate( |
| 94 | + request=request, |
| 95 | + queryset=anomalies, |
| 96 | + paginator_cls=OffsetPaginator, |
| 97 | + on_results=lambda x: serialize(x, request.user), |
| 98 | + ) |
0 commit comments