/
function.go
89 lines (78 loc) · 2.35 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
package rangeOfSeries
import (
"fmt"
"github.com/go-graphite/carbonapi/expr/helper"
"github.com/go-graphite/carbonapi/expr/interfaces"
"github.com/go-graphite/carbonapi/expr/types"
"github.com/go-graphite/carbonapi/pkg/parser"
"math"
)
type rangeOfSeries struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &rangeOfSeries{}
functions := []string{"rangeOfSeries"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// rangeOfSeries(*seriesLists)
func (f *rangeOfSeries) Do(e parser.Expr, from, until int32, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
series, err := helper.GetSeriesArg(e.Args()[0], from, until, values)
if err != nil {
return nil, err
}
r := *series[0]
r.Name = fmt.Sprintf("%s(%s)", e.Target(), e.RawArgs())
r.Values = make([]float64, len(series[0].Values))
r.IsAbsent = make([]bool, len(series[0].Values))
for i := range series[0].Values {
var min, max float64
count := 0
for _, s := range series {
if s.IsAbsent[i] {
continue
}
if count == 0 {
min = s.Values[i]
max = s.Values[i]
} else {
min = math.Min(min, s.Values[i])
max = math.Max(max, s.Values[i])
}
count++
}
if count >= 2 {
r.Values[i] = max - min
} else {
r.IsAbsent[i] = true
}
}
return []*types.MetricData{&r}, err
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *rangeOfSeries) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"rangeOfSeries": {
Description: "Takes a wildcard seriesList.\nDistills down a set of inputs into the range of the series\n\nExample:\n\n.. code-block:: none\n\n &target=rangeOfSeries(Server*.connections.total)\n\nThis is an alias for :py:func:`aggregate <aggregate>` with aggregation ``rangeOf``.",
Function: "rangeOfSeries(*seriesLists)",
Group: "Combine",
Module: "graphite.render.functions",
Name: "rangeOfSeries",
Params: []types.FunctionParam{
{
Multiple: true,
Name: "seriesLists",
Required: true,
Type: types.SeriesList,
},
},
},
}
}