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
|
# You can use the metric parameter to filter or modify results as needed
|
||||||
# For now, just return the same data and show metric usage
|
# For now, just return the same data and show metric usage
|
||||||
logger.debug(f"Metric used: {metric}")
|
logger.debug(f"Metric used: {metric}")
|
||||||
client = request.app.requests_client
|
__aggregates = await get_data_from_ontap(request, logger, "172.16.57.2", "admin", "Netapp12", "storage/aggregates", "fields=name,uuid,space,node,home_node")
|
||||||
__aggregates = await get_data_from_ontap(client, logger, "172.16.57.2", "admin", "Netapp12", "storage/aggregates", "fields=name,uuid,space,node,home_node")
|
|
||||||
logger.debug(__aggregates)
|
logger.debug(__aggregates)
|
||||||
__aggregates = __aggregates.get("records")
|
__aggregates = __aggregates.get("records")
|
||||||
if metric == MetricEnum.relative:
|
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 fastapi import APIRouter
|
||||||
|
|
||||||
from .schema import ConfigReturnSchema, ConfigSchema
|
from .schema import ConfigReturnSchema, ConfigSchema
|
||||||
|
from src.database import get_config_from_db
|
||||||
|
from src.main import shared_redis_conn
|
||||||
|
|
||||||
logger = logging.getLogger("uvicorn")
|
logger = logging.getLogger("uvicorn")
|
||||||
|
|
||||||
router = APIRouter(tags=["config"])
|
router = APIRouter(tags=["config_upload"])
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
@@ -20,3 +22,4 @@ async def create_config(config: ConfigSchema) -> ConfigSchema:
|
|||||||
"""
|
"""
|
||||||
logger.info("Received configuration data")
|
logger.info("Received configuration data")
|
||||||
return config
|
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
|
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
|
import httpx
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
shared_redis_conn = None
|
||||||
|
requests_client = None
|
||||||
|
|
||||||
from src.aggregate import aggregate_router
|
from src.aggregate import aggregate_router
|
||||||
from src.config import config_router
|
from src.config_upload import config_router
|
||||||
|
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
@@ -20,6 +23,7 @@ logger.info("Starting application")
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
"""make loading it async"""
|
"""make loading it async"""
|
||||||
|
global shared_redis_conn, requests_client
|
||||||
log = logging.getLogger("uvicorn")
|
log = logging.getLogger("uvicorn")
|
||||||
cfg_init_result = initialize_config()
|
cfg_init_result = initialize_config()
|
||||||
|
|
||||||
@@ -33,9 +37,9 @@ async def lifespan(app: FastAPI):
|
|||||||
if not cfg_init_result:
|
if not cfg_init_result:
|
||||||
log.error("Configuration initialization failed. Exiting...")
|
log.error("Configuration initialization failed. Exiting...")
|
||||||
# exit(1)
|
# exit(1)
|
||||||
app.requests_client = httpx.AsyncClient(verify=False)
|
requests_client = httpx.AsyncClient(verify=False)
|
||||||
yield
|
yield {"redis_conn": shared_redis_conn, "requests_client": requests_client}
|
||||||
await app.requests_client.aclose()
|
await requests_client.aclose()
|
||||||
log.info("Shutting down FastAPI app...")
|
log.info("Shutting down FastAPI app...")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
12
src/utils.py
12
src/utils.py
@@ -1,6 +1,8 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from fastapi import Request
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
|
|
||||||
def round_bytes(size_in_bytes: int) -> str:
|
def round_bytes(size_in_bytes: int) -> str:
|
||||||
# Helper function to convert bytes to a human-readable format
|
# Helper function to convert bytes to a human-readable format
|
||||||
for unit in ["B", "KiB", "MiB", "GiB", "TiB", "PiB"]:
|
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"
|
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}"
|
url = f"https://{hostname}/api/{endpoint}"
|
||||||
if query_string:
|
if query_string:
|
||||||
url += f"?{query_string}"
|
url += f"?{query_string}"
|
||||||
async with client as _client:
|
async with request.state.requests_client as _client:
|
||||||
try:
|
try:
|
||||||
logger.debug(f"Fetching data from ONTAP: {url}")
|
logger.debug(f"Fetching data from ONTAP: {url}")
|
||||||
response = await _client.get(url, auth=(username, password))
|
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}")
|
logger.error(f"HTTP error occurred: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def setup_logging() -> None:
|
def setup_logging() -> None:
|
||||||
"""Configure logging for the application"""
|
"""Configure logging for the application"""
|
||||||
logging.basicConfig(
|
logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] [%(levelname)5s] %(message)s")
|
||||||
level=logging.DEBUG,
|
|
||||||
format="[%(asctime)s] [%(levelname)5s] %(message)s"
|
|
||||||
)
|
|
||||||
print(f"Logger is initialized.")
|
print(f"Logger is initialized.")
|
||||||
|
|||||||
Reference in New Issue
Block a user