/
function.go
170 lines (159 loc) · 5.21 KB
/
function.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package seriesList
import (
"fmt"
"github.com/bookingcom/carbonapi/expr/helper"
"github.com/bookingcom/carbonapi/expr/interfaces"
"github.com/bookingcom/carbonapi/expr/types"
"github.com/bookingcom/carbonapi/pkg/parser"
"math"
)
type seriesList struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &seriesList{}
functions := []string{"divideSeriesLists", "diffSeriesLists", "multiplySeriesLists", "powSeriesLists"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
func (f *seriesList) Do(e parser.Expr, from, until int32, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
numerators, err := helper.GetSeriesArg(e.Args()[0], from, until, values)
if err != nil {
return nil, err
}
denominators, err := helper.GetSeriesArg(e.Args()[1], from, until, values)
if err != nil {
return nil, err
}
if len(numerators) != len(denominators) {
return nil, fmt.Errorf("both %s arguments must have equal length", e.Target())
}
var results []*types.MetricData
functionName := e.Target()[:len(e.Target())-len("Lists")]
var compute func(l, r float64) float64
switch e.Target() {
case "divideSeriesLists":
compute = func(l, r float64) float64 { return l / r }
case "multiplySeriesLists":
compute = func(l, r float64) float64 { return l * r }
case "diffSeriesLists":
compute = func(l, r float64) float64 { return l - r }
case "powSeriesLists":
compute = func(l, r float64) float64 { return math.Pow(l, r) }
}
for i, numerator := range numerators {
denominator := denominators[i]
if numerator.StepTime != denominator.StepTime || len(numerator.Values) != len(denominator.Values) {
return nil, fmt.Errorf("series %s must have the same length as %s", numerator.Name, denominator.Name)
}
r := *numerator
r.Name = fmt.Sprintf("%s(%s,%s)", functionName, numerator.Name, denominator.Name)
r.Values = make([]float64, len(numerator.Values))
r.IsAbsent = make([]bool, len(numerator.Values))
for i, v := range numerator.Values {
if numerator.IsAbsent[i] || denominator.IsAbsent[i] {
r.IsAbsent[i] = true
continue
}
switch e.Target() {
case "divideSeriesLists":
if denominator.Values[i] == 0 {
r.IsAbsent[i] = true
continue
}
r.Values[i] = compute(v, denominator.Values[i])
default:
r.Values[i] = compute(v, denominator.Values[i])
}
}
results = append(results, &r)
}
return results, nil
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *seriesList) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"divideSeriesLists": {
Description: "Iterates over a two lists and divides list1[0} by list2[0}, list1[1} by list2[1} and so on.\nThe lists need to be the same length",
Function: "divideSeriesLists(dividendSeriesList, divisorSeriesList)",
Group: "Combine",
Module: "graphite.render.functions",
Name: "divideSeriesLists",
Params: []types.FunctionParam{
{
Name: "dividendSeriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "divisorSeriesList",
Required: true,
Type: types.SeriesList,
},
},
},
"diffSeriesLists": {
Description: "Iterates over a two lists and substracts list1[0} by list2[0}, list1[1} by list2[1} and so on.\nThe lists need to be the same length",
Function: "diffSeriesLists(firstSeriesList, secondSeriesList)",
Group: "Combine",
Module: "graphite.render.functions.custom",
Name: "diffSeriesLists",
Params: []types.FunctionParam{
{
Name: "firstSeriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "secondSeriesList",
Required: true,
Type: types.SeriesList,
},
},
},
"multiplySeriesLists": {
Description: "Iterates over a two lists and multiplies list1[0} by list2[0}, list1[1} by list2[1} and so on.\nThe lists need to be the same length",
Function: "multiplySeriesLists(sourceSeriesList, factorSeriesList)",
Group: "Combine",
Module: "graphite.render.functions.custom",
Name: "multiplySeriesLists",
Params: []types.FunctionParam{
{
Name: "sourceSeriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "factorSeriesList",
Required: true,
Type: types.SeriesList,
},
},
},
"powSeriesLists": {
Description: "Iterates over a two lists and do list1[0} in power of list2[0}, list1[1} in power of list2[1} and so on.\nThe lists need to be the same length",
Function: "powSeriesLists(sourceSeriesList, factorSeriesList)",
Group: "Combine",
Module: "graphite.render.functions.custom",
Name: "powSeriesLists",
Params: []types.FunctionParam{
{
Name: "sourceSeriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "factorSeriesList",
Required: true,
Type: types.SeriesList,
},
},
},
}
}