feat: get cluster information from redis
feat: loop over all clusters
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
29
src/utils.py
29
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.")
|
||||
|
||||
Reference in New Issue
Block a user