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.

Keep mock alert history behind the observation clock

Pulse Test1a8d4aff8e23
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
index f9fe1571f1..8fa5bc9a17 100644
--- a/docs/release-control/v6/internal/subsystems/monitoring.md
+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
@@ -1946,8 +1946,11 @@ must never expose a Timeline control backed by a missing incident.
must mirror the live history read contract: rows are newest-first, closed rows
carry the same resolution timestamp as their incident in `lastSeen`, and live
alerts come from the active-alert snapshot rather than unrelated synthetic open
-history rows. History duration, status, and Timeline therefore remain three
-projections of one occurrence instead of contradicting each other in demo mode.
+history rows. The current-day generator is bounded by the fixture observation
+clock and local calendar boundary, including short or long daylight-saving
+days, so neither a row nor its lifecycle events can occur in the future.
+History duration, status, and Timeline therefore remain three projections of
+one occurrence instead of contradicting each other in demo mode.
Mock fixture defaults in `internal/mock/generator.go` (the `DefaultConfig`
constant) are also part of that mock-runtime contract. The Proxmox default is
an intentionally large public-demo estate so platform-first pages exercise
diff --git a/internal/mock/alert_history.go b/internal/mock/alert_history.go
index 1303b0142e..931f61ff12 100644
--- a/internal/mock/alert_history.go
+++ b/internal/mock/alert_history.go
@@ -11,6 +11,10 @@ import (
// buildAlertHistory derives historical alert data from the canonical fixture graph.
func buildAlertHistory(nodes []models.Node, vms []models.VM, containers []models.Container) []models.Alert {
+ return buildAlertHistoryAt(nodes, vms, containers, time.Now())
+}
+
+func buildAlertHistoryAt(nodes []models.Node, vms []models.VM, containers []models.Container, now time.Time) []models.Alert {
var history []models.Alert
// Alert types and messages
@@ -66,8 +70,9 @@ func buildAlertHistory(nodes []models.Node, vms []models.VM, containers []models
},
}
- // Generate alerts for the past 90 days with more consistent distribution
- now := time.Now()
+ // Generate alerts for the past 90 days with more consistent distribution.
+ // The current day is bounded by now; a history fixture must never describe
+ // an incident that has not happened yet.
for days := 90; days >= 0; days-- {
// Generate 2-15 alerts per day for more realistic history
numAlerts := rand.Intn(14) + 2
@@ -107,16 +112,7 @@ func buildAlertHistory(nodes []models.Node, vms []models.VM, containers []models
}
}
- // Random time during that day
- hours := rand.Intn(24)
- minutes := rand.Intn(60)
- seconds := rand.Intn(60)
-
- startTime := now.AddDate(0, 0, -days).
- Truncate(24 * time.Hour).
- Add(time.Duration(hours) * time.Hour).
- Add(time.Duration(minutes) * time.Minute).
- Add(time.Duration(seconds) * time.Second)
+ startTime := randomAlertHistoryTime(now, days)
// Alert duration (resolved after 1 minute to 4 hours) - for display purposes
@@ -164,3 +160,17 @@ func buildAlertHistory(nodes []models.Node, vms []models.VM, containers []models
return history
}
+
+func randomAlertHistoryTime(now time.Time, daysAgo int) time.Time {
+ day := now.AddDate(0, 0, -daysAgo)
+ dayStart := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, day.Location())
+ latest := dayStart.AddDate(0, 0, 1).Add(-time.Second)
+ if daysAgo == 0 {
+ latest = now.Truncate(time.Second)
+ }
+ if !latest.After(dayStart) {
+ return dayStart
+ }
+ spanSeconds := int64(latest.Sub(dayStart) / time.Second)
+ return dayStart.Add(time.Duration(rand.Int63n(spanSeconds+1)) * time.Second)
+}
diff --git a/internal/mock/integration_coverage_test.go b/internal/mock/integration_coverage_test.go
index e810f59316..6f5ce10a63 100644
--- a/internal/mock/integration_coverage_test.go
+++ b/internal/mock/integration_coverage_test.go
@@ -545,10 +545,12 @@ func TestUpdateAlertSnapshotsAndHistoryAccessors(t *testing.T) {
}
func TestMockAlertHistoryMatchesProductionLifecycleContract(t *testing.T) {
- history := buildAlertHistory(
+ fixtureNow := time.Date(2026, time.August, 27, 23, 40, 0, 0, time.FixedZone("fixture-local", 60*60))
+ history := buildAlertHistoryAt(
[]models.Node{{ID: "node-1", Name: "pve-1"}},
[]models.VM{{ID: "vm-1", Name: "database", Node: "pve-1"}},
[]models.Container{{ID: "ct-1", Name: "worker", Node: "pve-1"}},
+ fixtureNow,
)
if len(history) == 0 {
t.Fatal("mock history is empty")
@@ -560,10 +562,12 @@ func TestMockAlertHistoryMatchesProductionLifecycleContract(t *testing.T) {
if index > 0 && alert.StartTime.After(history[index-1].StartTime) {
t.Fatalf("mock history is not newest-first at %d: %v after %v", index, alert.StartTime, history[index-1].StartTime)
}
+ if alert.StartTime.After(fixtureNow) {
+ t.Fatalf("mock history row %q starts in the future: %v after %v", alert.ID, alert.StartTime, fixtureNow)
+ }
}
- now := time.Now()
- enriched, incidents := buildAlertIncidentFixtures(history, now)
+ enriched, incidents := buildAlertIncidentFixtures(history, fixtureNow)
if len(enriched) != len(incidents) {
t.Fatalf("enriched history rows = %d, incidents = %d", len(enriched), len(incidents))
}
@@ -576,3 +580,31 @@ func TestMockAlertHistoryMatchesProductionLifecycleContract(t *testing.T) {
}
}
}
+
+func TestRandomAlertHistoryTimeRespectsLocalCalendarAndObservationClock(t *testing.T) {
+ location, err := time.LoadLocation("Europe/London")
+ if err != nil {
+ t.Fatalf("load fixture timezone: %v", err)
+ }
+ times := []time.Time{
+ time.Date(2026, time.March, 29, 0, 30, 0, 0, location),
+ time.Date(2026, time.October, 25, 23, 30, 0, 0, location),
+ time.Date(2026, time.August, 27, 0, 0, 0, 0, location),
+ }
+ for _, now := range times {
+ for daysAgo := 0; daysAgo <= 2; daysAgo++ {
+ day := now.AddDate(0, 0, -daysAgo)
+ dayStart := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, location)
+ nextDay := dayStart.AddDate(0, 0, 1)
+ for sample := 0; sample < 100; sample++ {
+ got := randomAlertHistoryTime(now, daysAgo)
+ if got.Before(dayStart) || !got.Before(nextDay) {
+ t.Fatalf("random history time %v outside local day [%v, %v)", got, dayStart, nextDay)
+ }
+ if daysAgo == 0 && got.After(now) {
+ t.Fatalf("random history time %v is after observation clock %v", got, now)
+ }
+ }
+ }
+ }
+}