-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregation.go
131 lines (110 loc) · 3.59 KB
/
aggregation.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metric
import (
"sort"
)
// Aggregation modes which should be used for data aggregation. Eg. [sum, min, max].
type AggregationName string
const (
SumAggregation = "sum"
MaxAggregation = "max"
MinAggregation = "min"
DefaultAggregation = "sum"
)
type AggregationNames []AggregationName
var OnlySumAggregation = AggregationNames{SumAggregation}
var OnlyDefaultAggregation = AggregationNames{DefaultAggregation}
var AggregatingFunctions = map[AggregationName]func([]int64) int64{
SumAggregation: SumAggregate,
MaxAggregation: MaxAggregate,
MinAggregation: MinAggregate,
}
// SortableInt64 implements sort.Interface for []int64. This allows to use built in sort with int64.
type SortableInt64 []int64
func (a SortableInt64) Len() int { return len(a) }
func (a SortableInt64) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortableInt64) Less(i, j int) bool { return a[i] < a[j] }
// AggregateData aggregates all the data from dataList using AggregatingFunction with name aggregateName.
// Standard data aggregation function.
func AggregateData(metricList []Metric, metricName string, aggregationName AggregationName) Metric {
_, isAggregateAvailable := AggregatingFunctions[aggregationName]
if !isAggregateAvailable {
aggregationName = DefaultAggregation
}
aggrMap, newLabel := AggregatingMapFromDataList(metricList, metricName)
Xs := SortableInt64{}
for k := range aggrMap {
Xs = append(Xs, k)
}
newDataPoints := []DataPoint{}
sort.Sort(Xs) // ensure X data points are sorted
for _, x := range Xs {
y := AggregatingFunctions[aggregationName](aggrMap[x])
newDataPoints = append(newDataPoints, DataPoint{x, y})
}
// Create new data cell
return Metric{
DataPoints: newDataPoints,
MetricName: metricName,
Label: newLabel,
Aggregate: aggregationName,
}
}
// AggregatingMapFromDataList for all Data entries of given metric generates a cumulative map X -> [List of all Ys at this X].
// Afterwards this list of Ys can be easily aggregated.
func AggregatingMapFromDataList(metricList []Metric, metricName string) (map[int64][]int64, Label) {
newLabel := Label{}
aggrMap := make(map[int64][]int64, 0)
for _, data := range metricList {
if data.MetricName != metricName {
continue
}
newLabel = newLabel.AddMetricLabel(data.Label) // update label of resulting data
for _, dataPoint := range data.DataPoints {
_, isXPresent := aggrMap[dataPoint.X]
if !isXPresent {
aggrMap[dataPoint.X] = []int64{}
}
aggrMap[dataPoint.X] = append(aggrMap[dataPoint.X], dataPoint.Y)
}
}
return aggrMap, newLabel
}
// Implement aggregating functions:
func SumAggregate(values []int64) int64 {
result := int64(0)
for _, e := range values {
result += e
}
return result
}
func MaxAggregate(values []int64) int64 {
result := values[0]
for _, e := range values {
if e > result {
result = e
}
}
return result
}
func MinAggregate(values []int64) int64 {
result := values[0]
for _, e := range values {
if e < result {
result = e
}
}
return result
}