|
| 1 | +import os |
| 2 | +from dataclasses import dataclass, field |
| 3 | +from EndpointInfo import EndpointInfo |
| 4 | +from constants import ENDPOINT_OPERATIONS |
| 5 | +from openapi_pydantic.v3.v3_0 import OpenAPI, Reference |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class RequestStats: |
| 10 | + num_operations: int = 0 |
| 11 | + num_with_body: int = 0 |
| 12 | + num_with_examples: int = 0 |
| 13 | + num_examples: int = 0 |
| 14 | + content_types: set[str] = field(default_factory=set) |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class ResponseCodeStats: |
| 18 | + num_operations: int = 0 |
| 19 | + num_with_body: int = 0 |
| 20 | + num_with_examples: int = 0 |
| 21 | + num_examples: int = 0 |
| 22 | + content_types: set[str] = field(default_factory=set) |
| 23 | + |
| 24 | +@dataclass |
| 25 | +class ResponseStats: |
| 26 | + num_operations: int = 0 |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class OperationStats: |
| 30 | + request_stats: RequestStats = field(default_factory=RequestStats) |
| 31 | + response_stats: dict[str, ResponseCodeStats] = field(default_factory=dict) |
| 32 | + |
| 33 | +@dataclass |
| 34 | +class DetailedStats: |
| 35 | + num_endpoints: int = 0 |
| 36 | + num_operations: int = 0 |
| 37 | + operation_stats: dict[str, OperationStats] = field(default_factory=dict) |
| 38 | + |
| 39 | + |
| 40 | +class DetailedStatsGenerator: |
| 41 | + def __init__(self, openapi_spec: OpenAPI): |
| 42 | + self.openapi_spec = openapi_spec |
| 43 | + |
| 44 | + def get_endpoint_info_list(self) -> list[EndpointInfo]: |
| 45 | + endpoint_info_list = [] |
| 46 | + for path, path_item in self.openapi_spec.paths.items(): |
| 47 | + endpointInfo = EndpointInfo.from_path(path, path_item) |
| 48 | + endpoint_info_list.append(endpointInfo) |
| 49 | + return endpoint_info_list |
| 50 | + |
| 51 | + def get_detailed_stats(self) -> DetailedStats: |
| 52 | + endpoint_info_list = self.get_endpoint_info_list() |
| 53 | + stats = DetailedStats() |
| 54 | + stats.num_endpoints = len(endpoint_info_list) |
| 55 | + for operation in ENDPOINT_OPERATIONS: |
| 56 | + for endpoint_info in endpoint_info_list: |
| 57 | + if operation in endpoint_info.operations: |
| 58 | + stats.num_operations += 1 |
| 59 | + if operation not in stats.operation_stats: |
| 60 | + stats.operation_stats[operation] = OperationStats() |
| 61 | + operation_stats = stats.operation_stats[operation] |
| 62 | + operation_stats.request_stats.num_operations += 1 |
| 63 | + if endpoint_info.operations[operation].requestBody: |
| 64 | + requestBody = endpoint_info.operations[operation].requestBody |
| 65 | + if isinstance(requestBody, Reference): |
| 66 | + component_request_ref = os.path.basename(requestBody.ref) |
| 67 | + requestBody = self.openapi_spec.components.requestBodies[component_request_ref] |
| 68 | + for content_type, media_type in requestBody.content.items(): |
| 69 | + operation_stats.request_stats.content_types.add(content_type) |
| 70 | + if media_type.examples: |
| 71 | + operation_stats.request_stats.num_with_examples += 1 |
| 72 | + operation_stats.request_stats.num_examples += len(media_type.examples) |
| 73 | + operation_stats.request_stats.num_with_body += 1 |
| 74 | + if endpoint_info.operations[operation].responses: |
| 75 | + for response_code, response in endpoint_info.operations[operation].responses.items(): |
| 76 | + if response_code not in operation_stats.response_stats: |
| 77 | + operation_stats.response_stats[response_code] = ResponseCodeStats() |
| 78 | + operation_stats.response_stats[response_code].num_operations += 1 |
| 79 | + if isinstance(response, Reference): |
| 80 | + component_response_ref = os.path.basename(response.ref) |
| 81 | + response = self.openapi_spec.components.responses[component_response_ref] |
| 82 | + if response.content: |
| 83 | + for content_type, media_type in response.content.items(): |
| 84 | + operation_stats.response_stats[response_code].content_types.add(content_type) |
| 85 | + if media_type.examples: |
| 86 | + operation_stats.response_stats[response_code].num_with_examples += 1 |
| 87 | + operation_stats.response_stats[response_code].num_examples += len(media_type.examples) |
| 88 | + operation_stats.response_stats[response_code].num_with_body += 1 |
| 89 | + return stats |
| 90 | + |
| 91 | + def print_detailed_stats(self, stats: DetailedStats): |
| 92 | + print("========================") |
| 93 | + print("==== Detailed Stats ====") |
| 94 | + print("========================") |
| 95 | + print(f"Number of endpoints: {stats.num_endpoints}") |
| 96 | + print(f"Number of operations: {stats.num_operations}") |
| 97 | + for operation, operation_stats in stats.operation_stats.items(): |
| 98 | + print(f" {operation}: {operation_stats.request_stats.num_operations}") |
| 99 | + print(" Requests:") |
| 100 | + print(f" with body: {operation_stats.request_stats.num_with_body}") |
| 101 | + print(" Content types:") |
| 102 | + for content_type in operation_stats.request_stats.content_types: |
| 103 | + print(f" {content_type}") |
| 104 | + print(f" Has examples: {operation_stats.request_stats.num_with_examples}") |
| 105 | + print(f" Number of examples: {operation_stats.request_stats.num_examples}") |
| 106 | + for response_code, response_code_stats in operation_stats.response_stats.items(): |
| 107 | + print(" Responses:") |
| 108 | + print(f" '{response_code}': {response_code_stats.num_operations}") |
| 109 | + print(f" Has content: {response_code_stats.num_with_body}") |
| 110 | + print(" Content types:") |
| 111 | + for content_type in response_code_stats.content_types: |
| 112 | + print(f" {content_type}") |
| 113 | + print(f" Has examples: {response_code_stats.num_with_examples}") |
| 114 | + print(f" Number of examples: {response_code_stats.num_examples}") |
| 115 | + print() |
0 commit comments