Explorar el Código

fix(vitals): stop persisting the zero of a missing sensor

The SSH path never set hasCpu/hasRam/hasTemp, so a unit without a thermal zone stored 0 in metrics and the chart drew a flat zero. The poll now marks what it read and an unconfirmed zero is dropped.

Fixes #1947
Nacho hace 3 semanas
padre
commit
255f98d9da

+ 25 - 4
server-go/internal/adapters/live.go

@@ -1065,6 +1065,26 @@ func (l *Live) getAdguardClient() (std *AdGuardClient, gl *AdGuardGlinetClient)
 	return l.agStd, l.agGL
 }
 
+// vitalInt descarta un CERO no confirmado (#1947): sin hasTemp/hasCpu/hasRam
+// el 0 es "sin sensor", no una lectura. Un valor distinto de cero se mantiene
+// para no romper fuentes que no marcan el flag.
+func vitalInt(ok bool, v int) *int {
+	if !ok && v == 0 {
+		return nil
+	}
+	return &v
+}
+
+// vitalFloat es la variante para metrics: el cero sin confirmar no se
+// persiste, que era lo que dejaba la grafica de temperatura plana en 0.
+func vitalFloat(ok bool, v int) *float64 {
+	if !ok && v == 0 {
+		return nil
+	}
+	f := float64(v)
+	return &f
+}
+
 // histPoint es un punto de metricsHistory {t, down, up, cpu, ram, temp}.
 type histPoint struct {
 	t              string
@@ -1430,6 +1450,7 @@ func (l *Live) pollRouter(ctx context.Context, cfg RouterConfig) (*routerPolled,
 	return &routerPolled{
 		cfg: cfg, client: client, sysInfo: sysInfo, board: board,
 		cpu: cpuV, ram: ramPct, temp: tempV,
+		hasCpu: cpu != nil, hasRam: mem.Total > 0, hasTemp: temp != nil,
 		uptimeSec: sysInfo.Uptime, net: net, leases: leases, reservations: reservations, arp: arp, arpStale: arpStale, arpDevs: arpDevs, glClients: glClients,
 		wireless: wirelessGood, ports: portsGood, radios: radiosGood,
 		fdb: fdbGood, fdbVlans: fdbVlansGood, wirelessSide: wsGood, brMac: brMac, latencyMs: latencyMs, lossPct: lossPct,
@@ -1699,7 +1720,7 @@ func (l *Live) buildRouter(p *routerPolled, history []histPoint) Router {
 	r := Router{
 		ID: p.cfg.ID, Name: name, Model: model, ModelShort: model,
 		IP: p.cfg.Host, Status: status, Health: health,
-		CPU: iptr(p.cpu), RAM: iptr(p.ram), Temp: iptr(p.temp),
+		CPU: vitalInt(p.hasCpu, p.cpu), RAM: vitalInt(p.hasRam, p.ram), Temp: vitalInt(p.hasTemp, p.temp),
 		Uptime: uptimeLabel(p), Clients: len(p.leases),
 		Sparkline: sparkline, TempThreshold: thr,
 		LatencyMs: p.latencyMs,
@@ -5019,9 +5040,9 @@ func (l *Live) GetMetricsRows(context.Context) []MetricsRow {
 		}
 		row := MetricsRow{
 			RouterID:  id,
-			CPU:       fptr(float64(p.cpu)),
-			RAM:       fptr(float64(p.ram)),
-			Temp:      fptr(float64(p.temp)),
+			CPU:       vitalFloat(p.hasCpu, p.cpu),
+			RAM:       vitalFloat(p.hasRam, p.ram),
+			Temp:      vitalFloat(p.hasTemp, p.temp),
 			LatencyMs: p.latencyMs,
 		}
 		// #441/#1568: sin lecturas no se persisten ceros falsos, pero una

+ 58 - 0
server-go/internal/adapters/vitals_1947_test.go

@@ -0,0 +1,58 @@
+// vitals_1947_test.go - una lectura no confirmada no vale cero: la grafica de
+// temperatura del WNDR3700v2 salia plana a 0 C porque el poll guardaba ceros
+// de relleno cuando el sensor no existia (#1947).
+package adapters
+
+import (
+	"context"
+	"testing"
+)
+
+func TestBuildRouterDropsUnconfirmedTemp(t *testing.T) {
+	l := NewLive(nil, nil, []RouterConfig{
+		{ID: "rt1", Name: "test-gw", Host: "192.168.1.50"},
+	}, nil)
+
+	// Unidad sin sensor: temp 0 y hasTemp false.
+	p := &routerPolled{cfg: RouterConfig{ID: "rt1", Name: "test-gw"}, temp: 0, hasTemp: false}
+	r := l.buildRouter(p, nil)
+	if r.Temp != nil {
+		t.Fatalf("Temp = %v, esperaba nil sin lectura confirmada", *r.Temp)
+	}
+
+	// Sensor real: se conserva el valor.
+	p2 := &routerPolled{cfg: RouterConfig{ID: "rt1", Name: "test-gw"}, temp: 43, hasTemp: true}
+	r2 := l.buildRouter(p2, nil)
+	if r2.Temp == nil || *r2.Temp != 43 {
+		t.Fatalf("Temp = %v, esperaba 43", r2.Temp)
+	}
+}
+
+func TestMetricsRowsDropUnconfirmedVitals(t *testing.T) {
+	l := NewLive(nil, nil, nil, nil)
+	l.lastPolled = map[string]*routerPolled{
+		"no-sensor": {cfg: RouterConfig{ID: "no-sensor"}, cpu: 0, ram: 0, temp: 0},
+		"with-sensor": {cfg: RouterConfig{ID: "with-sensor"},
+			cpu: 12, hasCpu: true, ram: 40, hasRam: true, temp: 43, hasTemp: true},
+	}
+
+	rows := l.GetMetricsRows(context.Background())
+	if len(rows) != 2 {
+		t.Fatalf("filas: %d", len(rows))
+	}
+	for _, row := range rows {
+		switch row.RouterID {
+		case "no-sensor":
+			if row.Temp != nil || row.CPU != nil || row.RAM != nil {
+				t.Fatalf("sin sensor se persistieron ceros: %+v", row)
+			}
+		case "with-sensor":
+			if row.Temp == nil || *row.Temp != 43 {
+				t.Fatalf("temp confirmada: %+v", row.Temp)
+			}
+			if row.CPU == nil || *row.CPU != 12 || row.RAM == nil || *row.RAM != 40 {
+				t.Fatalf("vitals confirmadas: %+v", row)
+			}
+		}
+	}
+}