62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.api.deps import get_session
|
|
from app.core.constants import Role
|
|
from app.core.dependencies import get_current_user_id, require_role
|
|
from app.schemas.monitoring import AlertRead, AlertRuleCreate, AlertRuleRead, SystemHealthResponse
|
|
from app.services.monitoring_service import MonitoringService
|
|
|
|
router = APIRouter(prefix="/monitoring", tags=["monitoring"])
|
|
|
|
|
|
@router.get("/health", response_model=SystemHealthResponse)
|
|
async def system_health(
|
|
_: dict = Depends(require_role(*Role.MANAGEMENT_ROLES)),
|
|
session: AsyncSession = Depends(get_session),
|
|
) -> SystemHealthResponse:
|
|
service = MonitoringService(session)
|
|
return await service.get_system_health()
|
|
|
|
|
|
@router.get("/alerts", response_model=list[AlertRead])
|
|
async def list_alerts(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(50, ge=1, le=200),
|
|
_: dict = Depends(require_role(*Role.MANAGEMENT_ROLES)),
|
|
session: AsyncSession = Depends(get_session),
|
|
) -> list[AlertRead]:
|
|
service = MonitoringService(session)
|
|
return await service.list_active_alerts(skip=skip, limit=limit)
|
|
|
|
|
|
@router.post("/alerts/{alert_id}/acknowledge", response_model=AlertRead)
|
|
async def acknowledge_alert(
|
|
alert_id: int,
|
|
user_id: int = Depends(get_current_user_id),
|
|
session: AsyncSession = Depends(get_session),
|
|
) -> AlertRead:
|
|
service = MonitoringService(session)
|
|
return await service.acknowledge_alert(alert_id, user_id)
|
|
|
|
|
|
@router.get("/alert-rules", response_model=list[AlertRuleRead])
|
|
async def list_alert_rules(
|
|
_: dict = Depends(require_role(*Role.MANAGEMENT_ROLES)),
|
|
session: AsyncSession = Depends(get_session),
|
|
) -> list[AlertRuleRead]:
|
|
service = MonitoringService(session)
|
|
return await service.list_alert_rules()
|
|
|
|
|
|
@router.post("/alert-rules", response_model=AlertRuleRead, status_code=201)
|
|
async def create_alert_rule(
|
|
body: AlertRuleCreate,
|
|
user_id: int = Depends(get_current_user_id),
|
|
session: AsyncSession = Depends(get_session),
|
|
) -> AlertRuleRead:
|
|
service = MonitoringService(session)
|
|
return await service.create_alert_rule(body, user_id)
|