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.

fix: rebuild unified-seed source mappings for dashboard trends

rcourtmane1c852350ea5
diff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index fca7e98bff..4aa96989e0 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -499,6 +499,13 @@ must preserve the canonical `MetricsTarget` already attached to provider-backed
agents or app-containers, and typed views must expose that stored target
unchanged so shared chart routes keep using canonical IDs across live and demo
projections.
+Seeded unified-resource snapshots are part of that same registry ownership.
+`internal/unifiedresources/registry.go` must rebuild the canonical `bySource`
+index from source-owned facets already present on seeded unified resources
+before manual links, overlays, or metrics-target resolution run, so
+`/api/resources/dashboard-summary`, `/api/resources`, dashboard trend joins,
+and other chart consumers keep resolving history through canonical source
+coordinates instead of silently falling back to hashed unified resource IDs.
That same registry/view boundary now also applies to provider-backed storage.
`internal/unifiedresources/registry.go` must attach the resolved
`MetricsTarget` onto cached view clones before `ReadState` exposes
diff --git a/internal/api/resources_test.go b/internal/api/resources_test.go
index be360302f1..e410ba5ce4 100644
--- a/internal/api/resources_test.go
+++ b/internal/api/resources_test.go
@@ -3068,6 +3068,67 @@ func TestResourceDashboardSummaryUsesCompactGovernedPayload(t *testing.T) {
}
}
+func TestResourceDashboardSummaryHydratesMetricsTargetsFromUnifiedSeedResources(t *testing.T) {
+ now := time.Now().UTC()
+
+ cfg := &config.Config{DataPath: t.TempDir()}
+ h := NewResourceHandlers(cfg)
+ h.SetStateProvider(resourceUnifiedSeedProvider{
+ snapshot: models.StateSnapshot{LastUpdate: now},
+ resources: []unified.Resource{
+ {
+ ID: "agent-dashboard-1",
+ Type: unified.ResourceTypeAgent,
+ Name: "delly",
+ Status: unified.StatusOnline,
+ LastSeen: now,
+ UpdatedAt: now,
+ Sources: []unified.DataSource{unified.SourceProxmox, unified.SourceAgent},
+ Metrics: &unified.ResourceMetrics{
+ CPU: &unified.MetricValue{Percent: 91},
+ Memory: &unified.MetricValue{Percent: 63},
+ },
+ Identity: unified.ResourceIdentity{
+ MachineID: "machine-delly",
+ Hostnames: []string{"delly"},
+ },
+ Proxmox: &unified.ProxmoxData{
+ SourceID: "homelab-delly",
+ NodeName: "delly",
+ Instance: "homelab-entry",
+ },
+ Agent: &unified.AgentData{
+ AgentID: "agent-source-delly",
+ Hostname: "delly",
+ },
+ },
+ },
+ })
+
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodGet, "/api/resources/dashboard-summary", nil)
+ h.HandleDashboardSummary(rec, req)
+
+ if rec.Code != http.StatusOK {
+ t.Fatalf("status = %d, body=%s", rec.Code, rec.Body.String())
+ }
+
+ var resp DashboardOverviewSummaryResponse
+ if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
+ t.Fatalf("decode response: %v", err)
+ }
+
+ if len(resp.Infrastructure.TopCPU) != 1 {
+ t.Fatalf("topCPU len = %d, want 1", len(resp.Infrastructure.TopCPU))
+ }
+ if resp.Infrastructure.TopCPU[0].MetricsTarget == nil {
+ t.Fatalf("expected dashboard summary metrics target, got %+v", resp.Infrastructure.TopCPU[0])
+ }
+ if got := *resp.Infrastructure.TopCPU[0].MetricsTarget; got != (unified.MetricsTarget{ResourceType: "agent", ResourceID: "homelab-delly"}) {
+ t.Fatalf("topCPU[0].metricsTarget = %+v, want agent/homelab-delly", got)
+ }
+}
+
func TestResourceListIncludesTrueNASPhysicalDiskTemperature(t *testing.T) {
previous := truenas.IsFeatureEnabled()
truenas.SetFeatureEnabled(true)
diff --git a/internal/unifiedresources/registry.go b/internal/unifiedresources/registry.go
index 5cb6b48a7a..5b517e12ff 100644
--- a/internal/unifiedresources/registry.go
+++ b/internal/unifiedresources/registry.go
@@ -276,6 +276,7 @@ func (rr *ResourceRegistry) IngestRecords(source DataSource, records []IngestRec
// This is used when a caller already has a canonical unified read model and
// only needs store-backed manual links/exclusions applied on top.
func (rr *ResourceRegistry) IngestResources(resources []Resource) {
+ seededIDs := make([]string, 0, len(resources))
for _, incoming := range resources {
resource := cloneResourcePtr(&incoming)
if resource == nil {
@@ -302,9 +303,14 @@ func (rr *ResourceRegistry) IngestResources(resources []Resource) {
rr.matcher.Add(resource.ID, resource.Identity)
rr.viewsDirty = true
rr.mu.Unlock()
+
+ seededIDs = append(seededIDs, resource.ID)
}
rr.mu.Lock()
+ for _, resourceID := range seededIDs {
+ rr.seedSourceMappingsFromResourceLocked(rr.resources[resourceID])
+ }
rr.applyManualLinks()
rr.refreshStorageConsumersLocked()
rr.refreshPBSRollupsLocked()
@@ -316,6 +322,276 @@ func (rr *ResourceRegistry) IngestResources(resources []Resource) {
rr.mu.Unlock()
}
+func (rr *ResourceRegistry) seedSourceMappingsFromResourceLocked(resource *Resource) {
+ if resource == nil {
+ return
+ }
+
+ sources := make([]DataSource, 0, len(resource.Sources)+len(resource.SourceStatus))
+ seen := make(map[DataSource]struct{}, len(resource.Sources)+len(resource.SourceStatus))
+ for _, source := range resource.Sources {
+ if _, ok := seen[source]; ok {
+ continue
+ }
+ seen[source] = struct{}{}
+ sources = append(sources, source)
+ }
+ for source := range resource.SourceStatus {
+ if _, ok := seen[source]; ok {
+ continue
+ }
+ seen[source] = struct{}{}
+ sources = append(sources, source)
+ }
+ sort.Slice(sources, func(i, j int) bool {
+ return string(sources[i]) < string(sources[j])
+ })
+
+ for _, source := range sources {
+ sourceID := rr.seedSourceIDForResourceLocked(resource, source)
+ sourceID = normalizeSourceID(sourceID)
+ if sourceID == "" {
+ continue
+ }
+ if _, ok := rr.bySource[source]; !ok {
+ rr.bySource[source] = make(map[string]string)
+ }
+ rr.bySource[source][sourceID] = resource.ID
+ }
+}
+
+func (rr *ResourceRegistry) seedSourceIDForResourceLocked(resource *Resource, source DataSource) string {
+ if resource == nil {
+ return ""
+ }
+
+ switch source {
+ case SourceProxmox:
+ if resource.Proxmox != nil {
+ if sourceID := strings.TrimSpace(resource.Proxmox.SourceID); sourceID != "" {
+ return sourceID
+ }
+ if CanonicalResourceType(resource.Type) == ResourceTypeAgent {
+ return proxmoxNodeSourceID(
+ strings.TrimSpace(resource.Proxmox.Instance),
+ strings.TrimSpace(resource.Proxmox.NodeName),
+ )
+ }
+ }
+ if resource.Ceph != nil {
+ return strings.TrimSpace(resource.Ceph.FSID)
+ }
+ case SourceAgent:
+ if resource.Agent != nil {
+ if sourceID := strings.TrimSpace(resource.Agent.AgentID); sourceID != "" {
+ return sourceID
+ }
+ }
+ switch CanonicalResourceType(resource.Type) {
+ case ResourceTypeStorage:
+ if resource.Storage != nil && strings.EqualFold(resource.Storage.Type, "unraid-array") {
+ if parentSourceID := rr.seedParentSourceIDLocked(resource, SourceAgent); parentSourceID != "" {
+ return parentSourceID + "/storage:unraid-array"
+ }
+ }
+ case ResourceTypePhysicalDisk:
+ if resource.PhysicalDisk == nil {
+ return ""
+ }
+ fallback := ""
+ if parentSourceID := rr.seedParentSourceIDLocked(resource, SourceAgent); parentSourceID != "" {
+ device := strings.TrimSpace(strings.TrimPrefix(resource.PhysicalDisk.DevPath, "/dev/"))
+ if device != "" {
+ fallback = fmt.Sprintf("%s:%s", parentSourceID, device)
+ }
+ }
+ return PreferredPhysicalDiskMetricID(resource.PhysicalDisk.Serial, resource.PhysicalDisk.WWN, fallback)
+ }
+ case SourceDocker:
+ if resource.Docker == nil {
+ return ""
+ }
+ switch CanonicalResourceType(resource.Type) {
+ case ResourceTypeAgent:
+ return strings.TrimSpace(resource.Docker.HostSourceID)
+ case ResourceTypeAppContainer:
+ return strings.TrimSpace(resource.Docker.ContainerID)
+ case ResourceTypeDockerService:
+ clusterKey := dockerSwarmClusterKeyFromMeta(resource.Docker.Swarm)
+ if clusterKey == "" {
+ return ""
+ }
+ serviceID := strings.TrimSpace(resource.Docker.ServiceID)
+ if serviceID == "" {
+ serviceID = strings.TrimSpace(resource.Name)
+ }
+ if serviceID == "" {
+ return ""
+ }
+ return fmt.Sprintf("%s:service:%s", clusterKey, serviceID)
+ default:
+ return strings.TrimSpace(resource.Docker.HostSourceID)
+ }
+ case SourcePBS:
+ if resource.PBS != nil {
+ if sourceID := strings.TrimSpace(resource.PBS.InstanceID); sourceID != "" {
+ return sourceID
+ }
+ }
+ if CanonicalResourceType(resource.Type) == ResourceTypeStorage && resource.Storage != nil && strings.EqualFold(resource.Storage.Platform, "pbs") {
+ parentSourceID := rr.seedParentSourceIDLocked(resource, SourcePBS)
+ if parentSourceID == "" {
+ return ""
+ }
+ datastoreName := strings.TrimSpace(resource.Name)
+ if datastoreName == "" {
+ return parentSourceID
+ }
+ return parentSourceID + "/" + datastoreName
+ }
+ case SourcePMG:
+ if resource.PMG != nil {
+ return strings.TrimSpace(resource.PMG.InstanceID)
+ }
+ case SourceK8s:
+ clusterSourceID := seededKubernetesClusterSourceID(resource.Kubernetes)
+ switch CanonicalResourceType(resource.Type) {
+ case ResourceTypeK8sCluster:
+ return clusterSourceID
+ case ResourceTypeAgent, ResourceTypeK8sNode:
+ nodeID := seededKubernetesNodeIdentity(resource.Kubernetes)
+ if clusterSourceID == "" || nodeID == "" {
+ return ""
+ }
+ return fmt.Sprintf("%s:node:%s", clusterSourceID, nodeID)
+ case ResourceTypePod:
+ podID := seededKubernetesPodIdentity(resource)
+ if clusterSourceID == "" || podID == "" {
+ return ""
+ }
+ return fmt.Sprintf("%s:pod:%s", clusterSourceID, podID)
+ case ResourceTypeK8sDeployment:
+ deploymentID := seededKubernetesDeploymentIdentity(resource)
+ if clusterSourceID == "" || deploymentID == "" {
+ return ""
+ }
+ return fmt.Sprintf("%s:deployment:%s", clusterSourceID, deploymentID)
+ }
+ case SourceVMware:
+ return seededVMwareSourceID(resource)
+ }
+
+ return ""
+}
+
+func (rr *ResourceRegistry) seedParentSourceIDLocked(resource *Resource, source DataSource) string {
+ if resource == nil || resource.ParentID == nil {
+ return ""
+ }
+
+ parentID := CanonicalResourceID(strings.TrimSpace(*resource.ParentID))
+ if parentID == "" {
+ return ""
+ }
+
+ parent := rr.resources[parentID]
+ return rr.seedSourceIDForResourceLocked(parent, source)
+}
+
+func dockerSwarmClusterKeyFromMeta(swarm *DockerSwarmInfo) string {
+ if swarm == nil {
+ return ""
+ }
+ if clusterID := strings.TrimSpace(swarm.ClusterID); clusterID != "" {
+ return clusterID
+ }
+ return strings.TrimSpace(swarm.ClusterName)
+}
+
+func seededKubernetesClusterSourceID(kubernetes *K8sData) string {
+ if kubernetes == nil {
+ return ""
+ }
+ for _, candidate := range []string{
+ kubernetes.ClusterID,
+ kubernetes.AgentID,
+ kubernetes.SourceName,
+ kubernetes.ClusterName,
+ kubernetes.Context,
+ } {
+ if value := strings.TrimSpace(candidate); value != "" {
+ return value
+ }
+ }
+ return ""
+}
+
+func seededKubernetesNodeIdentity(kubernetes *K8sData) string {
+ if kubernetes == nil {
+ return ""
+ }
+ if nodeUID := strings.TrimSpace(kubernetes.NodeUID); nodeUID != "" {
+ return nodeUID
+ }
+ return strings.TrimSpace(kubernetes.NodeName)
+}
+
+func seededKubernetesPodIdentity(resource *Resource) string {
+ if resource == nil || resource.Kubernetes == nil {
+ return ""
+ }
+ if podUID := strings.TrimSpace(resource.Kubernetes.PodUID); podUID != "" {
+ return podUID
+ }
+ namespace := strings.TrimSpace(resource.Kubernetes.Namespace)
+ name := strings.TrimSpace(resource.Name)
+ if namespace == "" || name == "" {
+ return ""
+ }
+ return namespace + "/" + name
+}
+
+func seededKubernetesDeploymentIdentity(resource *Resource) string {
+ if resource == nil || resource.Kubernetes == nil {
+ return ""
+ }
+ if deploymentUID := strings.TrimSpace(resource.Kubernetes.DeploymentUID); deploymentUID != "" {
+ return deploymentUID
+ }
+ namespace := strings.TrimSpace(resource.Kubernetes.Namespace)
+ name := strings.TrimSpace(resource.Name)
+ if namespace == "" || name == "" {
+ return ""
+ }
+ return namespace + "/" + name
+}
+
+func seededVMwareSourceID(resource *Resource) string {
+ if resource == nil || resource.VMware == nil {
+ return ""
+ }
+
+ managedObjectID := strings.TrimSpace(resource.VMware.ManagedObjectID)
+ if managedObjectID == "" {
+ return ""
+ }
+
+ entityType := strings.TrimSpace(resource.VMware.EntityType)
+ if entityType == "" {
+ entityType = string(CanonicalResourceType(resource.Type))
+ }
+
+ parts := make([]string, 0, 3)
+ if connectionID := strings.TrimSpace(resource.VMware.ConnectionID); connectionID != "" {
+ parts = append(parts, connectionID)
+ }
+ if entityType != "" {
+ parts = append(parts, entityType)
+ }
+ parts = append(parts, managedObjectID)
+ return strings.Join(parts, ":")
+}
+
// List returns all resources.
func (rr *ResourceRegistry) List() []Resource {
rr.mu.RLock()
diff --git a/internal/unifiedresources/registry_test.go b/internal/unifiedresources/registry_test.go
index d9ba0e634f..24230ab97a 100644
--- a/internal/unifiedresources/registry_test.go
+++ b/internal/unifiedresources/registry_test.go
@@ -175,6 +175,62 @@ func TestResourceRegistry_IngestResourcesSeedsMatcherForOverlayRecords(t *testin
}
}
+func TestResourceRegistry_IngestResourcesRebuildsSourceMappingsForMetricsTargets(t *testing.T) {
+ rr := NewRegistry(nil)
+ now := time.Date(2026, 4, 21, 9, 0, 0, 0, time.UTC)
+
+ rr.IngestResources([]Resource{
+ {
+ ID: "agent-dashboard-1",
+ Type: ResourceTypeAgent,
+ Name: "delly",
+ Status: StatusOnline,
+ LastSeen: now,
+ Sources: []DataSource{SourceProxmox, SourceAgent},
+ Identity: ResourceIdentity{
+ MachineID: "machine-delly",
+ Hostnames: []string{"delly"},
+ },
+ Proxmox: &ProxmoxData{
+ SourceID: "homelab-delly",
+ NodeName: "delly",
+ Instance: "homelab-entry",
+ },
+ Agent: &AgentData{
+ AgentID: "agent-source-delly",
+ Hostname: "delly",
+ },
+ },
+ })
+
+ target := BuildMetricsTargetForRegistry(rr, "agent-dashboard-1")
+ if target == nil {
+ t.Fatal("expected seeded unified agent to rebuild a metrics target")
+ }
+ if target.ResourceType != "agent" || target.ResourceID != "homelab-delly" {
+ t.Fatalf("metrics target = %+v, want agent/homelab-delly", target)
+ }
+
+ targets := rr.SourceTargets("agent-dashboard-1")
+ if len(targets) != 2 {
+ t.Fatalf("expected 2 source targets, got %d", len(targets))
+ }
+
+ foundAgent := false
+ foundProxmox := false
+ for _, sourceTarget := range targets {
+ switch sourceTarget.Source {
+ case SourceAgent:
+ foundAgent = sourceTarget.SourceID == "agent-source-delly"
+ case SourceProxmox:
+ foundProxmox = sourceTarget.SourceID == "homelab-delly"
+ }
+ }
+ if !foundAgent || !foundProxmox {
+ t.Fatalf("expected rebuilt agent+proxmox source targets, got %+v", targets)
+ }
+}
+
func TestStorageRiskSemanticsPrefersUnraidParitySummaryOverGenericDiskCounts(t *testing.T) {
risk := &StorageRisk{
Reasons: []StorageRiskReason{