| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- // health.go — GET /api/health (público, contrato) y GET /health (SPEC §2.1/§2.2).
- package httpapi
- import (
- "net/http"
- "runtime"
- "time"
- )
- // handleAPIHealth: {ok, version, mode, uptimeSec, db, agentsConnected,
- // sseConnections, devicesTotal}.
- func (s *server) handleAPIHealth(mode string) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- dbOk := s.db.CheckHealth()
- dbStatus := "ok"
- if !dbOk {
- dbStatus = "error"
- }
- // Métricas operativas (Fase 8): agentes vivos (dentro de TTL),
- // clientes SSE conectados y total de dispositivos del último
- // overview del poller. Nil-safe: demo/sin datos → 0.
- agentsConnected := 0
- if s.agents != nil {
- agentsConnected = s.agents.ActiveCount()
- }
- sseConnections := 0
- if s.hub != nil {
- sseConnections = s.hub.Size()
- }
- // #681: los agentes conectan por el AgentHub (SSE bidireccional),
- // distinto del hub de UI. Sin este conteo, con toda la flota de
- // agentes empujando el health solo refleja las pestañas abiertas.
- agentSseConnections := 0
- if s.agentHub != nil {
- agentSseConnections = len(s.agentHub.ConnectedSlugs())
- }
- devicesTotal := 0
- if s.lastOv != nil {
- if ov := s.lastOv(); ov != nil {
- devicesTotal = ov.DeviceTotals.Total
- }
- }
- writeJSON(w, http.StatusOK, map[string]any{
- "ok": dbOk,
- "version": Version,
- "mode": mode,
- "uptimeSec": int64(time.Since(s.started).Seconds()),
- "db": dbStatus,
- "agentsConnected": agentsConnected,
- "sseConnections": sseConnections,
- "agentSseConnections": agentSseConnections,
- "devicesTotal": devicesTotal,
- })
- }
- }
- // handleHealth: {status, uptime (float segundos), memory:{rss, heap}, db}.
- // En Go los bytes salen de runtime.ReadMemStats: rss ≈ Sys (memoria obtenida
- // del SO) y heap ≈ HeapAlloc. Los valores absolutos cambian de lenguaje —
- // solo el shape es contrato (SPEC §2.2).
- func (s *server) handleHealth(w http.ResponseWriter, r *http.Request) {
- dbOk := s.db.CheckHealth()
- status, dbStatus := "ok", "connected"
- if !dbOk {
- status, dbStatus = "degraded", "error"
- }
- var mem runtime.MemStats
- runtime.ReadMemStats(&mem)
- writeJSON(w, http.StatusOK, map[string]any{
- "status": status,
- "uptime": time.Since(s.started).Seconds(),
- "memory": map[string]any{
- "rss": mem.Sys,
- "heap": mem.HeapAlloc,
- },
- "db": dbStatus,
- })
- }
|