|
| 1 | +from django.conf import settings |
| 2 | +from django.http.response import FileResponse, HttpResponseBase |
| 3 | +from rest_framework.request import Request |
| 4 | +from rest_framework.response import Response |
| 5 | + |
| 6 | +from sentry import analytics, features |
| 7 | +from sentry.api.api_owners import ApiOwner |
| 8 | +from sentry.api.api_publish_status import ApiPublishStatus |
| 9 | +from sentry.api.base import region_silo_endpoint |
| 10 | +from sentry.api.bases.project import ProjectEndpoint |
| 11 | +from sentry.models.files.file import File |
| 12 | +from sentry.preprod.models import PreprodArtifactSizeMetrics |
| 13 | + |
| 14 | + |
| 15 | +@region_silo_endpoint |
| 16 | +class ProjectPreprodArtifactSizeAnalysisDownloadEndpoint(ProjectEndpoint): |
| 17 | + owner = ApiOwner.EMERGE_TOOLS |
| 18 | + publish_status = { |
| 19 | + "GET": ApiPublishStatus.EXPERIMENTAL, |
| 20 | + } |
| 21 | + |
| 22 | + def get(self, request: Request, project, artifact_id) -> HttpResponseBase: |
| 23 | + """ |
| 24 | + Download size analysis results for a preprod artifact |
| 25 | + ```````````````````````````````````````````````````` |
| 26 | +
|
| 27 | + Download the size analysis results for a preprod artifact. |
| 28 | +
|
| 29 | + :pparam string organization_id_or_slug: the id or slug of the organization the |
| 30 | + artifact belongs to. |
| 31 | + :pparam string project_id_or_slug: the id or slug of the project to retrieve the |
| 32 | + artifact from. |
| 33 | + :pparam string artifact_id: the ID of the preprod artifact to download size analysis for. |
| 34 | + :auth: required |
| 35 | + """ |
| 36 | + |
| 37 | + analytics.record( |
| 38 | + "preprod_artifact.api.size_analysis_download", |
| 39 | + organization_id=project.organization_id, |
| 40 | + project_id=project.id, |
| 41 | + user_id=request.user.id, |
| 42 | + artifact_id=artifact_id, |
| 43 | + ) |
| 44 | + |
| 45 | + if not settings.IS_DEV and not features.has( |
| 46 | + "organizations:preprod-artifact-assemble", project.organization, actor=request.user |
| 47 | + ): |
| 48 | + return Response({"error": "Feature not enabled"}, status=403) |
| 49 | + |
| 50 | + try: |
| 51 | + size_metrics_qs = PreprodArtifactSizeMetrics.objects.select_related( |
| 52 | + "preprod_artifact" |
| 53 | + ).filter( |
| 54 | + preprod_artifact__project=project, |
| 55 | + preprod_artifact__id=artifact_id, |
| 56 | + ) |
| 57 | + size_metrics_count = size_metrics_qs.count() |
| 58 | + if size_metrics_count == 0: |
| 59 | + return Response( |
| 60 | + {"error": "Preprod artifact not found or size analysis results not available"}, |
| 61 | + status=404, |
| 62 | + ) |
| 63 | + elif size_metrics_count > 1: |
| 64 | + return Response( |
| 65 | + {"error": "Multiple size analysis results found for this artifact"}, |
| 66 | + status=409, |
| 67 | + ) |
| 68 | + size_metrics = size_metrics_qs.first() |
| 69 | + except Exception: |
| 70 | + return Response( |
| 71 | + {"error": "Failed to retrieve size analysis results"}, |
| 72 | + status=500, |
| 73 | + ) |
| 74 | + |
| 75 | + if size_metrics is None or size_metrics.analysis_file_id is None: |
| 76 | + return Response( |
| 77 | + {"error": "Size analysis file not available for this artifact"}, status=404 |
| 78 | + ) |
| 79 | + |
| 80 | + try: |
| 81 | + file_obj = File.objects.get(id=size_metrics.analysis_file_id) |
| 82 | + except File.DoesNotExist: |
| 83 | + return Response({"error": "Size analysis file not found"}, status=404) |
| 84 | + |
| 85 | + try: |
| 86 | + fp = file_obj.getfile() |
| 87 | + except Exception: |
| 88 | + return Response({"error": "Failed to retrieve size analysis file"}, status=500) |
| 89 | + |
| 90 | + response = FileResponse( |
| 91 | + fp, |
| 92 | + content_type="application/json", |
| 93 | + ) |
| 94 | + response["Content-Length"] = file_obj.size |
| 95 | + return response |
0 commit comments