-
Notifications
You must be signed in to change notification settings - Fork 1
/
type_avg.go
95 lines (82 loc) · 1.78 KB
/
type_avg.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import "math"
func init() {
mt := metricType{
create: func() metric { return &avgMetric{} },
channels: []string{"avg", "avg-cnt"},
defaults: []float64{math.NaN(), 0},
persist: []bool{false, false},
aggregator: createAvgAggregator,
}
registerMetricType(Averager, mt)
}
type avgMetric struct {
tickSum, tickCount, sum, count float64
}
func (m *avgMetric) init([]float64) {
}
func (m *avgMetric) inject(metric *Metric) {
m.tickSum += metric.Value / metric.SampleRate
m.tickCount += 1 / metric.SampleRate
}
func (m *avgMetric) tick() []float64 {
sum, count := m.tickSum, m.tickCount
m.tickSum, m.tickCount = 0, 0
m.sum += sum
m.count += count
return []float64{sum / count, count}
}
func (m *avgMetric) flush() []float64 {
sum, count := m.sum, m.count
m.sum, m.count = 0, 0
return []float64{sum / count, count}
}
type avgAggregator struct {
avgOut, cntOut int
sum, cnt float64
}
func createAvgAggregator(chs []string) aggregator {
aggr := &avgAggregator{avgOut: -1, cntOut: -1}
for i, ch := range chs {
if ch == "avg" {
aggr.avgOut = i
} else {
// "avg-cnt"
aggr.cntOut = i
}
}
return aggr
}
func (aggr *avgAggregator) channels() []int {
if aggr.avgOut == -1 {
return []int{1}
} else {
return []int{0, 1}
}
}
func (aggr *avgAggregator) init([]float64) {
}
func (aggr *avgAggregator) put(data []float64) {
if aggr.avgOut != -1 {
aggr.sum += data[0] * data[1]
aggr.cnt += data[1]
} else {
aggr.cnt += data[0]
}
}
func (aggr *avgAggregator) get() []float64 {
avg, cnt := aggr.sum/aggr.cnt, aggr.cnt
aggr.sum, aggr.cnt = 0, 0
switch aggr.avgOut {
case -1:
return []float64{cnt}
case 0:
if aggr.cntOut == -1 {
return []float64{avg}
} else {
return []float64{avg, cnt}
}
default:
return []float64{cnt, avg}
}
}