This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
func_removeemptyseries.go
66 lines (52 loc) · 1.39 KB
/
func_removeemptyseries.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
package expr
import (
"math"
"github.com/grafana/metrictank/api/models"
)
type FuncRemoveEmptySeries struct {
in GraphiteFunc
xFilesFactor float64
}
func NewRemoveEmptySeries() GraphiteFunc {
return &FuncRemoveEmptySeries{xFilesFactor: 0}
}
func (s *FuncRemoveEmptySeries) Signature() ([]Arg, []Arg) {
return []Arg{
ArgSeriesList{val: &s.in},
ArgFloat{key: "xFilesFactor", val: &s.xFilesFactor, opt: true, validator: []Validator{WithinZeroOneInclusiveInterval}},
}, []Arg{
ArgSeriesList{},
}
}
func (s *FuncRemoveEmptySeries) Context(context Context) Context {
return context
}
func (s *FuncRemoveEmptySeries) Exec(dataMap DataMap) ([]models.Series, error) {
series, err := s.in.Exec(dataMap)
if err != nil {
return nil, err
}
if math.IsNaN(s.xFilesFactor) {
s.xFilesFactor = 0.0
}
var output []models.Series
for _, serie := range series {
notNull := 0
for _, p := range serie.Datapoints {
if !math.IsNaN(p.Val) {
notNull++
}
}
if xffCheck(notNull, len(serie.Datapoints), s.xFilesFactor) {
output = append(output, serie)
}
}
return output, nil
}
// xffCheck returns whether a series with given notNull and total point counts should be included based on the xFilesFactor
func xffCheck(notNull int, total int, xFilesFactor float64) bool {
if notNull == 0 || total == 0 {
return false
}
return float64(notNull)/float64(total) >= xFilesFactor
}