/
function.go
108 lines (94 loc) · 2.81 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
package keepLastValue
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 keepLastValue struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &keepLastValue{}
functions := []string{"keepLastValue"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// keepLastValue(seriesList, limit=inf)
func (f *keepLastValue) Do(e parser.Expr, from, until int32, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
arg, err := helper.GetSeriesArg(e.Args()[0], from, until, values)
if err != nil {
return nil, err
}
keep, err := e.GetIntNamedOrPosArgDefault("limit", 1, -1)
if err != nil {
return nil, err
}
_, ok := e.NamedArgs()["limit"]
if !ok {
ok = len(e.Args()) > 1
}
var results []*types.MetricData
for _, a := range arg {
var name string
if ok {
name = fmt.Sprintf("keepLastValue(%s,%d)", a.Name, keep)
} else {
name = fmt.Sprintf("keepLastValue(%s)", a.Name)
}
r := *a
r.Name = name
r.Values = make([]float64, len(a.Values))
r.IsAbsent = make([]bool, len(a.Values))
prev := math.NaN()
missing := 0
for i, v := range a.Values {
if a.IsAbsent[i] {
if (keep < 0 || missing < keep) && !math.IsNaN(prev) {
r.Values[i] = prev
missing++
} else {
r.IsAbsent[i] = true
}
continue
}
missing = 0
prev = v
r.Values[i] = v
}
results = append(results, &r)
}
return results, err
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *keepLastValue) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"keepLastValue": {
Description: "Takes one metric or a wildcard seriesList, and optionally a limit to the number of 'None' values to skip over.\nContinues the line with the last received value when gaps ('None' values) appear in your data, rather than breaking your line.\n\nExample:\n\n.. code-block:: none\n\n &target=keepLastValue(Server01.connections.handled)\n &target=keepLastValue(Server01.connections.handled, 10)",
Function: "keepLastValue(seriesList, limit=inf)",
Group: "Transform",
Module: "graphite.render.functions",
Name: "keepLastValue",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Default: types.NewSuggestion("INF"),
Name: "limit",
Type: types.Integer,
},
},
},
}
}