File size: 1,900 Bytes
04bb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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()