Refactor for readability

This commit is contained in:
flo_isea committed 2026-04-20 15:42:34 +02:00
1 parent ff9860023b
commit 17baba1490
11 files changed
+536 -405

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
package store
import (
"sync"
"time"
)
const MaxHistoryPoints = 10000
type Sample struct {
At time.Time
Value int
}
type Store struct {
mu sync.RWMutex
value int
updatedAt time.Time
history []Sample
}
func (s *Store) Get() int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.value
}
func (s *Store) Set(v int) {
s.mu.Lock()
s.value = v
now := time.Now()
s.updatedAt = now
s.history = append(s.history, Sample{At: now, Value: v})
s.trimHistoryLocked(now)
s.mu.Unlock()
}
func (s *Store) trimHistoryLocked(now time.Time) {
cutoff := now.Add(-12 * time.Hour)
keep := s.history[:0]
for _, point := range s.history {
if point.At.After(cutoff) {
keep = append(keep, point)
}
}
if len(keep) > MaxHistoryPoints {
keep = keep[len(keep)-MaxHistoryPoints:]
}
s.history = keep
}
func (s *Store) Snapshot() (int, time.Time, []Sample) {
s.mu.RLock()
defer s.mu.RUnlock()
history := append([]Sample(nil), s.history...)
return s.value, s.updatedAt, history
}