From e8aa7d7df591ca06b8a2d6fc5e137128dcae79c7 Mon Sep 17 00:00:00 2001 From: "Magel, Denis" Date: Thu, 18 Sep 2025 15:36:39 +0200 Subject: [PATCH] feat: get cluster information from redis feat: loop over all clusters --- src/config_upload/router.py | 2 -- src/main.py | 3 --- src/utils.py | 29 +++++++++++++++++++---------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/config_upload/router.py b/src/config_upload/router.py index 3f17183..3fe2950 100644 --- a/src/config_upload/router.py +++ b/src/config_upload/router.py @@ -3,8 +3,6 @@ import logging from fastapi import APIRouter from .schema import ConfigReturnSchema, ConfigSchema -from src.database import get_config_from_db -from src.main import shared_redis_conn logger = logging.getLogger("uvicorn") diff --git a/src/main.py b/src/main.py index 0a40abc..65fb690 100644 --- a/src/main.py +++ b/src/main.py @@ -3,9 +3,6 @@ import logging import httpx from fastapi import FastAPI -shared_redis_conn = None -requests_client = None - from src.aggregate import aggregate_router from src.config_upload import config_router diff --git a/src/utils.py b/src/utils.py index e6b590a..84cec77 100644 --- a/src/utils.py +++ b/src/utils.py @@ -2,6 +2,8 @@ import logging from fastapi import Request import httpx +from src.database import get_config_from_db + def round_bytes(size_in_bytes: int) -> str: # Helper function to convert bytes to a human-readable format @@ -13,21 +15,28 @@ def round_bytes(size_in_bytes: int) -> str: async def get_data_from_ontap(request: Request, logger, hostname: str, username: str, password: str, endpoint: str, query_string: str = ""): + # get clusters from redis url = f"https://{hostname}/api/{endpoint}" if query_string: url += f"?{query_string}" - 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)) - response.raise_for_status() - return response.json() - except httpx.HTTPError as e: - logger.error(f"HTTP error occurred: {e}") - return None + + redis_conn = request.state.redis_conn + config = get_config_from_db(redis_conn) + logger.debug("Got the config from REDIS: %s", config) + + for cluster in config: + async with request.state.requests_client as _client: + try: + logger.debug(f"Fetching data from ONTAP: {url}") + response = await _client.get(url, auth=(cluster.username, cluster.password)) + response.raise_for_status() + return response.json() + except httpx.HTTPError as e: + 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") - print(f"Logger is initialized.") + print("Logger is initialized.")