-
Notifications
You must be signed in to change notification settings - Fork 23
/
function.go
131 lines (112 loc) · 3.8 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
package multiplySeriesWithWildcards
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"
"strings"
)
type multiplySeriesWithWildcards struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &multiplySeriesWithWildcards{}
functions := []string{"multiplySeriesWithWildcards"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// multiplySeriesWithWildcards(seriesList, *position)
func (f *multiplySeriesWithWildcards) Do(e parser.Expr, from, until int32, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
/* TODO(dgryski): make sure the arrays are all the same 'size'
(duplicated from sumSeriesWithWildcards because of similar logic but multiplication) */
args, err := helper.GetSeriesArg(e.Args()[0], from, until, values)
if err != nil {
return nil, err
}
fields, err := e.GetIntArgs(1)
if err != nil {
return nil, err
}
var results []*types.MetricData
nodeList := []string{}
groups := make(map[string][]*types.MetricData)
for _, a := range args {
metric := helper.ExtractMetric(a.Name)
nodes := strings.Split(metric, ".")
var s []string
// Yes, this is O(n^2), but len(nodes) < 10 and len(fields) < 3
// Iterating an int slice is faster than a map for n ~ 30
// http://www.antoine.im/posts/someone_is_wrong_on_the_internet
for i, n := range nodes {
if !helper.Contains(fields, i) {
s = append(s, n)
}
}
node := strings.Join(s, ".")
if len(groups[node]) == 0 {
nodeList = append(nodeList, node)
}
groups[node] = append(groups[node], a)
}
for _, series := range nodeList {
args := groups[series]
r := *args[0]
r.Name = fmt.Sprintf("multiplySeriesWithWildcards(%s)", series)
r.Values = make([]float64, len(args[0].Values))
r.IsAbsent = make([]bool, len(args[0].Values))
atLeastOne := make([]bool, len(args[0].Values))
hasVal := make([]bool, len(args[0].Values))
for _, arg := range args {
for i, v := range arg.Values {
if arg.IsAbsent[i] {
continue
}
atLeastOne[i] = true
if hasVal[i] == false {
r.Values[i] = v
hasVal[i] = true
} else {
r.Values[i] *= v
}
}
}
for i, v := range atLeastOne {
if !v {
r.IsAbsent[i] = true
}
}
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 *multiplySeriesWithWildcards) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"multiplySeriesWithWildcards": {
Description: "Call multiplySeries after inserting wildcards at the given position(s).\n\nExample:\n\n.. code-block:: none\n\n &target=multiplySeriesWithWildcards(web.host-[0-7}.{avg-response,total-request}.value, 2)\n\nThis would be the equivalent of\n\n.. code-block:: none\n\n &target=multiplySeries(web.host-0.{avg-response,total-request}.value)&target=multiplySeries(web.host-1.{avg-response,total-request}.value)...\n\nThis is an alias for :py:func:`aggregateWithWildcards <aggregateWithWildcards>` with aggregation ``multiply``.",
Function: "multiplySeriesWithWildcards(seriesList, *position)",
Group: "Combine",
Module: "graphite.render.functions",
Name: "multiplySeriesWithWildcards",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Multiple: true,
Name: "position",
Type: types.Node,
},
},
},
}
}