This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
func_integral.go
59 lines (48 loc) · 1.27 KB
/
func_integral.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
package expr
import (
"fmt"
"math"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/consolidation"
"github.com/grafana/metrictank/schema"
)
type FuncIntegral struct {
in GraphiteFunc
}
func NewIntegral() GraphiteFunc {
return &FuncIntegral{}
}
func (s *FuncIntegral) Signature() ([]Arg, []Arg) {
return []Arg{
ArgSeriesList{val: &s.in}}, []Arg{ArgSeriesList{}}
}
func (s *FuncIntegral) Context(context Context) Context {
return context
}
func (s *FuncIntegral) Exec(dataMap DataMap) ([]models.Series, error) {
series, err := s.in.Exec(dataMap)
if err != nil {
return nil, err
}
outSeries := make([]models.Series, 0, len(series))
for _, serie := range series {
serie.Target = fmt.Sprintf("integral(%s)", serie.Target)
serie.Tags = serie.CopyTagsWith("integral", "1")
serie.QueryPatt = fmt.Sprintf("integral(%s)", serie.QueryPatt)
serie.Consolidator = consolidation.None
serie.QueryCons = consolidation.None
current := 0.0
out := pointSlicePool.Get().([]schema.Point)
for _, p := range serie.Datapoints {
if !math.IsNaN(p.Val) {
current += p.Val
p.Val = current
}
out = append(out, p)
}
serie.Datapoints = out
outSeries = append(outSeries, serie)
}
dataMap.Add(Req{}, outSeries...)
return outSeries, nil
}