forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_persecond.go
More file actions
72 lines (65 loc) · 1.71 KB
/
Copy pathfunc_persecond.go
File metadata and controls
72 lines (65 loc) · 1.71 KB
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
package expr
import (
"fmt"
"math"
"github.com/grafana/metrictank/api/models"
"gopkg.in/raintank/schema.v1"
)
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(cache map[Req][]models.Series) ([]models.Series, error) {
series, _, err := consumeFuncs(cache, s.in)
if err != nil {
return nil, err
}
maxValue := math.NaN()
if s.maxValue > 0 {
maxValue = float64(s.maxValue)
}
var outputs []models.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()
}
}
s := models.Series{
Target: fmt.Sprintf("perSecond(%s)", serie.Target),
QueryPatt: fmt.Sprintf("perSecond(%s)", serie.QueryPatt),
Tags: serie.Tags,
Datapoints: out,
Interval: serie.Interval,
}
outputs = append(outputs, s)
cache[Req{}] = append(cache[Req{}], s)
}
return outputs, nil
}