diff --git a/src/aggregate/aggregate_service.py b/src/aggregate/aggregate_service.py index af35121..48c03a8 100644 --- a/src/aggregate/aggregate_service.py +++ b/src/aggregate/aggregate_service.py @@ -16,8 +16,7 @@ async def get_aggregates(request: Request, metric: str = "relative") -> List[Agg # You can use the metric parameter to filter or modify results as needed # For now, just return the same data and show metric usage logger.debug(f"Metric used: {metric}") - client = request.app.requests_client - __aggregates = await get_data_from_ontap(client, logger, "172.16.57.2", "admin", "Netapp12", "storage/aggregates", "fields=name,uuid,space,node,home_node") + __aggregates = await get_data_from_ontap(request, logger, "172.16.57.2", "admin", "Netapp12", "storage/aggregates", "fields=name,uuid,space,node,home_node") logger.debug(__aggregates) __aggregates = __aggregates.get("records") if metric == MetricEnum.relative: diff --git a/src/main.py b/src/main.py index 1ee061a..0a40abc 100644 --- a/src/main.py +++ b/src/main.py @@ -20,7 +20,6 @@ logger.setLevel("DEBUG") logger.info("Starting application") - @asynccontextmanager async def lifespan(app: FastAPI): """make loading it async""" @@ -39,7 +38,7 @@ async def lifespan(app: FastAPI): log.error("Configuration initialization failed. Exiting...") # exit(1) requests_client = httpx.AsyncClient(verify=False) - yield + yield {"redis_conn": shared_redis_conn, "requests_client": requests_client} await requests_client.aclose() log.info("Shutting down FastAPI app...") diff --git a/src/utils.py b/src/utils.py index 8ab9e69..e6b590a 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,6 +1,8 @@ import logging +from fastapi import Request import httpx + def round_bytes(size_in_bytes: int) -> str: # Helper function to convert bytes to a human-readable format for unit in ["B", "KiB", "MiB", "GiB", "TiB", "PiB"]: @@ -10,11 +12,11 @@ def round_bytes(size_in_bytes: int) -> str: return f"{size_in_bytes:.2f}EB" -async def get_data_from_ontap(client, logger, hostname: str, username: str, password: str, endpoint: str, query_string: str = ""): +async def get_data_from_ontap(request: Request, logger, hostname: str, username: str, password: str, endpoint: str, query_string: str = ""): url = f"https://{hostname}/api/{endpoint}" if query_string: url += f"?{query_string}" - async with client as _client: + async with request.state.requests_client as _client: try: logger.debug(f"Fetching data from ONTAP: {url}") response = await _client.get(url, auth=(username, password)) @@ -24,10 +26,8 @@ async def get_data_from_ontap(client, logger, hostname: str, username: str, pass logger.error(f"HTTP error occurred: {e}") return None + def setup_logging() -> None: """Configure logging for the application""" - logging.basicConfig( - level=logging.DEBUG, - format="[%(asctime)s] [%(levelname)5s] %(message)s" - ) + logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] [%(levelname)5s] %(message)s") print(f"Logger is initialized.")