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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Keep each alert recurrence as its own history row
rcourtmanb549e232a592
diff --git a/internal/alerts/history.go b/internal/alerts/history.goindex 75129ba299..296c5580ad 100644--- a/internal/alerts/history.go+++ b/internal/alerts/history.go@@ -205,12 +205,11 @@ func sameHistoryIncident(existing, incoming HistoryEntry) bool {if existingResolved || incomingResolved {return false}- if existing.Alert.OperationalRecord != nil &&- incoming.Alert.OperationalRecord != nil &&- !existingResolved && !incomingResolved {- return true- }+ // Two open records do not by themselves make one incident: an operational+ // record can survive across occurrences (or be stranded open), and treating+ // that as identity merged every recurrence into its first row forever. The+ // observation-gap window below decides instead.lastObservation := existing.Timestampif existing.Alert.LastSeen.After(lastObservation) {lastObservation = existing.Alert.LastSeen@@ -297,6 +296,19 @@ func (hm *HistoryManager) UpdateAlertOperationalContractForAlert(alert *Alert) {entryKey := historyIdentityKey(entry)if (matchKey != "" && entryKey == matchKey) ||(matchID != "" && hm.history[i].Alert.ID == matchID) {+ // A resolved row is this occurrence's final state. A later+ // occurrence of the same identity (different StartTime) gets its+ // own row from AddAlert, so its record must not overwrite the+ // resolved one here.+ entryResolved := entry.OperationalRecord != nil &&+ entry.OperationalRecord.State == operationaltrust.OperationalResolved+ snapshotResolved := snapshot.OperationalRecord != nil &&+ snapshot.OperationalRecord.State == operationaltrust.OperationalResolved+ if entryResolved && !snapshotResolved &&+ !entry.StartTime.IsZero() && !snapshot.StartTime.IsZero() &&+ !entry.StartTime.Equal(snapshot.StartTime) {+ return+ }hm.history[i].Alert.OperationalRecord = snapshot.OperationalRecordhm.history[i].Alert.LatestTransition = snapshot.LatestTransitionhm.history[i].Alert.Transitions = snapshot.Transitionsdiff --git a/internal/alerts/history_recurrence_test.go b/internal/alerts/history_recurrence_test.gonew file mode 100644index 0000000000..527ee4bfed--- /dev/null+++ b/internal/alerts/history_recurrence_test.go@@ -0,0 +1,122 @@+package alerts++import (+ "testing"+ "time"++ "github.com/rcourtman/pulse-go-rewrite/internal/models"+)++// A resolved occurrence followed by a fresh firing outside the refire+// cooldown must produce a second history row, not fold into the first.+// Refs #1497 follow-up: history frozen at the v6.2.0 upgrade date.+func TestHistoryNewOccurrenceAfterResolveAppendsRow(t *testing.T) {+ m := newTestManager(t)++ fire := func() {+ m.checkGuestPoweredOff("vm100", "TestVM", "pve-node1", "pve-instance", "VM", false)+ m.checkGuestPoweredOff("vm100", "TestVM", "pve-node1", "pve-instance", "VM", false)+ }++ fire()+ if got := len(m.GetAlertHistory(0)); got != 1 {+ t.Fatalf("expected 1 history row after first firing, got %d", got)+ }++ m.clearGuestPoweredOffAlert("vm100", "TestVM")++ // Age the resolved record out of the refire cooldown so the second+ // firing is a genuinely new occurrence, as it is a day later in prod.+ m.resolvedMutex.Lock()+ for key, resolved := range m.recentlyResolved {+ resolved.ResolvedTime = time.Now().Add(-2 * recentlyResolvedRetention)+ m.recentlyResolved[key] = resolved+ }+ m.resolvedMutex.Unlock()++ fire()++ history := m.GetAlertHistory(0)+ if len(history) != 2 {+ for i, alert := range history {+ t.Logf("history[%d]: id=%s start=%s record=%+v", i, alert.ID, alert.StartTime, alert.OperationalRecord)+ }+ t.Fatalf("expected 2 history rows after resolve and refire, got %d", len(history))+ }+}++// Metric-path variant: a resolved CPU occurrence followed by a fresh firing+// outside the refire cooldown must append a second history row. Before the+// fix, setActiveAlertNoLock stamped the new occurrence's open record onto+// the previous occurrence's resolved row and the history dedup then merged+// every recurrence into that first row forever. Refs #1497.+func TestHistoryMetricNewOccurrenceAfterResolveAppendsRow(t *testing.T) {+ m := newTestManager(t)++ m.mu.Lock()+ m.config.TimeThresholds = map[string]int{}+ m.config.ActivationState = ActivationActive+ m.mu.Unlock()++ guest := func(cpu float64) models.VM {+ return models.VM{+ ID: "vm-hist-1",+ Name: "hist-vm",+ Node: "node1",+ Instance: "inst1",+ Status: "running",+ CPU: cpu,+ Memory: models.Memory{Usage: 10},+ Disk: models.Disk{Usage: 10},+ }+ }++ m.CheckGuest(guest(1.0), "inst1")+ if got := len(m.GetAlertHistory(0)); got != 1 {+ t.Fatalf("expected 1 history row after first firing, got %d", got)+ }++ m.CheckGuest(guest(0.05), "inst1")++ m.resolvedMutex.Lock()+ if len(m.recentlyResolved) == 0 {+ m.resolvedMutex.Unlock()+ t.Fatal("expected the alert to resolve after CPU dropped")+ }+ for key, resolved := range m.recentlyResolved {+ resolved.ResolvedTime = time.Now().Add(-2 * recentlyResolvedRetention)+ m.recentlyResolved[key] = resolved+ }+ m.resolvedMutex.Unlock()++ // A next-day occurrence is outside the recent-alert suppression window+ // and carries a different observed value.+ m.mu.Lock()+ for key, recent := range m.recentAlerts {+ aged := *recent+ aged.StartTime = aged.StartTime.Add(-24 * time.Hour)+ m.recentAlerts[key] = &aged+ }+ m.mu.Unlock()++ // Age the stored resolution as well so the recurrence-continuity window+ // (flap dedup) does not apply, as it would not a day later.+ m.historyManager.mu.Lock()+ for i := range m.historyManager.history {+ if record := m.historyManager.history[i].Alert.OperationalRecord; record != nil && record.ResolvedAt != nil {+ aged := record.ResolvedAt.Add(-24 * time.Hour)+ record.ResolvedAt = &aged+ }+ }+ m.historyManager.mu.Unlock()++ m.CheckGuest(guest(0.93), "inst1")++ history := m.GetAlertHistory(0)+ if len(history) != 2 {+ for i, alert := range history {+ t.Logf("history[%d]: id=%s start=%s record=%+v", i, alert.ID, alert.StartTime, alert.OperationalRecord)+ }+ t.Fatalf("expected 2 history rows after resolve and refire, got %d", len(history))+ }+}