This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
func_persecond.go
74 lines (64 loc) · 1.79 KB
/
func_persecond.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
package expr
import (
"fmt"
"math"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/consolidation"
"github.com/grafana/metrictank/schema"
)
type FuncPerSecond struct {
in []GraphiteFunc
maxValue int64
}
func NewPerSecond() GraphiteFunc {
return &FuncPerSecond{}
}
func (s *FuncPerSecond) Signature() ([]Arg, []Arg) {
return []Arg{
ArgSeriesLists{val: &s.in},
ArgInt{key: "maxValue", opt: true, validator: []Validator{IntPositive}, val: &s.maxValue},
}, []Arg{
ArgSeriesList{},
}
}
func (s *FuncPerSecond) Context(context Context) Context {
context.consol = 0
return context
}
func (s *FuncPerSecond) Exec(dataMap DataMap) ([]models.Series, error) {
series, _, err := consumeFuncs(dataMap, s.in)
if err != nil {
return nil, err
}
maxValue := math.NaN()
if s.maxValue > 0 {
maxValue = float64(s.maxValue)
}
outSeries := make([]models.Series, 0, len(series))
for _, serie := range series {
out := pointSlicePool.Get().([]schema.Point)
for i, v := range serie.Datapoints {
out = append(out, schema.Point{Ts: v.Ts})
if i == 0 || math.IsNaN(v.Val) || math.IsNaN(serie.Datapoints[i-1].Val) {
out[i].Val = math.NaN()
continue
}
diff := v.Val - serie.Datapoints[i-1].Val
if diff >= 0 {
out[i].Val = diff / float64(serie.Interval)
} else if !math.IsNaN(maxValue) && maxValue >= v.Val {
out[i].Val = (maxValue + diff + 1) / float64(serie.Interval)
} else {
out[i].Val = math.NaN()
}
}
serie.Target = fmt.Sprintf("perSecond(%s)", serie.Target)
serie.QueryPatt = fmt.Sprintf("perSecond(%s)", serie.QueryPatt)
serie.Consolidator = consolidation.None
serie.QueryCons = consolidation.None
serie.Datapoints = out
outSeries = append(outSeries, serie)
}
dataMap.Add(Req{}, outSeries...)
return outSeries, nil
}