-
Notifications
You must be signed in to change notification settings - Fork 23
/
function.go
130 lines (122 loc) · 4.2 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
package minMax
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 minMax struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &minMax{}
functions := []string{"minSeries", "min", "maxSeries", "max"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// maxSeries(*seriesLists)
// alias: max
// minSeries(*seriesLists)
// alias: min
func (f *minMax) Do(e parser.Expr, from, until int32, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
args, err := helper.GetSeriesArgsAndRemoveNonExisting(e, from, until, values)
if err != nil {
return nil, err
}
switch e.Target() {
case "maxSeries", "max":
return helper.AggregateSeries(e, args, func(values []float64) float64 {
max := math.Inf(-1)
for _, value := range values {
if value > max {
max = value
}
}
return max
})
case "minSeries", "min":
return helper.AggregateSeries(e, args, func(values []float64) float64 {
min := math.Inf(1)
for _, value := range values {
if value < min {
min = value
}
}
return min
})
}
return nil, fmt.Errorf("unsupported target: %v", e.Target())
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *minMax) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"minSeries": {
Description: "Takes one metric or a wildcard seriesList.\nFor each datapoint from each metric passed in, pick the minimum value and graph it.\n\nExample:\n\n.. code-block:: none\n\n &target=minSeries(Server*.connections.total)\n\nThis is an alias for :py:func:`aggregate <aggregate>` with aggregation ``min``.",
Function: "minSeries(*seriesLists)",
Group: "Combine",
Module: "graphite.render.functions",
Name: "minSeries",
Params: []types.FunctionParam{
{
Multiple: true,
Name: "seriesLists",
Required: true,
Type: types.SeriesList,
},
},
},
"min": {
Description: "Takes one metric or a wildcard seriesList.\nFor each datapoint from each metric passed in, pick the minimum value and graph it.\n\nExample:\n\n.. code-block:: none\n\n &target=minSeries(Server*.connections.total)\n\nThis is an alias for :py:func:`aggregate <aggregate>` with aggregation ``min``.",
Function: "minSeries(*seriesLists)",
Group: "Combine",
Module: "graphite.render.functions",
Name: "minSeries",
Params: []types.FunctionParam{
{
Multiple: true,
Name: "seriesLists",
Required: true,
Type: types.SeriesList,
},
},
},
"maxSeries": {
Description: "Takes one metric or a wildcard seriesList.\nFor each datapoint from each metric passed in, pick the maximum value and graph it.\n\nExample:\n\n.. code-block:: none\n\n &target=maxSeries(Server*.connections.total)\n\nThis is an alias for :py:func:`aggregate <aggregate>` with aggregation ``max``.",
Function: "maxSeries(*seriesLists)",
Group: "Combine",
Module: "graphite.render.functions",
Name: "maxSeries",
Params: []types.FunctionParam{
{
Multiple: true,
Name: "seriesLists",
Required: true,
Type: types.SeriesList,
},
},
},
"max": {
Description: "Takes one metric or a wildcard seriesList.\nFor each datapoint from each metric passed in, pick the maximum value and graph it.\n\nExample:\n\n.. code-block:: none\n\n &target=maxSeries(Server*.connections.total)\n\nThis is an alias for :py:func:`aggregate <aggregate>` with aggregation ``max``.",
Function: "maxSeries(*seriesLists)",
Group: "Combine",
Module: "graphite.render.functions",
Name: "maxSeries",
Params: []types.FunctionParam{
{
Multiple: true,
Name: "seriesLists",
Required: true,
Type: types.SeriesList,
},
},
},
}
}