-
Notifications
You must be signed in to change notification settings - Fork 139
/
function.go
111 lines (101 loc) · 3.06 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
package lowPass
import (
"context"
"math"
"strconv"
"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"
)
type lowPass struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &lowPass{}
functions := []string{"lowPass", "lpf"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// lowPass(seriesList, cutPercent)
func (f *lowPass) Do(ctx context.Context, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
arg, err := helper.GetSeriesArg(ctx, e.Arg(0), from, until, values)
if err != nil {
return nil, err
}
cutPercent, err := e.GetFloatArg(1)
if err != nil {
return nil, err
}
cutPercentStr := strconv.FormatFloat(cutPercent, 'g', -1, 64)
results := make([]*types.MetricData, len(arg))
for j, a := range arg {
r := a.CopyLinkTags()
r.Name = "lowPass(" + a.Name + "," + cutPercentStr + ")"
r.Values = make([]float64, len(a.Values))
lowCut := int((cutPercent / 200) * float64(len(a.Values)))
highCut := len(a.Values) - lowCut
for i, v := range a.Values {
if i < lowCut || i >= highCut {
r.Values[i] = v
} else {
r.Values[i] = math.NaN()
}
}
results[j] = r
}
return results, nil
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *lowPass) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"lpf": {
Description: "Low-pass filters provide a smoother form of a signal, removing the short-term fluctuations, and leaving the longer-term trend. https://en.wikipedia.org/wiki/Low-pass_filter",
Function: "lpf(seriesList, cutPercent)",
Group: "Transform",
Module: "graphite.render.functions",
Name: "lpf",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "cutPercent",
Required: true,
Type: types.Float,
},
},
NameChange: true, // name changed
ValuesChange: true, // values changed
},
"lowPass": {
Description: "Low-pass filters provide a smoother form of a signal, removing the short-term fluctuations, and leaving the longer-term trend. https://en.wikipedia.org/wiki/Low-pass_filter",
Function: "lowPass(seriesList, cutPercent)",
Group: "Transform",
Module: "graphite.render.functions.custom",
Name: "lowPass",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "cutPercent",
Required: true,
Type: types.Float,
},
},
NameChange: true, // name changed
ValuesChange: true, // values changed
},
}
}