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(metrics): retain newest observations in bounded history

rcourtman94523d880755
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index cd9d8078e8..a44faa7688 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -43,6 +43,9 @@ the monitor-owned history selector. Responses contain usage percentages with
timestamps and source provenance, including `mock_synthetic`, rather than
invented zero CPU/memory observations. Retained read errors remain errors and an
empty requested window cannot be filled with older chart samples.
+History downsampling covers every observation in the returned window, including
+the newest remainder samples when the count is not a multiple of the tool limit.
+`TestDownsampleMetrics_IncludesNewestRemainder` covers dense retained series.
`TestStoragePerformanceReadsRetainedHistoryAcrossRestart`,
`TestStorageInvestigationHistoryKeepsRequestedWindowAndStoreErrors` and
`TestInvestigationStorageHistoryUsesCommunityMonitoringSource` cover retained
diff --git a/internal/ai/tools/tools_metrics.go b/internal/ai/tools/tools_metrics.go
index f3f720d5cb..629f0d9f31 100644
--- a/internal/ai/tools/tools_metrics.go
+++ b/internal/ai/tools/tools_metrics.go
@@ -803,7 +803,7 @@ func (e *PulseToolExecutor) executeListPhysicalDisks(_ context.Context, args map
const maxMetricPoints = 120
// downsampleMetrics reduces a slice of MetricPoints to targetCount points
-// by bucket-averaging. Each bucket covers an equal time span and the output
+// by bucket-averaging. Each bucket covers consecutive observations and the output
// point uses the bucket's midpoint timestamp with averaged metric values.
func downsampleMetrics(points []MetricPoint, targetCount int) []MetricPoint {
if len(points) <= targetCount || targetCount <= 0 {
@@ -815,18 +815,13 @@ func downsampleMetrics(points []MetricPoint, targetCount int) []MetricPoint {
return points[i].Timestamp.Before(points[j].Timestamp)
})
- bucketSize := len(points) / targetCount
- if bucketSize < 1 {
- bucketSize = 1
- }
-
result := make([]MetricPoint, 0, targetCount)
- for i := 0; i < len(points); i += bucketSize {
- end := i + bucketSize
- if end > len(points) {
- end = len(points)
- }
- bucket := points[i:end]
+ for i := 0; i < targetCount; i++ {
+ // Proportional boundaries cover every observation. Fixed-size buckets
+ // followed by an output cap discarded the newest remainder samples.
+ start := i * len(points) / targetCount
+ end := (i + 1) * len(points) / targetCount
+ bucket := points[start:end]
var sumCPU, sumMem, sumDisk float64
hasDisk := false
@@ -852,9 +847,6 @@ func downsampleMetrics(points []MetricPoint, targetCount int) []MetricPoint {
}
result = append(result, avg)
- if len(result) >= targetCount {
- break
- }
}
return result
diff --git a/internal/ai/tools/tools_metrics_downsample_test.go b/internal/ai/tools/tools_metrics_downsample_test.go
index 7a57a28e4a..f91725112c 100644
--- a/internal/ai/tools/tools_metrics_downsample_test.go
+++ b/internal/ai/tools/tools_metrics_downsample_test.go
@@ -1,6 +1,7 @@
package tools
import (
+ "strconv"
"testing"
"time"
@@ -206,3 +207,27 @@ func TestMetricsResponseFields(t *testing.T) {
assert.True(t, resp.Downsampled)
assert.Equal(t, 10080, resp.OriginalCount)
}
+
+// A dense retained series must not silently lose the most recent evidence.
+func TestDownsampleMetrics_IncludesNewestRemainder(t *testing.T) {
+ for _, count := range []int{121, 239, 500, 1000} {
+ t.Run(strconv.Itoa(count), func(t *testing.T) {
+ points := make([]MetricPoint, count)
+ start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
+ for i := range points {
+ points[i].Timestamp = start.Add(time.Duration(i) * time.Minute)
+ }
+ points[count-1].CPU = 120
+ points[count-1].Memory = 240
+ points[count-1].Disk = 60
+
+ result := downsampleMetrics(points, maxMetricPoints)
+ require.Len(t, result, maxMetricPoints)
+ last := result[len(result)-1]
+ assert.Positive(t, last.CPU, "newest CPU evidence must contribute")
+ assert.Positive(t, last.Memory, "newest memory evidence must contribute")
+ assert.Positive(t, last.Disk, "newest storage evidence must contribute")
+ assert.True(t, last.Timestamp.After(points[count-maxMetricPoints].Timestamp))
+ })
+ }
+}