/
function.go
102 lines (92 loc) · 3.33 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
package sortBy
import (
"github.com/bookingcom/carbonapi/expr/helper"
"github.com/bookingcom/carbonapi/expr/interfaces"
"github.com/bookingcom/carbonapi/expr/types"
"github.com/bookingcom/carbonapi/pkg/parser"
"sort"
)
type sortBy struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &sortBy{}
functions := []string{"sortByMaxima", "sortByMinima", "sortByTotal"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// sortByMaxima(seriesList), sortByMinima(seriesList), sortByTotal(seriesList)
func (f *sortBy) Do(e parser.Expr, from, until int32, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
original, err := helper.GetSeriesArg(e.Args()[0], from, until, values)
if err != nil {
return nil, err
}
arg := make([]*types.MetricData, len(original))
copy(arg, original)
vals := make([]float64, len(arg))
for i, a := range arg {
switch e.Target() {
case "sortByTotal":
vals[i] = helper.SummarizeValues("sum", a.Values)
case "sortByMaxima":
vals[i] = helper.SummarizeValues("max", a.Values)
case "sortByMinima":
vals[i] = 1 / helper.SummarizeValues("min", a.Values)
}
}
sort.Sort(helper.ByVals{Vals: vals, Series: arg})
return arg, nil
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *sortBy) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"sortByMaxima": {
Description: "Takes one metric or a wildcard seriesList.\n\nSorts the list of metrics in descending order by the maximum value across the time period\nspecified. Useful with the &areaMode=all parameter, to keep the\nlowest value lines visible.\n\nExample:\n\n.. code-block:: none\n\n &target=sortByMaxima(server*.instance*.memory.free)",
Function: "sortByMaxima(seriesList)",
Group: "Sorting",
Module: "graphite.render.functions",
Name: "sortByMaxima",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
},
},
"sortByMinima": {
Description: "Takes one metric or a wildcard seriesList.\n\nSorts the list of metrics by the lowest value across the time period\nspecified, including only series that have a maximum value greater than 0.\n\nExample:\n\n.. code-block:: none\n\n &target=sortByMinima(server*.instance*.memory.free)",
Function: "sortByMinima(seriesList)",
Group: "Sorting",
Module: "graphite.render.functions",
Name: "sortByMinima",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
},
},
"sortByTotal": {
Description: "Takes one metric or a wildcard seriesList.\n\nSorts the list of metrics in descending order by the sum of values across the time period\nspecified.",
Function: "sortByTotal(seriesList)",
Group: "Sorting",
Module: "graphite.render.functions",
Name: "sortByTotal",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
},
},
}
}