mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2026-07-21 22:40:37 +02:00
feat(metricstore): add running-stat fields to buffer
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -42,6 +42,7 @@ package metricstore
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"math"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -176,6 +177,17 @@ type buffer struct {
|
|||||||
archived bool
|
archived bool
|
||||||
closed bool
|
closed bool
|
||||||
lastUsed int64
|
lastUsed int64
|
||||||
|
|
||||||
|
// Running statistics over the non-NaN values currently in data.
|
||||||
|
// avg is derived as statSum/statSamples, never stored.
|
||||||
|
// statsValid is false when the cached aggregate may be stale (after an
|
||||||
|
// overwrite, or for a bare-constructed buffer); a stats query recomputes
|
||||||
|
// before using the cache. Zero value (false) fails safe to "recompute".
|
||||||
|
statSum float64
|
||||||
|
statSamples int
|
||||||
|
statMin float64
|
||||||
|
statMax float64
|
||||||
|
statsValid bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBuffer(ts, freq int64) *buffer {
|
func newBuffer(ts, freq int64) *buffer {
|
||||||
@@ -187,6 +199,11 @@ func newBuffer(ts, freq int64) *buffer {
|
|||||||
b.archived = false
|
b.archived = false
|
||||||
b.closed = false
|
b.closed = false
|
||||||
b.data = b.data[:0]
|
b.data = b.data[:0]
|
||||||
|
b.statSum = 0
|
||||||
|
b.statSamples = 0
|
||||||
|
b.statMin = math.MaxFloat32
|
||||||
|
b.statMax = -math.MaxFloat32
|
||||||
|
b.statsValid = true
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||||
|
// All rights reserved. This file is part of cc-backend.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package metricstore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewBufferStatsInitialized(t *testing.T) {
|
||||||
|
b := newBuffer(100, 10)
|
||||||
|
if b.statSamples != 0 {
|
||||||
|
t.Errorf("statSamples = %d, want 0", b.statSamples)
|
||||||
|
}
|
||||||
|
if b.statSum != 0 {
|
||||||
|
t.Errorf("statSum = %v, want 0", b.statSum)
|
||||||
|
}
|
||||||
|
if b.statMin != math.MaxFloat32 {
|
||||||
|
t.Errorf("statMin = %v, want MaxFloat32 sentinel", b.statMin)
|
||||||
|
}
|
||||||
|
if b.statMax != -math.MaxFloat32 {
|
||||||
|
t.Errorf("statMax = %v, want -MaxFloat32 sentinel", b.statMax)
|
||||||
|
}
|
||||||
|
if !b.statsValid {
|
||||||
|
t.Error("statsValid = false, want true for a fresh empty buffer")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user