This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
func_derivative.go
61 lines (51 loc) · 1.35 KB
/
func_derivative.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
package expr
import (
"fmt"
"math"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/consolidation"
"github.com/grafana/metrictank/schema"
)
type FuncDerivative struct {
in GraphiteFunc
}
func NewDerivative() GraphiteFunc {
return &FuncDerivative{}
}
func (s *FuncDerivative) Signature() ([]Arg, []Arg) {
return []Arg{
ArgSeriesList{val: &s.in}}, []Arg{ArgSeriesList{}}
}
func (s *FuncDerivative) Context(context Context) Context {
return context
}
func (s *FuncDerivative) 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("derivative(%s)", serie.Target)
serie.Tags = serie.CopyTagsWith("derivative", "1")
serie.QueryPatt = fmt.Sprintf("derivative(%s)", serie.QueryPatt)
serie.Consolidator = consolidation.None
serie.QueryCons = consolidation.None
out := pointSlicePool.Get().([]schema.Point)
prev := math.NaN()
for _, p := range serie.Datapoints {
val := p.Val
if math.IsNaN(prev) || math.IsNaN(val) {
p.Val = math.NaN()
} else {
p.Val -= prev
}
prev = val
out = append(out, p)
}
serie.Datapoints = out
outSeries = append(outSeries, serie)
}
dataMap.Add(Req{}, outSeries...)
return outSeries, nil
}