44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
from datetime import UTC, datetime
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from ..db import get_session
|
|
from ..models import Agent, Check, Incident
|
|
from ..schemas import SystemSummaryResponse
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/system-summary", response_model=SystemSummaryResponse)
|
|
async def system_summary(session: AsyncSession = Depends(get_session)) -> SystemSummaryResponse:
|
|
agents_rows = (
|
|
await session.execute(
|
|
select(Agent.status, func.count())
|
|
.where(Agent.accepted_key_hash.is_not(None))
|
|
.group_by(Agent.status)
|
|
)
|
|
).all()
|
|
checks_rows = (
|
|
await session.execute(select(Check.status, func.count()).group_by(Check.status))
|
|
).all()
|
|
open_incidents = (
|
|
await session.execute(
|
|
select(func.count()).select_from(Incident).where(Incident.state == "open")
|
|
)
|
|
).scalar_one()
|
|
|
|
agents = {status: count for status, count in agents_rows}
|
|
checks = {status: count for status, count in checks_rows}
|
|
return SystemSummaryResponse(
|
|
agents_online=agents.get("alive", 0),
|
|
agents_offline=agents.get("stale", 0) + agents.get("dead", 0),
|
|
checks_ok=checks.get("ok", 0),
|
|
checks_warning=checks.get("warning", 0),
|
|
checks_critical=checks.get("critical", 0),
|
|
checks_unknown=checks.get("unknown", 0),
|
|
open_incidents=open_incidents,
|
|
generated_at=datetime.now(UTC),
|
|
)
|