Compare commits
3 Commits
fc3f39c6ae
...
GET/aggreg
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b60383071a | ||
| 58f7c5c393 | |||
|
|
5dfba7416b |
@@ -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:
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
from src.config.router import router as config_router
|
||||
|
||||
__all__ = ["config_router"]
|
||||
@@ -1,2 +0,0 @@
|
||||
# contains the business logic for the config service
|
||||
async def save_config() -> None: ...
|
||||
3
src/config_upload/__init__.py
Normal file
3
src/config_upload/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from src.config_upload.router import router as config_router
|
||||
|
||||
__all__ = ["config_router"]
|
||||
@@ -3,10 +3,12 @@ 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")
|
||||
|
||||
router = APIRouter(tags=["config"])
|
||||
router = APIRouter(tags=["config_upload"])
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -20,3 +22,4 @@ async def create_config(config: ConfigSchema) -> ConfigSchema:
|
||||
"""
|
||||
logger.info("Received configuration data")
|
||||
return config
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# contains the schema definitions for the config service
|
||||
# contains the schema definitions for the config_upload service
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
2
src/config_upload/service.py
Normal file
2
src/config_upload/service.py
Normal file
@@ -0,0 +1,2 @@
|
||||
# contains the business logic for the config_upload service
|
||||
async def save_config() -> None: ...
|
||||
12
src/main.py
12
src/main.py
@@ -3,8 +3,11 @@ import logging
|
||||
import httpx
|
||||
from fastapi import FastAPI
|
||||
|
||||
shared_redis_conn = None
|
||||
requests_client = None
|
||||
|
||||
from src.aggregate import aggregate_router
|
||||
from src.config import config_router
|
||||
from src.config_upload import config_router
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
@@ -20,6 +23,7 @@ logger.info("Starting application")
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""make loading it async"""
|
||||
global shared_redis_conn, requests_client
|
||||
log = logging.getLogger("uvicorn")
|
||||
cfg_init_result = initialize_config()
|
||||
|
||||
@@ -33,9 +37,9 @@ async def lifespan(app: FastAPI):
|
||||
if not cfg_init_result:
|
||||
log.error("Configuration initialization failed. Exiting...")
|
||||
# exit(1)
|
||||
app.requests_client = httpx.AsyncClient(verify=False)
|
||||
yield
|
||||
await app.requests_client.aclose()
|
||||
requests_client = httpx.AsyncClient(verify=False)
|
||||
yield {"redis_conn": shared_redis_conn, "requests_client": requests_client}
|
||||
await requests_client.aclose()
|
||||
log.info("Shutting down FastAPI app...")
|
||||
|
||||
|
||||
|
||||
12
src/utils.py
12
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.")
|
||||
|
||||
Reference in New Issue
Block a user