This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
func_constantline.go
68 lines (56 loc) · 1.7 KB
/
func_constantline.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
package expr
import (
"fmt"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/schema"
)
type FuncConstantLine struct {
value float64
first uint32
last uint32
}
func NewConstantLine() GraphiteFunc {
return &FuncConstantLine{}
}
func (s *FuncConstantLine) Signature() ([]Arg, []Arg) {
return []Arg{
ArgFloat{key: "value", val: &s.value},
}, []Arg{
ArgSeriesList{},
}
}
func (s *FuncConstantLine) Context(context Context) Context {
// Graphite implements constantLine in a non-standard way with repspect to time.
// Normally, graphite is from exclusive, to inclusive, but for constantLine,
// is from inclusive, to inclusive. This means the first datapoint has a ts 1s
// earlier than what it would normally be. We do the same here.
s.first = context.from - 1
s.last = context.to - 1
return context
}
func (s *FuncConstantLine) Exec(dataMap DataMap) ([]models.Series, error) {
out := pointSlicePool.Get().([]schema.Point)
out = append(out, schema.Point{Val: s.value, Ts: s.first})
diff := s.last - s.first
// edge cases
// if first = last - 1, return one datapoint to user, so don't add more points
// if first = last - 2, return two datapoints where timestamps are first, first +1
if diff > 2 {
out = append(out,
schema.Point{Val: s.value, Ts: s.first + uint32(diff/2.0)},
schema.Point{Val: s.value, Ts: s.last},
)
} else if diff == 2 {
out = append(out, schema.Point{Val: s.value, Ts: s.first + 1})
}
strValue := fmt.Sprintf("%g", s.value)
outputs := make([]models.Series, 1)
outputs[0] = models.Series{
Target: strValue,
QueryPatt: strValue,
Datapoints: out,
}
outputs[0].SetTags()
dataMap.Add(Req{}, outputs...)
return outputs, nil
}