-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
58 lines (49 loc) · 1.11 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package tchart
import (
"math"
"github.com/VividCortex/gohistogram"
)
type Storage struct {
title string
DataLabels *RingBuffer
Data *RingBuffer
Histogram *gohistogram.NumericHistogram
NumBins int
Min float64
Max float64
Sum float64
Count int
}
func NewStorage(title string, bufsize int, histogramBinCount int) *Storage {
storage := &Storage{
title: title,
DataLabels: NewRingBuffer(bufsize),
Data: NewRingBuffer(bufsize),
Histogram: gohistogram.NewHistogram(histogramBinCount),
NumBins: histogramBinCount,
Min: math.Inf(1),
Max: math.Inf(-1),
Sum: 0,
Count: 0,
}
return storage
}
func (storage *Storage) SetBuffersize(bufsize int) {
storage.DataLabels = NewRingBuffer(bufsize)
storage.Data = NewRingBuffer(bufsize)
}
func (storage *Storage) Add(x float64, dataLabel string) {
storage.Data.Add(x)
storage.Histogram.Add(x)
if x < storage.Min {
storage.Min = x
}
if x > storage.Max {
storage.Max = x
}
storage.Sum += x
storage.Count++
if dataLabel != "" {
storage.DataLabels.Add(dataLabel)
}
}