-
Notifications
You must be signed in to change notification settings - Fork 23
/
function.go
102 lines (87 loc) · 2.75 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 mostDeviant
import (
"container/heap"
"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 mostDeviant struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &mostDeviant{}
functions := []string{"mostDeviant"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// mostDeviant(seriesList, n) -or- mostDeviant(n, seriesList)
func (f *mostDeviant) Do(e parser.Expr, from, until int32, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
var nArg int
if !e.Args()[0].IsConst() {
// mostDeviant(seriesList, n)
nArg = 1
}
seriesArg := nArg ^ 1 // XOR to make seriesArg the opposite argument. ( 0^1 -> 1 ; 1^1 -> 0 )
n, err := e.GetIntArg(nArg)
if err != nil {
return nil, err
}
args, err := helper.GetSeriesArg(e.Args()[seriesArg], from, until, values)
if err != nil {
return nil, err
}
var mh types.MetricHeap
for index, arg := range args {
variance := helper.VarianceValue(arg.Values, arg.IsAbsent)
if math.IsNaN(variance) {
continue
}
if len(mh) < n {
heap.Push(&mh, types.MetricHeapElement{Idx: index, Val: variance})
continue
}
if variance > mh[0].Val {
mh[0].Idx = index
mh[0].Val = variance
heap.Fix(&mh, 0)
}
}
results := make([]*types.MetricData, len(mh))
for len(mh) > 0 {
v := heap.Pop(&mh).(types.MetricHeapElement)
results[len(mh)] = args[v.Idx]
}
return results, err
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *mostDeviant) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"mostDeviant": {
Description: "Takes one metric or a wildcard seriesList followed by an integer N.\nDraws the N most deviant metrics.\nTo find the deviants, the standard deviation (sigma) of each series\nis taken and ranked. The top N standard deviations are returned.\n\n Example:\n\n.. code-block:: none\n\n &target=mostDeviant(server*.instance*.memory.free, 5)\n\nDraws the 5 instances furthest from the average memory free.",
Function: "mostDeviant(seriesList, n)",
Group: "Filter Series",
Module: "graphite.render.functions",
Name: "mostDeviant",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "n",
Required: true,
Type: types.Integer,
},
},
},
}
}