Co-authored-by: Claude (Opus 4.6) <claude@hermes.local> Co-committed-by: Claude (Opus 4.6) <claude@hermes.local>
41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
"""WebSocket emitter for the sovereignty metrics dashboard widget.
|
|
|
|
Streams real-time sovereignty snapshots to connected clients every
|
|
*_PUSH_INTERVAL* seconds. The snapshot includes per-layer sovereignty
|
|
percentages, API cost rate, and skill crystallisation count.
|
|
|
|
Refs: #954, #953
|
|
"""
|
|
|
|
import asyncio
|
|
import json
|
|
import logging
|
|
|
|
from fastapi import APIRouter, WebSocket
|
|
|
|
router = APIRouter(tags=["sovereignty"])
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_PUSH_INTERVAL = 5 # seconds between snapshot pushes
|
|
|
|
|
|
@router.websocket("/ws/sovereignty")
|
|
async def sovereignty_ws(websocket: WebSocket) -> None:
|
|
"""Stream sovereignty metric snapshots to the dashboard widget."""
|
|
from timmy.sovereignty.metrics import get_metrics_store
|
|
|
|
await websocket.accept()
|
|
logger.info("Sovereignty WS connected")
|
|
|
|
store = get_metrics_store()
|
|
try:
|
|
# Send initial snapshot immediately
|
|
await websocket.send_text(json.dumps(store.get_snapshot()))
|
|
|
|
while True:
|
|
await asyncio.sleep(_PUSH_INTERVAL)
|
|
await websocket.send_text(json.dumps(store.get_snapshot()))
|
|
except Exception:
|
|
logger.debug("Sovereignty WS disconnected")
|