This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
aggregations.go
112 lines (97 loc) · 3.08 KB
/
aggregations.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
package conf
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/alyu/configparser"
)
// Aggregations holds the aggregation definitions
type Aggregations struct {
Data []Aggregation
DefaultAggregation Aggregation
}
type Aggregation struct {
Name string
Pattern *regexp.Regexp
XFilesFactor float64
AggregationMethod []Method
}
// NewAggregations create instance of Aggregations
func NewAggregations() Aggregations {
return Aggregations{
Data: make([]Aggregation, 0),
DefaultAggregation: Aggregation{
Name: "default",
Pattern: regexp.MustCompile(".*"),
XFilesFactor: 0.5,
AggregationMethod: []Method{Avg},
},
}
}
// ReadAggregations returns the defined aggregations from a storage-aggregation.conf file
// and adds the default
func ReadAggregations(file string) (Aggregations, error) {
config, err := configparser.Read(file)
if err != nil {
return Aggregations{}, err
}
sections, err := config.AllSections()
if err != nil {
return Aggregations{}, err
}
result := NewAggregations()
for _, s := range sections {
item := Aggregation{}
item.Name = strings.Trim(strings.SplitN(s.String(), "\n", 2)[0], " []")
if item.Name == "" || strings.HasPrefix(item.Name, "#") {
continue
}
item.Pattern, err = regexp.Compile(s.ValueOf("pattern"))
if err != nil {
return Aggregations{}, fmt.Errorf("[%s]: failed to parse pattern %q: %s", item.Name, s.ValueOf("pattern"), err.Error())
}
item.XFilesFactor, err = strconv.ParseFloat(s.ValueOf("xFilesFactor"), 64)
if err != nil {
return Aggregations{}, fmt.Errorf("[%s]: failed to parse xFilesFactor %q: %s", item.Name, s.ValueOf("xFilesFactor"), err.Error())
}
aggregationMethodStr := s.ValueOf("aggregationMethod")
methodStrs := strings.Split(aggregationMethodStr, ",")
for _, methodStr := range methodStrs {
switch methodStr {
case "average", "avg":
item.AggregationMethod = append(item.AggregationMethod, Avg)
case "sum":
item.AggregationMethod = append(item.AggregationMethod, Sum)
case "last":
item.AggregationMethod = append(item.AggregationMethod, Lst)
case "max":
item.AggregationMethod = append(item.AggregationMethod, Max)
case "min":
item.AggregationMethod = append(item.AggregationMethod, Min)
default:
return result, fmt.Errorf("[%s]: unknown aggregation method %q", item.Name, methodStr)
}
}
result.Data = append(result.Data, item)
}
return result, nil
}
// Match returns the correct aggregation setting for the given metric
// it can always find a valid setting, because there's a default catch all
// also returns the index of the setting, to efficiently reference it
func (a Aggregations) Match(metric string) (uint16, Aggregation) {
for i, s := range a.Data {
if s.Pattern.MatchString(metric) {
return uint16(i), s
}
}
return uint16(len(a.Data)), a.DefaultAggregation
}
// Get returns the aggregation setting corresponding to the given index
func (a Aggregations) Get(i uint16) Aggregation {
if i+1 > uint16(len(a.Data)) {
return a.DefaultAggregation
}
return a.Data[i]
}