# Copyright (c) 2026 CrewHub. All rights reserved. # Proprietary and confidential. See LICENSE for details. """Admin infrastructure usage monitoring endpoints.""" from fastapi import APIRouter, Depends, Query from sqlalchemy.ext.asyncio import AsyncSession from src.api.admin import require_admin, require_super_admin from src.database import get_db from src.schemas.infrastructure import ( InfrastructureCurrentResponse, InfrastructureHistoryResponse, ) from src.services.infrastructure_monitor import InfrastructureMonitorService router = APIRouter(prefix="/admin/infrastructure", tags=["admin-infrastructure"]) @router.get("/current", response_model=InfrastructureCurrentResponse) async def get_current_metrics( admin=Depends(require_admin), db: AsyncSession = Depends(get_db), ): """Get the latest infrastructure usage snapshot for all metrics.""" service = InfrastructureMonitorService(db) return await service.get_current() @router.get("/history", response_model=InfrastructureHistoryResponse) async def get_metric_history( metric: str = Query(..., description="Metric key, e.g. db_storage_bytes"), days: int = Query(30, ge=1, le=365), admin=Depends(require_admin), db: AsyncSession = Depends(get_db), ): """Get daily snapshots for a single metric over N days.""" service = InfrastructureMonitorService(db) return await service.get_history(metric, days) @router.post("/collect", response_model=InfrastructureCurrentResponse) async def trigger_collection( admin=Depends(require_super_admin), db: AsyncSession = Depends(get_db), ): """Manually trigger metric collection (super_admin only).""" service = InfrastructureMonitorService(db) results = await service.collect_all() await service.send_daily_report(results) await service.check_threshold_crossings(results) return await service.get_current()