Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Use mock unified state for demo infrastructure charts

rcourtman1caa34536b40
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index fcc014aa12..b273f5001c 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -158,6 +158,12 @@ an add-only capacity posture.
snapshot must carry the same canonical resource types and display names as
`/api/resources` instead of briefly showing legacy host aliases before the
first websocket-backed refresh lands.
+ Chart-adjacent lifecycle reads in shared `internal/api/router.go` must obey
+ that same mock-aware unified snapshot boundary: demo `/api/charts` and
+ `/api/charts/infrastructure` payloads may not bypass
+ `GetUnifiedReadStateOrSnapshot()` and silently drop VMware-backed host rows
+ that the canonical mock estate already published through `/api/state` and
+ `/api/resources`.
Persisted legacy hosted quickstart model IDs are therefore not lifecycle
truth either: when shared settings helpers load or save historical
quickstart values, they must normalize them back to
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index 895e1ccae2..9e1c6607ff 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -281,6 +281,14 @@ when the disabled candidate no longer counts toward monitored-system capacity.
through `/api/resources/dashboard-summary` instead of reconstructing that
shell from the paginated `/api/resources` list payload or guessing how
dashboard trend identities map onto infrastructure chart series.
+37. Keep mock and demo chart reads on the same canonical unified snapshot as
+ the rest of the API surface. `internal/api/router.go`,
+ `internal/api/contract_test.go`, and chart consumers must route
+ `/api/charts`, `/api/charts/infrastructure`, and `/api/storage-charts`
+ through `GetUnifiedReadStateOrSnapshot()` whenever mock or demo
+ presentation is active, so VMware, storage, and infrastructure series stay
+ aligned with `/api/resources` and `/api/state` instead of drifting onto the
+ live store-backed graph.
## Forbidden Paths
@@ -2592,6 +2600,12 @@ That same resource-handler seed contract must also stay on canonical unified
resource ownership for tenant-scoped requests: once a tenant state provider
implements `UnifiedResourceSnapshotForTenant`, `/api/resources` may not fall
back to raw tenant `StateSnapshot` seeding when that unified seed is empty.
+That same mock/runtime contract now also governs chart payloads under
+`internal/api/router.go`: when demo or mock presentation is enabled,
+`/api/charts`, `/api/charts/infrastructure`, and `/api/storage-charts` must
+read through `GetUnifiedReadStateOrSnapshot()` so chart payloads use the same
+canonical mock unified-resource snapshot as `/api/resources` and `/api/state`
+instead of drifting onto the live store-backed graph.
Tenant AI service wiring now follows that same canonical ownership rule:
`internal/api/ai_handlers.go` may provide tenant `ReadState` and
tenant-scoped unified-resource providers, but it must not mint tenant snapshot
diff --git a/docs/release-control/v6/internal/subsystems/performance-and-scalability.md b/docs/release-control/v6/internal/subsystems/performance-and-scalability.md
index b518feee7f..3d06e671a0 100644
--- a/docs/release-control/v6/internal/subsystems/performance-and-scalability.md
+++ b/docs/release-control/v6/internal/subsystems/performance-and-scalability.md
@@ -199,6 +199,12 @@ regression protection.
callback behavior, or reuse those public auth endpoints as a justification
for relaxing the protected history payload budgets that belong elsewhere.
30. Keep dashboard summary-chart fetches scope-owned rather than page-churn-owned: `frontend-modern/src/hooks/useDashboardTrends.ts` must hydrate infrastructure and storage summaries once per org/range scope from the canonical summary caches and recompute card presentation locally as the compact dashboard overview changes, rather than refetching the infrastructure-summary transport in `frontend-modern/src/components/Infrastructure/useInfrastructureSummaryState.ts`, the dashboard storage-summary trend transport in `frontend-modern/src/utils/storageSummaryTrendCache.ts`, or the storage-page summary transport in `frontend-modern/src/utils/storageSummaryCache.ts` for every top-resource or card reshuffle on the same dashboard load. That dashboard infrastructure path must also request only the metrics it renders through the canonical infrastructure-summary route owned by `internal/api/router_routes_monitoring.go` and `internal/api/router.go`; the dashboard may not pay for disk or network summary series when it only renders CPU and memory. App-shell prewarm in `frontend-modern/src/useAppRuntimeState.ts` must not front-run that dashboard-specific route while the operator is already on the root dashboard route owned by `frontend-modern/src/App.tsx`.
+ The same hot path must keep mock/demo chart identity on the canonical
+ unified snapshot too: when mock mode is enabled, `internal/api/router.go`
+ may not read `/api/charts`, `/api/charts/infrastructure`, or
+ `/api/storage-charts` from the live store-backed `ReadState` if that would
+ omit provider-backed resources already present in the canonical mock
+ unified-resource graph.
The same app-shell bootstrap boundary now also governs demo-hidden
organization context. When presentation policy suppresses organization
chrome, `frontend-modern/src/useAppRuntimeState.ts` may retain a hidden
diff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.md
index 566c003880..473bc3ebf0 100644
--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md
+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md
@@ -99,6 +99,11 @@ querying, and the operator-facing storage health presentation layer.
recovery consumers must also wait for the first canonical REST snapshot
instead of painting thinner websocket transport rows first and then
rehydrating into a richer canonical shape a moment later.
+ Shared chart transports in `internal/api/router.go` must follow the same
+ rule in mock mode: `/api/storage-charts` and adjacent infrastructure chart
+ payloads must read through `GetUnifiedReadStateOrSnapshot()` so storage and
+ recovery consumers stay aligned with the canonical mock unified snapshot
+ instead of slipping onto the live store graph.
Storage and recovery consumers must also inherit the hook's canonical
`ResourceType` normalization for route/query filters, so storage subtypes
such as `physical_disk` stay on the same cache-backed snapshot instead of
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index 68492e3573..4a089a59ab 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"reflect"
+ "sort"
"strings"
"testing"
"time"
@@ -50,6 +51,15 @@ type resourceContractSnapshot struct {
Type string
}
+func sortedVMChartKeys(values map[string]VMChartData) []string {
+ keys := make([]string, 0, len(values))
+ for key := range values {
+ keys = append(keys, key)
+ }
+ sort.Strings(keys)
+ return keys
+}
+
type contractCapturingStreamingProvider struct {
lastRequest providers.ChatRequest
}
@@ -1039,6 +1049,74 @@ func TestContract_InfrastructureChartsHonorExplicitMetricFilters(t *testing.T) {
}
}
+func TestContract_MockChartRoutesUseCanonicalMockUnifiedReadStateForVMwareHosts(t *testing.T) {
+ prevMock := mock.IsMockEnabled()
+ mock.SetEnabled(true)
+ t.Cleanup(func() { mock.SetEnabled(prevMock) })
+
+ fixtures := vmware.DefaultFixtures()
+ if len(fixtures.Hosts) == 0 {
+ t.Fatal("expected default VMware fixtures to include at least one host")
+ }
+ expectedHostID := vmware.SourceID(fixtures.ConnectionID, "host", fixtures.Hosts[0].Host)
+
+ monitor, state, _ := newTestMonitor(t)
+ state.Hosts = []models.Host{{
+ ID: "live-store-host-1",
+ Hostname: "live-store-host-1",
+ CPUUsage: 11.0,
+ Memory: models.Memory{Usage: 22.0},
+ Disks: []models.Disk{{Usage: 33.0}},
+ Status: "online",
+ }}
+ syncTestResourceStore(t, monitor, state)
+
+ router := &Router{monitor: monitor}
+ for _, path := range []string{"/api/charts?range=5m", "/api/charts/infrastructure?range=5m"} {
+ req := httptest.NewRequest(http.MethodGet, path, nil)
+ rec := httptest.NewRecorder()
+
+ switch path {
+ case "/api/charts?range=5m":
+ router.handleCharts(rec, req)
+ case "/api/charts/infrastructure?range=5m":
+ router.handleInfrastructureCharts(rec, req)
+ default:
+ t.Fatalf("unexpected path %q", path)
+ }
+
+ if rec.Code != http.StatusOK {
+ t.Fatalf("%s status = %d, body=%s", path, rec.Code, rec.Body.String())
+ }
+
+ agentData := map[string]VMChartData{}
+ if path == "/api/charts?range=5m" {
+ var decoded ChartResponse
+ if err := json.Unmarshal(rec.Body.Bytes(), &decoded); err != nil {
+ t.Fatalf("decode %s: %v", path, err)
+ }
+ agentData = decoded.AgentData
+ } else {
+ var decoded InfrastructureChartsResponse
+ if err := json.Unmarshal(rec.Body.Bytes(), &decoded); err != nil {
+ t.Fatalf("decode %s: %v", path, err)
+ }
+ agentData = decoded.AgentData
+ }
+
+ series, ok := agentData[expectedHostID]
+ if !ok {
+ t.Fatalf("%s missing VMware host %q; got keys=%v", path, expectedHostID, sortedVMChartKeys(agentData))
+ }
+ if len(series["cpu"]) == 0 {
+ t.Fatalf("%s expected VMware host %q cpu series", path, expectedHostID)
+ }
+ if _, ok := agentData["live-store-host-1"]; ok {
+ t.Fatalf("%s unexpectedly used live store-only host instead of canonical mock snapshot; keys=%v", path, sortedVMChartKeys(agentData))
+ }
+ }
+}
+
func TestContract_WorkloadChartsCapLongRangeMixedCadenceByTime(t *testing.T) {
store := newTestMetricsStore(t)
monitor, state, _ := newTestMonitor(t)
diff --git a/internal/api/router.go b/internal/api/router.go
index 3bcca345f5..87a461d40f 100644
--- a/internal/api/router.go
+++ b/internal/api/router.go
@@ -4973,7 +4973,7 @@ func (r *Router) handleCharts(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Tenant monitor is not available", http.StatusInternalServerError)
return
}
- readState := monitor.GetUnifiedReadState()
+ readState := monitor.GetUnifiedReadStateOrSnapshot()
if readState == nil {
http.Error(w, "State unavailable", http.StatusInternalServerError)
return
@@ -6560,7 +6560,7 @@ func (r *Router) handleInfrastructureCharts(w http.ResponseWriter, req *http.Req
http.Error(w, "Tenant monitor is not available", http.StatusInternalServerError)
return
}
- readState := monitor.GetUnifiedReadState()
+ readState := monitor.GetUnifiedReadStateOrSnapshot()
if readState == nil {
http.Error(w, "State unavailable", http.StatusInternalServerError)
return
@@ -7821,7 +7821,7 @@ func (r *Router) handleStorageCharts(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Monitor not available", http.StatusInternalServerError)
return
}
- readState := monitor.GetUnifiedReadState()
+ readState := monitor.GetUnifiedReadStateOrSnapshot()
if readState == nil {
http.Error(w, "State unavailable", http.StatusInternalServerError)
return
diff --git a/internal/api/router_misc_additional_test.go b/internal/api/router_misc_additional_test.go
index 9f51298582..3703f3aa51 100644
--- a/internal/api/router_misc_additional_test.go
+++ b/internal/api/router_misc_additional_test.go
@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
+ "sort"
"strings"
"testing"
"time"
@@ -14,6 +15,7 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/monitoring"
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
+ "github.com/rcourtman/pulse-go-rewrite/internal/vmware"
)
func newTestMonitor(t *testing.T) (*monitoring.Monitor, *models.State, *monitoring.MetricsHistory) {
@@ -39,6 +41,15 @@ func syncTestResourceStore(t *testing.T, monitor *monitoring.Monitor, state *mod
setUnexportedField(t, monitor, "resourceStore", monitoring.ResourceStoreInterface(adapter))
}
+func mapKeys[V any](values map[string]V) []string {
+ keys := make([]string, 0, len(values))
+ for key := range values {
+ keys = append(keys, key)
+ }
+ sort.Strings(keys)
+ return keys
+}
+
func TestNormalizeMetricsHistoryResourceType_ContainerCanonicalTypes(t *testing.T) {
tests := []struct {
name string
@@ -500,6 +511,55 @@ func TestHandleCharts_StatsDebugMetadata(t *testing.T) {
}
}
+func TestHandleCharts_UsesCanonicalMockUnifiedReadStateForVMwareHosts(t *testing.T) {
+ prevMock := mock.IsMockEnabled()
+ mock.SetEnabled(true)
+ t.Cleanup(func() { mock.SetEnabled(prevMock) })
+
+ fixtures := vmware.DefaultFixtures()
+ if len(fixtures.Hosts) == 0 {
+ t.Fatal("expected default VMware fixtures to include at least one host")
+ }
+ expectedHostID := vmware.SourceID(fixtures.ConnectionID, "host", fixtures.Hosts[0].Host)
+
+ monitor, state, _ := newTestMonitor(t)
+ state.Hosts = []models.Host{{
+ ID: "live-store-host-1",
+ Hostname: "live-store-host-1",
+ CPUUsage: 11.0,
+ Memory: models.Memory{Usage: 22.0},
+ Disks: []models.Disk{{Usage: 33.0}},
+ Status: "online",
+ }}
+ syncTestResourceStore(t, monitor, state)
+
+ router := &Router{monitor: monitor}
+ req := httptest.NewRequest(http.MethodGet, "/api/charts?range=5m", nil)
+ rec := httptest.NewRecorder()
+
+ router.handleCharts(rec, req)
+
+ if rec.Code != http.StatusOK {
+ t.Fatalf("expected status %d, got %d: %s", http.StatusOK, rec.Code, rec.Body.String())
+ }
+
+ var decoded ChartResponse
+ if err := json.Unmarshal(rec.Body.Bytes(), &decoded); err != nil {
+ t.Fatalf("unmarshal ChartResponse: %v", err)
+ }
+
+ vmwareSeries, ok := decoded.AgentData[expectedHostID]
+ if !ok {
+ t.Fatalf("expected VMware host %q in full charts payload, got keys=%v", expectedHostID, mapKeys(decoded.AgentData))
+ }
+ if len(vmwareSeries["cpu"]) == 0 {
+ t.Fatalf("expected VMware host %q cpu series", expectedHostID)
+ }
+ if _, ok := decoded.AgentData["live-store-host-1"]; ok {
+ t.Fatalf("expected mock-aware full charts to ignore live store-only host, got keys=%v", mapKeys(decoded.AgentData))
+ }
+}
+
func TestHandleInfrastructureCharts_Lightweight(t *testing.T) {
monitor, state, _ := newTestMonitor(t)
state.Nodes = []models.Node{{
@@ -646,6 +706,55 @@ func TestHandleInfrastructureCharts_MetricFilter(t *testing.T) {
}
}
+func TestHandleInfrastructureCharts_UsesCanonicalMockUnifiedReadStateForVMwareHosts(t *testing.T) {
+ prevMock := mock.IsMockEnabled()
+ mock.SetEnabled(true)
+ t.Cleanup(func() { mock.SetEnabled(prevMock) })
+
+ fixtures := vmware.DefaultFixtures()
+ if len(fixtures.Hosts) == 0 {
+ t.Fatal("expected default VMware fixtures to include at least one host")
+ }
+ expectedHostID := vmware.SourceID(fixtures.ConnectionID, "host", fixtures.Hosts[0].Host)
+
+ monitor, state, _ := newTestMonitor(t)
+ state.Hosts = []models.Host{{
+ ID: "live-store-host-1",
+ Hostname: "live-store-host-1",
+ CPUUsage: 11.0,
+ Memory: models.Memory{Usage: 22.0},
+ Disks: []models.Disk{{Usage: 33.0}},
+ Status: "online",
+ }}
+ syncTestResourceStore(t, monitor, state)
+
+ router := &Router{monitor: monitor}
+ req := httptest.NewRequest(http.MethodGet, "/api/charts/infrastructure?range=5m", nil)
+ rec := httptest.NewRecorder()
+
+ router.handleInfrastructureCharts(rec, req)
+
+ if rec.Code != http.StatusOK {
+ t.Fatalf("expected status %d, got %d: %s", http.StatusOK, rec.Code, rec.Body.String())
+ }
+
+ var decoded InfrastructureChartsResponse
+ if err := json.Unmarshal(rec.Body.Bytes(), &decoded); err != nil {
+ t.Fatalf("unmarshal InfrastructureChartsResponse: %v", err)
+ }
+
+ vmwareSeries, ok := decoded.AgentData[expectedHostID]
+ if !ok {
+ t.Fatalf("expected VMware host %q in infrastructure charts, got keys=%v", expectedHostID, mapKeys(decoded.AgentData))
+ }
+ if len(vmwareSeries["cpu"]) == 0 {
+ t.Fatalf("expected VMware host %q cpu series", expectedHostID)
+ }
+ if _, ok := decoded.AgentData["live-store-host-1"]; ok {
+ t.Fatalf("expected mock-aware infrastructure charts to ignore live store-only host, got keys=%v", mapKeys(decoded.AgentData))
+ }
+}
+
func TestHandleWorkloadsSummaryCharts_AggregatesAndCounts(t *testing.T) {
monitor, state, _ := newTestMonitor(t)
state.Nodes = []models.Node{{