|
| 1 | +from http import HTTPStatus |
| 2 | + |
| 3 | +from drf_spectacular.utils import extend_schema |
| 4 | +from pydantic import ValidationError |
| 5 | +from rest_framework.request import Request |
| 6 | +from rest_framework.response import Response |
| 7 | +from rest_framework.views import APIView |
| 8 | + |
| 9 | +from kernelCI_app.queries.hardware import get_hardware_listing_data_by_revision |
| 10 | +from kernelCI_app.typeModels.hardwareListing import ( |
| 11 | + HardwareItem, |
| 12 | + HardwareListingResponse, |
| 13 | +) |
| 14 | +from kernelCI_app.typeModels.hardwareListingByRevision import ( |
| 15 | + HardwareListingByRevisionQueryParams, |
| 16 | + HardwareListingByRevisionQueryParamsDocumentationOnly, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class HardwareByRevisionView(APIView): |
| 21 | + def _sanitize_records(self, hardwares_raw: list[dict]) -> list[HardwareItem]: |
| 22 | + hardwares = [] |
| 23 | + for hardware in hardwares_raw: |
| 24 | + hardwares.append( |
| 25 | + HardwareItem( |
| 26 | + platform=hardware["platform"], |
| 27 | + hardware=hardware["hardware"], |
| 28 | + build_status_summary={ |
| 29 | + "PASS": hardware["pass_builds"], |
| 30 | + "FAIL": hardware["fail_builds"], |
| 31 | + "NULL": hardware["null_builds"], |
| 32 | + "ERROR": hardware["error_builds"], |
| 33 | + "MISS": hardware["miss_builds"], |
| 34 | + "DONE": hardware["done_builds"], |
| 35 | + "SKIP": hardware["skip_builds"], |
| 36 | + }, |
| 37 | + boot_status_summary={ |
| 38 | + "PASS": hardware["pass_boots"], |
| 39 | + "FAIL": hardware["fail_boots"], |
| 40 | + "NULL": hardware["null_boots"], |
| 41 | + "ERROR": hardware["error_boots"], |
| 42 | + "MISS": hardware["miss_boots"], |
| 43 | + "DONE": hardware["done_boots"], |
| 44 | + "SKIP": hardware["skip_boots"], |
| 45 | + }, |
| 46 | + test_status_summary={ |
| 47 | + "PASS": hardware["pass_tests"], |
| 48 | + "FAIL": hardware["fail_tests"], |
| 49 | + "NULL": hardware["null_tests"], |
| 50 | + "ERROR": hardware["error_tests"], |
| 51 | + "MISS": hardware["miss_tests"], |
| 52 | + "DONE": hardware["done_tests"], |
| 53 | + "SKIP": hardware["skip_tests"], |
| 54 | + }, |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + return hardwares |
| 59 | + |
| 60 | + @extend_schema( |
| 61 | + parameters=[HardwareListingByRevisionQueryParamsDocumentationOnly], |
| 62 | + responses=HardwareListingResponse, |
| 63 | + ) |
| 64 | + def get(self, request: Request): |
| 65 | + try: |
| 66 | + query_params = HardwareListingByRevisionQueryParams( |
| 67 | + origin=request.GET.get("origin"), |
| 68 | + tree_name=request.GET.get("tree_name"), |
| 69 | + git_repository_url=request.GET.get("git_repository_url"), |
| 70 | + git_repository_branch=request.GET.get("git_repository_branch"), |
| 71 | + git_commit_hash=request.GET.get("git_commit_hash"), |
| 72 | + ) |
| 73 | + except ValidationError as e: |
| 74 | + return Response(data=e.json(), status=HTTPStatus.BAD_REQUEST) |
| 75 | + |
| 76 | + hardwares_raw = get_hardware_listing_data_by_revision( |
| 77 | + origin=query_params.origin, |
| 78 | + tree_name=query_params.tree_name, |
| 79 | + git_repository_url=query_params.git_repository_url, |
| 80 | + git_repository_branch=query_params.git_repository_branch, |
| 81 | + git_commit_hash=query_params.git_commit_hash, |
| 82 | + ) |
| 83 | + |
| 84 | + try: |
| 85 | + sanitized_records = self._sanitize_records(hardwares_raw=hardwares_raw) |
| 86 | + result = HardwareListingResponse(hardware=sanitized_records) |
| 87 | + except ValidationError as e: |
| 88 | + return Response(data=e.json(), status=HTTPStatus.INTERNAL_SERVER_ERROR) |
| 89 | + |
| 90 | + return Response(data=result.model_dump(), status=HTTPStatus.OK) |
0 commit comments