"""Node health endpoints for the Farm Manager API server.""" import asyncio import httpx from fastapi import APIRouter from server.config import get_nodes router = APIRouter() async def _check_node_health(http_client: httpx.AsyncClient, node: dict) -> dict: """Query a single agent's /health endpoint and return a NodeStatus dict.""" url = f"http://{node['host']}:{node['agent_port']}/health" try: resp = await http_client.get(url, timeout=5.0) resp.raise_for_status() data = resp.json() return { "name": node["name"], "host": node["host"], "healthy": True, "containers_total": data.get("containers_total", 0), "error": None, } except Exception as exc: return { "name": node["name"], "host": node["host"], "healthy": False, "containers_total": 0, "error": str(exc), } @router.get("/api/nodes") async def list_nodes(): """Return all configured nodes with their current health status.""" nodes = get_nodes() async with httpx.AsyncClient() as client: tasks = [_check_node_health(client, node) for node in nodes] results = await asyncio.gather(*tasks) return list(results)