Spaces:
Running
Running
| """DTO pubblico e sanificato dello stato del servizio. | |
| Questa route non legge agent_tasks, sessioni operative o log. La tabella | |
| public_dashboard_snapshot viene aggiornata dal backend con service_role e letta | |
| qui tramite una whitelist di campi. | |
| """ | |
| from __future__ import annotations | |
| import asyncio | |
| import logging | |
| from typing import Any | |
| from fastapi import APIRouter, HTTPException | |
| from .state import sb | |
| _logger = logging.getLogger("agente_ai.api.public_status") | |
| router = APIRouter(prefix="/api/public", tags=["public"]) | |
| _PUBLIC_FIELDS = ( | |
| "singleton,service_status,active_sessions,queued_tasks,in_progress_tasks," | |
| "app_version,updated_at" | |
| ) | |
| async def public_status() -> dict[str, Any]: | |
| """Restituisce esclusivamente lo snapshot deliberatamente pubblico.""" | |
| client = sb() | |
| if client is None: | |
| raise HTTPException(status_code=503, detail="Public status non configurato") | |
| def operation(): | |
| return client.table("public_dashboard_snapshot").select(_PUBLIC_FIELDS).eq("singleton", True).limit(1).execute() | |
| try: | |
| result = await asyncio.to_thread(operation) | |
| except Exception as exc: | |
| _logger.warning("public status snapshot unavailable: %s", type(exc).__name__) | |
| raise HTTPException(status_code=503, detail="Public status temporaneamente non disponibile") from exc | |
| row = (result.data or [None])[0] | |
| if not row: | |
| raise HTTPException(status_code=503, detail="Public status snapshot non inizializzato") | |
| return { | |
| "service_status": str(row.get("service_status") or "unknown"), | |
| "active_sessions": int(row.get("active_sessions") or 0), | |
| "queued_tasks": int(row.get("queued_tasks") or 0), | |
| "in_progress_tasks": int(row.get("in_progress_tasks") or 0), | |
| "app_version": row.get("app_version"), | |
| "updated_at": row.get("updated_at"), | |
| } | |