feat: host heartbeat status and dashboard metrics (2.2)

Track agent.heartbeat and report.daily.ssh per host; show online/stale
on Hosts UI; dashboard counters and SAC_HEARTBEAT_STALE_MINUTES config.
This commit is contained in:
2026-05-27 13:36:17 +10:00
parent 9ba3c4b63b
commit 04301ab359
14 changed files with 246 additions and 4 deletions
+17
View File
@@ -3,9 +3,16 @@ from sqlalchemy import func, select
from sqlalchemy.orm import Session
from app.auth.jwt_auth import get_current_user
from app.config import get_settings
from app.database import get_db
from app.models import Event, Host
from app.schemas.list_models import HostListResponse, HostSummary
from app.services.host_health import (
DAILY_REPORT_SSH,
HEARTBEAT_TYPE,
agent_status,
max_event_time_by_host,
)
router = APIRouter(prefix="/hosts", tags=["hosts"])
@@ -37,11 +44,16 @@ def list_hosts(
.limit(page_size)
).all()
settings = get_settings()
hb_map = max_event_time_by_host(db, HEARTBEAT_TYPE)
report_map = max_event_time_by_host(db, DAILY_REPORT_SSH)
items: list[HostSummary] = []
for host in rows:
event_count = db.scalar(
select(func.count()).select_from(Event).where(Event.host_id == host.id)
)
last_hb = hb_map.get(host.id)
items.append(
HostSummary(
id=host.id,
@@ -53,6 +65,11 @@ def list_hosts(
ipv4=host.ipv4,
last_seen_at=host.last_seen_at,
event_count=int(event_count or 0),
last_heartbeat_at=last_hb,
last_daily_report_at=report_map.get(host.id),
agent_status=agent_status(
last_hb, stale_minutes=settings.sac_heartbeat_stale_minutes
),
)
)