This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
func_transformnull.go
64 lines (54 loc) · 1.3 KB
/
func_transformnull.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
package expr
import (
"fmt"
"math"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/schema"
)
type FuncTransformNull struct {
in GraphiteFunc
def float64
}
func NewTransformNull() GraphiteFunc {
return &FuncTransformNull{nil, math.NaN()}
}
func (s *FuncTransformNull) Signature() ([]Arg, []Arg) {
return []Arg{
ArgSeriesList{val: &s.in},
ArgFloat{key: "default", opt: true, val: &s.def},
}, []Arg{ArgSeriesList{}}
}
func (s *FuncTransformNull) Context(context Context) Context {
return context
}
func (s *FuncTransformNull) Exec(dataMap DataMap) ([]models.Series, error) {
series, err := s.in.Exec(dataMap)
if err != nil {
return nil, err
}
custom := true
if math.IsNaN(s.def) {
s.def = 0
custom = false
}
output := make([]models.Series, 0, len(series))
for _, serie := range series {
out := pointSlicePool.Get().([]schema.Point)
for _, p := range serie.Datapoints {
if math.IsNaN(p.Val) {
p.Val = s.def
}
out = append(out, p)
}
if custom {
serie.Target = fmt.Sprintf("transformNull(%s,%g)", serie.Target, s.def)
} else {
serie.Target = fmt.Sprintf("transformNull(%s)", serie.Target)
}
serie.QueryPatt = serie.Target
serie.Datapoints = out
output = append(output, serie)
}
dataMap.Add(Req{}, output...)
return output, nil
}