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.

Bound mock chart synthesis before computing canonical values

rcourtman358a85002750
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
index f3babc1da0..3e9e77830a 100644
--- a/docs/release-control/v6/internal/subsystems/monitoring.md
+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
@@ -1196,6 +1196,14 @@ history, the store query path, chart point targets and mock values are unchanged
pins identical cpu/memory/disk/network/disk-I/O timestamps for a mock VM, container
and node, on synthetic and seeded history, at 1h, 4h, 24h and 7d.
+Synthetic chart fallbacks select that same retained timestamp grid before
+evaluating canonical metric values. A long-window request over the full demo
+estate must not synthesize hundreds of discarded readings for every series.
+The selected readings retain their exact canonical values and timestamps,
+including the oldest and newest reading. Historical seeding remains dense and
+unchanged. `mock_chart_history_test.go` proves the generation bound, parity with
+the former dense-then-thin timeline, and canonical values across resource types.
+
### Podman interval CPU selection — issue #2447
Every Podman one-shot response uses consecutive cumulative CPU-time nanoseconds
diff --git a/docs/release-control/v6/internal/subsystems/registry.json b/docs/release-control/v6/internal/subsystems/registry.json
index 44b54af4a3..7d2dc99934 100644
--- a/docs/release-control/v6/internal/subsystems/registry.json
+++ b/docs/release-control/v6/internal/subsystems/registry.json
@@ -6840,6 +6840,7 @@
"internal/monitoring/issue1638_dns_cache_test.go",
"internal/monitoring/issue1913_host_continuity_test.go",
"internal/monitoring/metric_window_provider_test.go",
+ "internal/monitoring/mock_chart_history_test.go",
"internal/monitoring/monitor_additional_test.go",
"internal/monitoring/monitor_alert_handling_test.go",
"internal/monitoring/monitor_alert_intent_test.go",
diff --git a/internal/monitoring/mock_chart_history.go b/internal/monitoring/mock_chart_history.go
index 1bc5df509e..8d17938412 100644
--- a/internal/monitoring/mock_chart_history.go
+++ b/internal/monitoring/mock_chart_history.go
@@ -37,7 +37,10 @@ func mockChartTimestamps(duration time.Duration) []time.Time {
now := normalizeMockMetricTimestamp(time.Now().UTC(), defaultMockSampleInterval)
timestamps := buildTieredTimestamps(now, duration)
if len(timestamps) > 0 {
- return timestamps
+ // Canonical values depend only on identity and timestamp. Select the
+ // chart's retained timestamps before synthesizing values, rather than
+ // computing a dense history for every guest and discarding most of it.
+ return sampleOnMockChartGrid(timestamps, func(at time.Time) time.Time { return at }, mockChartGridInterval(duration))
}
return []time.Time{
@@ -94,12 +97,16 @@ func mockChartGridInterval(duration time.Duration) time.Duration {
// sampled at the same times keeps the same timestamps, as the poller writes
// them. points must be in ascending timestamp order.
func sampleMetricSeriesOnMockChartGrid(points []MetricPoint, interval time.Duration) []MetricPoint {
+ return sampleOnMockChartGrid(points, func(point MetricPoint) time.Time { return point.Timestamp }, interval)
+}
+
+func sampleOnMockChartGrid[T any](points []T, timestamp func(T) time.Time, interval time.Duration) []T {
if len(points) == 0 {
return nil
}
width := int64(interval)
if width <= 0 {
- return cloneMetricSeries(points)
+ return append([]T(nil), points...)
}
bucketOf := func(ts time.Time) int64 {
@@ -111,9 +118,9 @@ func sampleMetricSeriesOnMockChartGrid(points []MetricPoint, interval time.Durat
return bucket
}
- sampled := make([]MetricPoint, 0, len(points))
+ var sampled []T
for i, point := range points {
- if i > 0 && i < len(points)-1 && bucketOf(point.Timestamp) == bucketOf(points[i-1].Timestamp) {
+ if i > 0 && i < len(points)-1 && bucketOf(timestamp(point)) == bucketOf(timestamp(points[i-1])) {
continue
}
sampled = append(sampled, point)
diff --git a/internal/monitoring/mock_chart_history_test.go b/internal/monitoring/mock_chart_history_test.go
new file mode 100644
index 0000000000..9da5239ec3
--- /dev/null
+++ b/internal/monitoring/mock_chart_history_test.go
@@ -0,0 +1,104 @@
+package monitoring
+
+import (
+ "reflect"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/mock"
+)
+
+func TestMockChartTimestampGridKeepsOriginalBucketReadings(t *testing.T) {
+ // Include dates before the Unix epoch, several readings in one bucket,
+ // and a terminal reading in the same bucket as its predecessor.
+ offsets := []time.Duration{-3 * time.Second, -2800 * time.Millisecond, -1100 * time.Millisecond,
+ -time.Second, -500 * time.Millisecond, 0, 500 * time.Millisecond,
+ time.Second, 1100 * time.Millisecond, 1500 * time.Millisecond}
+ points := make([]time.Time, len(offsets))
+ for i, offset := range offsets {
+ points[i] = time.Unix(0, 0).Add(offset)
+ }
+ want := []time.Time{points[0], points[2], points[3], points[5], points[7], points[9]}
+ got := sampleOnMockChartGrid(points, func(at time.Time) time.Time { return at }, time.Second)
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("grid changed original bucket readings: got %v, want %v", got, want)
+ }
+ for _, interval := range []time.Duration{0, -time.Second} {
+ cloned := sampleOnMockChartGrid(points, func(at time.Time) time.Time { return at }, interval)
+ if !reflect.DeepEqual(cloned, points) {
+ t.Fatalf("interval %s changed the unsampled input", interval)
+ }
+ cloned[0] = time.Time{}
+ if points[0].IsZero() {
+ t.Fatal("the returned series aliases its input")
+ }
+ }
+}
+
+func TestMockChartTimestampsBoundSynthesisAndPreserveTheDenseHistoryGrid(t *testing.T) {
+ for _, duration := range []time.Duration{5 * time.Minute, time.Hour, 4 * time.Hour,
+ 24 * time.Hour, 7 * 24 * time.Hour, 30 * 24 * time.Hour} {
+ t.Run(duration.String(), func(t *testing.T) {
+ got := mockChartTimestamps(duration)
+ if len(got) < 2 || len(got) > mockChartPointTarget(duration) {
+ t.Fatalf("synthesizing %d values per series, want 2..%d", len(got), mockChartPointTarget(duration))
+ }
+ // Retain the former dense-then-thin calculation as the parity
+ // oracle. Selecting timestamps first must preserve every reading
+ // the chart previously returned, including the fresh terminal one.
+ dense := buildTieredTimestamps(got[len(got)-1], duration)
+ readings := make([]MetricPoint, len(dense))
+ for i, at := range dense {
+ readings[i] = MetricPoint{Timestamp: at, Value: float64(i)}
+ }
+ want := sampleMetricSeriesOnMockChartGrid(readings, mockChartGridInterval(duration))
+ if len(got) != len(want) {
+ t.Fatalf("timestamp selection changed: got %d readings, want %d", len(got), len(want))
+ }
+ for i, at := range got {
+ if !at.Equal(want[i].Timestamp) {
+ t.Fatalf("reading %d moved: got %s, want %s", i, at, want[i].Timestamp)
+ }
+ }
+ if !got[0].Equal(dense[0]) || !got[len(got)-1].Equal(dense[len(dense)-1]) {
+ t.Fatal("the full history window or fresh terminal reading was lost")
+ }
+ })
+ }
+}
+
+func TestMockChartSynthesisKeepsCanonicalValuesAtRetainedTimestamps(t *testing.T) {
+ for _, resourceType := range []string{"vm", "container", "dockerContainer", "k8s", "agent", "dockerHost", "node", "disk"} {
+ t.Run(resourceType, func(t *testing.T) {
+ const resourceID = "mock-chart-parity"
+ const duration = 7 * 24 * time.Hour
+ var series map[string][]MetricPoint
+ switch resourceType {
+ case "node":
+ series = mockNodeMetricsForChart(resourceID, nil, duration)
+ case "disk":
+ series = mockDiskMetricsForChart(resourceID, nil, duration)
+ default:
+ series = mockGuestMetricsForChart(resourceType, resourceID, duration)
+ }
+ if len(series) == 0 {
+ t.Fatal("synthetic history is empty")
+ }
+ sampler := mock.CurrentMetricSampler()
+ for metric, points := range series {
+ if metric == "memoryused" {
+ continue // Derived bytes have their own capacity parity proof.
+ }
+ if len(points) < 2 || len(points) > mockChartPointTarget(duration) {
+ t.Fatalf("%s synthesized %d points, want a bounded full-window series", metric, len(points))
+ }
+ for _, point := range points {
+ want := sampler.SampleMetric(resourceType, resourceID, metric, point.Timestamp)
+ if point.Value != want {
+ t.Fatalf("%s at %s changed its canonical value: got %g, want %g", metric, point.Timestamp, point.Value, want)
+ }
+ }
+ }
+ })
+ }
+}