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.

Deduplicate PBS datastores and bridge their override key formats

rcourtman5b963dc34ff7
diff --git a/frontend-modern/src/features/alerts/__tests__/alertOverridesModel.pbsDatastore.test.ts b/frontend-modern/src/features/alerts/__tests__/alertOverridesModel.pbsDatastore.test.ts
new file mode 100644
index 0000000000..84d69e918e
--- /dev/null
+++ b/frontend-modern/src/features/alerts/__tests__/alertOverridesModel.pbsDatastore.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, it } from 'vitest';
+
+import type { Resource } from '@/types/resource';
+
+import { storageOverrideIdCandidates } from '../alertOverridesModel';
+
+const pbsDatastore = (overrides: Partial<Resource> = {}): Resource =>
+ ({
+ id: 'storage-hash',
+ name: 'main',
+ type: 'storage',
+ storage: { platform: 'pbs', type: 'pbs-datastore' },
+ metricsTarget: { resourceType: 'storage', resourceId: 'pbs-pbs-docker/main' },
+ ...overrides,
+ }) as Resource;
+
+describe('storageOverrideIdCandidates for PBS datastores (#1591)', () => {
+ it('includes the legacy dash-format key after the canonical candidates', () => {
+ expect(storageOverrideIdCandidates(pbsDatastore())).toEqual([
+ 'pbs-pbs-docker/main',
+ 'storage-hash',
+ 'pbs-pbs-docker-main',
+ ]);
+ });
+
+ it('does not fabricate a legacy key for non-PBS storage', () => {
+ expect(
+ storageOverrideIdCandidates(
+ pbsDatastore({ storage: { platform: 'truenas' } as Resource['storage'] }),
+ ),
+ ).toEqual(['pbs-pbs-docker/main', 'storage-hash']);
+ });
+
+ it('does not fabricate a legacy key without a canonical metrics target', () => {
+ expect(storageOverrideIdCandidates(pbsDatastore({ metricsTarget: undefined }))).toEqual([
+ 'storage-hash',
+ ]);
+ });
+});
diff --git a/frontend-modern/src/features/alerts/alertOverridesModel.ts b/frontend-modern/src/features/alerts/alertOverridesModel.ts
index b88b161ed4..8fcd9adcf0 100644
--- a/frontend-modern/src/features/alerts/alertOverridesModel.ts
+++ b/frontend-modern/src/features/alerts/alertOverridesModel.ts
@@ -70,13 +70,30 @@ const buildSharedStorageLegacyKeyMap = (storageResources: Resource[]): Map<strin
return legacyToCanonical;
};
-export const storageOverrideIdCandidates = (resource: Resource): string[] =>
- uniqueIds(
+// PBS datastore overrides were historically stored under the poller's
+// "<instance-id>-<name>" storage ID while the canonical resource ID is
+// "<instance-id>/<name>". Datastore names cannot contain "/", so mapping the
+// last slash to a dash reconstructs the legacy key exactly; carrying it as a
+// trailing candidate keeps pre-existing overrides bound to the single card
+// and lets the next save re-home them onto the canonical key (#1591).
+const legacyPBSDatastoreOverrideId = (resource: Resource, canonicalId?: string): string | undefined => {
+ if (resource.storage?.platform !== 'pbs' || !canonicalId) return undefined;
+ const slash = canonicalId.lastIndexOf('/');
+ if (slash <= 0) return undefined;
+ return `${canonicalId.slice(0, slash)}-${canonicalId.slice(slash + 1)}`;
+};
+
+export const storageOverrideIdCandidates = (resource: Resource): string[] => {
+ const canonicalId =
resource.metricsTarget?.resourceType === 'storage'
? resource.metricsTarget.resourceId
- : undefined,
+ : undefined;
+ return uniqueIds(
+ canonicalId,
resource.id,
+ legacyPBSDatastoreOverrideId(resource, canonicalId),
);
+};
export const storageOverrideActionId = (resource: Resource): string =>
storageOverrideIdCandidates(resource)[0] || resource.id;
diff --git a/internal/alerts/pbs_datastore_override_alias_test.go b/internal/alerts/pbs_datastore_override_alias_test.go
new file mode 100644
index 0000000000..667c9329e2
--- /dev/null
+++ b/internal/alerts/pbs_datastore_override_alias_test.go
@@ -0,0 +1,38 @@
+package alerts
+
+import (
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+)
+
+// Overrides written by the thresholds UI key PBS datastores by the canonical
+// "<instance-id>/<name>" resource ID, while CheckStorage evaluates the legacy
+// "<instance-id>-<name>" storage model. The alias carried by the conversion
+// must bridge the two so a UI-written override actually applies (#1591).
+func TestResolveStorageThresholds_HonorsCanonicalPBSDatastoreAlias(t *testing.T) {
+ m := NewManagerWithDataDir(t.TempDir())
+ m.UpdateConfig(AlertConfig{
+ Enabled: true,
+ StorageDefault: HysteresisThreshold{Trigger: 85, Clear: 80},
+ Overrides: map[string]ThresholdConfig{
+ "pbs-pbs-docker/main": {Usage: &HysteresisThreshold{Trigger: 95, Clear: 90}},
+ },
+ })
+
+ storage := models.Storage{
+ ID: "pbs-pbs-docker-main",
+ AliasIDs: []string{"pbs-pbs-docker/main"},
+ Name: "main",
+ Type: "pbs",
+ Instance: "pbs-pbs-docker",
+ }
+
+ m.mu.RLock()
+ thresholds := m.resolveStorageThresholdsNoLock(storage)
+ m.mu.RUnlock()
+
+ if thresholds.Usage == nil || thresholds.Usage.Trigger != 95 {
+ t.Fatalf("expected canonical-keyed override to apply via alias, got %+v", thresholds.Usage)
+ }
+}
diff --git a/internal/monitoring/monitor_pbs_pmg.go b/internal/monitoring/monitor_pbs_pmg.go
index fb64768bd1..105ecae6da 100644
--- a/internal/monitoring/monitor_pbs_pmg.go
+++ b/internal/monitoring/monitor_pbs_pmg.go
@@ -457,7 +457,11 @@ func (m *Monitor) pollPBSInstance(ctx context.Context, instanceName string, clie
// Create a storage entry for this PBS datastore
storageID := fmt.Sprintf("pbs-%s-%s", instanceName, ds.Name)
pbsStorage := models.Storage{
- ID: storageID,
+ ID: storageID,
+ // The unified registry and thresholds UI key this datastore by
+ // its canonical "<instance-id>/<name>" ID; carrying it as an
+ // alias lets alert override lookups accept either format (#1591).
+ AliasIDs: []string{pbsInst.ID + "/" + ds.Name},
Name: ds.Name,
Node: instanceName, // Use PBS instance name as "node"
Instance: "pbs-" + instanceName,
diff --git a/internal/unifiedresources/pbs_datastore_dedupe_test.go b/internal/unifiedresources/pbs_datastore_dedupe_test.go
new file mode 100644
index 0000000000..e9d04385ba
--- /dev/null
+++ b/internal/unifiedresources/pbs_datastore_dedupe_test.go
@@ -0,0 +1,67 @@
+package unifiedresources
+
+import (
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+)
+
+// A PBS datastore reaches the registry twice: once via the PBS instance
+// snapshot (canonical "<instance-id>/<name>" source ID) and once via the PBS
+// poller's models.Storage conversion (legacy "<instance-id>-<name>" ID). Both
+// must resolve to ONE resource, or the thresholds page renders duplicate cards
+// and overrides split across two key formats (#1591).
+func TestIngestSnapshot_PBSDatastoreStorageConversionDoesNotDuplicate(t *testing.T) {
+ now := time.Now()
+ registry := NewRegistry(nil)
+
+ registry.IngestSnapshot(models.StateSnapshot{
+ PBSInstances: []models.PBSInstance{{
+ ID: "pbs-pbs-docker",
+ Name: "pbs-docker",
+ Host: "https://pbs-docker.lan:8007",
+ Status: "online",
+ LastSeen: now,
+ Datastores: []models.PBSDatastore{{
+ Name: "main",
+ Total: 1000,
+ Used: 400,
+ Free: 600,
+ Usage: 40,
+ Status: "available",
+ }},
+ }},
+ Storage: []models.Storage{{
+ ID: "pbs-pbs-docker-main",
+ AliasIDs: []string{"pbs-pbs-docker/main"},
+ Name: "main",
+ Node: "pbs-docker",
+ Instance: "pbs-pbs-docker",
+ Type: "pbs",
+ Status: "available",
+ Total: 1000,
+ Used: 400,
+ Free: 600,
+ Usage: 40,
+ Content: "backup",
+ Enabled: true,
+ Active: true,
+ LastSeen: now,
+ }},
+ })
+
+ var datastores []Resource
+ for _, resource := range registry.List() {
+ if resource.Type == ResourceTypeStorage && resource.Name == "main" {
+ datastores = append(datastores, resource)
+ }
+ }
+
+ if len(datastores) != 1 {
+ t.Fatalf("expected one unified resource for the PBS datastore, got %d: %+v", len(datastores), datastores)
+ }
+ if datastores[0].Storage == nil || datastores[0].Storage.Type != "pbs-datastore" {
+ t.Fatalf("expected the canonical pbs-datastore resource to survive, got %+v", datastores[0].Storage)
+ }
+}
diff --git a/internal/unifiedresources/registry.go b/internal/unifiedresources/registry.go
index 7b392960ab..4b3ab53df7 100644
--- a/internal/unifiedresources/registry.go
+++ b/internal/unifiedresources/registry.go
@@ -1634,6 +1634,17 @@ func (rr *ResourceRegistry) ingestStorage(storage models.Storage) {
// datastores stale between PBS polls. storage.Instance carries the
// PBS instance source ID ("pbs-<name>"), so parent them to the PBS
// instance, which IngestSnapshot ingests before storage.
+ // The PBS snapshot adapter already ingests this datastore under its
+ // canonical "<instance-id>/<name>" source ID (pbsDatastoreSourceID)
+ // with the richer pbs-datastore metadata. Ingesting the poller's
+ // storage conversion again under the legacy "<instance-id>-<name>"
+ // storage ID minted a second resource for the same datastore, which
+ // rendered duplicate threshold cards and split overrides across two
+ // key formats (#1591).
+ canonicalDatastoreID := normalizeSourceID(storage.Instance + "/" + storage.Name)
+ if _, ok := rr.bySource[SourcePBS][canonicalDatastoreID]; ok {
+ return
+ }
if parentID, ok := rr.bySource[SourcePBS][storage.Instance]; ok {
resource.ParentID = &parentID
}