-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.go
73 lines (66 loc) · 1.38 KB
/
aggregate.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
package carbon
// AggregationFunc is a type for custom flush aggregation funcs
type AggregationFunc func(mm []Metric) Metric
// AggregateSum will sum metrics
func AggregateSum(mm []Metric) Metric {
return Metric{
Name: mm[0].Name,
Value: func() (sum float64) {
for i := range mm {
sum += mm[i].Value
}
return
}(),
Time: mm[len(mm)-1].Time,
}
}
// AggregateAvg will calculate average value of metrics
func AggregateAvg(mm []Metric) Metric {
return Metric{
Name: mm[0].Name,
Value: func() (avg float64) {
for i := range mm {
avg += mm[i].Value
}
avg /= float64(len(mm))
return
}(),
Time: mm[len(mm)-1].Time,
}
}
// AggregateMin will store min value of metrics
func AggregateMin(mm []Metric) (m Metric) {
m = mm[0]
for i := range mm {
if mm[i].Value < m.Value {
m = mm[i]
}
}
return
}
// AggregateMax will store max value of metrics
func AggregateMax(mm []Metric) (m Metric) {
m = mm[0]
for i := range mm {
if mm[i].Value > m.Value {
m = mm[i]
}
}
return
}
// AggregateFirst will store first value of metrics
func AggregateFirst(mm []Metric) Metric {
return Metric{
Name: mm[0].Name,
Value: mm[0].Value,
Time: mm[0].Time,
}
}
// AggregateLast will store first value of metrics
func AggregateLast(mm []Metric) Metric {
return Metric{
Name: mm[len(mm)-1].Name,
Value: mm[len(mm)-1].Value,
Time: mm[len(mm)-1].Time,
}
}