Lizzy Presland | 9cd06da | 2022-09-27 16:52:30 +0000 | [diff] [blame] | 1 | # Copyright 2022 The ChromiumOS Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Observability API Service. |
| 6 | |
| 7 | The monitoring-related API endpoints should generally be found here. |
| 8 | """ |
| 9 | |
| 10 | from typing import Dict, Tuple, TYPE_CHECKING |
| 11 | |
| 12 | from chromite.api import faux |
| 13 | from chromite.api import validate |
| 14 | from chromite.api.gen.chromiumos import common_pb2 |
| 15 | from chromite.lib import constants |
| 16 | from chromite.service import observability as observability_service |
| 17 | |
| 18 | |
| 19 | if TYPE_CHECKING: |
| 20 | from chromite.api.gen.chromite.api import observability_pb2 |
| 21 | |
| 22 | |
| 23 | # Dict to allow easily translating names to enum ids and vice versa. |
| 24 | _IMAGE_MAPPING = { |
| 25 | common_pb2.IMAGE_TYPE_BASE: constants.IMAGE_TYPE_BASE, |
| 26 | constants.IMAGE_TYPE_BASE: common_pb2.IMAGE_TYPE_BASE, |
| 27 | common_pb2.IMAGE_TYPE_DEV: constants.IMAGE_TYPE_DEV, |
| 28 | constants.IMAGE_TYPE_DEV: common_pb2.IMAGE_TYPE_DEV, |
| 29 | common_pb2.IMAGE_TYPE_TEST: constants.IMAGE_TYPE_TEST, |
| 30 | constants.IMAGE_TYPE_TEST: common_pb2.IMAGE_TYPE_TEST, |
| 31 | } |
| 32 | |
| 33 | |
| 34 | @faux.all_empty |
| 35 | @validate.validation_complete |
| 36 | def GetImageSizeData( |
| 37 | input_proto: "observability_pb2.GetImageSizeDataRequest", |
| 38 | output_proto: "observability_pb2.GetImageSizeDataResponse", |
| 39 | _config: "api_config.ApiConfig", |
| 40 | ): |
| 41 | """Kick off data reshaping and retrieval for ImageSize dataset. |
| 42 | |
| 43 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 44 | input_proto: The data provided by the request. |
| 45 | output_proto: The resulting output message. |
| 46 | _config: The config provided with the API call. |
Lizzy Presland | 9cd06da | 2022-09-27 16:52:30 +0000 | [diff] [blame] | 47 | """ |
| 48 | reshaped_input = {} |
| 49 | for image in input_proto.built_images: |
| 50 | if image.type not in _IMAGE_MAPPING: |
| 51 | continue |
| 52 | reshaped_input[image.path] = _IMAGE_MAPPING[image.type] |
| 53 | |
| 54 | package_size_data = observability_service.get_image_size_data( |
| 55 | reshaped_input |
| 56 | ) |
| 57 | |
| 58 | for image_type, package_sizes in package_size_data.items(): |
| 59 | _build_package_size_output(image_type, package_sizes, output_proto) |
| 60 | |
| 61 | |
| 62 | def _build_package_size_output( |
| 63 | image_type: str, |
| 64 | package_sizes: Dict[ |
| 65 | str, Dict[observability_service.PackageIdentifier, Tuple[int, int]] |
| 66 | ], |
| 67 | output_proto: "observability_pb2.GetImageSizeDataResponse", |
| 68 | ): |
| 69 | """Convert package size data to equivalent proto format. |
| 70 | |
| 71 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 72 | image_type: The string representation of the type of image |
| 73 | (base, dev, test). |
| 74 | package_sizes: The structured Python dict of partition:(PackageId:size) |
| 75 | output_proto: The proto to insert structured data into |
Lizzy Presland | 9cd06da | 2022-09-27 16:52:30 +0000 | [diff] [blame] | 76 | """ |
| 77 | image_data_proto = output_proto.image_data.add() |
| 78 | image_data_proto.image_type = _IMAGE_MAPPING[image_type] |
| 79 | for partition, package_data in package_sizes.items(): |
| 80 | image_partition_proto = image_data_proto.image_partition_data.add() |
| 81 | image_partition_proto.partition_name = partition |
| 82 | partition_apparent_size = 0 |
| 83 | partition_disk_utilization = 0 |
| 84 | # TODO: summation of individual package sizes? |
| 85 | for package_id, sizes in package_data.items(): |
| 86 | package_size_proto = image_partition_proto.packages.add() |
| 87 | _get_package_identifier_proto( |
| 88 | package_id, package_size_proto.identifier |
| 89 | ) |
| 90 | package_size_proto.apparent_size = sizes[0] |
| 91 | partition_apparent_size += sizes[0] |
| 92 | package_size_proto.disk_utilization_size = sizes[1] |
| 93 | partition_disk_utilization += sizes[1] |
| 94 | image_partition_proto.partition_apparent_size = partition_apparent_size |
| 95 | image_partition_proto.partition_disk_utilization_size = ( |
| 96 | partition_disk_utilization |
| 97 | ) |
| 98 | |
| 99 | |
| 100 | def _get_package_identifier_proto( |
| 101 | python_copy: observability_service.PackageIdentifier, |
| 102 | proto_copy: "sizes_pb2.PackageIdentifier", |
| 103 | ): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 104 | """Convert PackageIdentifier named tuple to PackageIdentifier protobuf. |
Lizzy Presland | 9cd06da | 2022-09-27 16:52:30 +0000 | [diff] [blame] | 105 | |
| 106 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 107 | python_copy: The named tuple version of PackageIdentifier. |
| 108 | proto_copy: The protobuf version of PackageIdentifier. |
Lizzy Presland | 9cd06da | 2022-09-27 16:52:30 +0000 | [diff] [blame] | 109 | |
| 110 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 111 | None |
Lizzy Presland | 9cd06da | 2022-09-27 16:52:30 +0000 | [diff] [blame] | 112 | """ |
| 113 | proto_copy.package_name.atom = python_copy.package_name.atom |
| 114 | proto_copy.package_name.category = python_copy.package_name.category |
| 115 | proto_copy.package_name.package_name = python_copy.package_name.package_name |
| 116 | proto_copy.package_version.major = python_copy.package_version.major |
Alex Klein | a0bbeac | 2023-03-02 11:55:16 -0700 | [diff] [blame] | 117 | proto_copy.package_version.minor = python_copy.package_version.minor |
Lizzy Presland | 9cd06da | 2022-09-27 16:52:30 +0000 | [diff] [blame] | 118 | proto_copy.package_version.patch = python_copy.package_version.patch |
| 119 | proto_copy.package_version.extended = python_copy.package_version.extended |
| 120 | proto_copy.package_version.revision = python_copy.package_version.revision |
| 121 | proto_copy.package_version.full_version = ( |
| 122 | python_copy.package_version.full_version |
| 123 | ) |