This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
func_scaletoseconds.go
60 lines (50 loc) · 1.44 KB
/
func_scaletoseconds.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
package expr
import (
"fmt"
"math"
"strconv"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/schema"
)
type FuncScaleToSeconds struct {
in GraphiteFunc
seconds float64
}
func NewScaleToSeconds() GraphiteFunc {
return &FuncScaleToSeconds{}
}
func (s *FuncScaleToSeconds) Signature() ([]Arg, []Arg) {
return []Arg{
ArgSeriesList{val: &s.in},
ArgFloat{key: "seconds", val: &s.seconds},
}, []Arg{ArgSeriesList{}}
}
func (s *FuncScaleToSeconds) Context(context Context) Context {
return context
}
func (s *FuncScaleToSeconds) Exec(dataMap DataMap) ([]models.Series, error) {
series, err := s.in.Exec(dataMap)
if err != nil {
return nil, err
}
output := make([]models.Series, 0, len(series))
for _, serie := range series {
out := pointSlicePool.Get().([]schema.Point)
factor := float64(s.seconds) / float64(serie.Interval)
for _, p := range serie.Datapoints {
if !math.IsNaN(p.Val) {
// round to 6 decimal places to mimic graphite
roundingFactor := math.Pow(10, 6)
p.Val = math.Round(p.Val*factor*roundingFactor) / roundingFactor
}
out = append(out, p)
}
serie.Target = fmt.Sprintf("scaleToSeconds(%s,%d)", serie.Target, int64(s.seconds))
serie.QueryPatt = serie.Target
serie.Tags = serie.CopyTagsWith("scaleToSeconds", strconv.FormatFloat(s.seconds, 'g', -1, 64))
serie.Datapoints = out
output = append(output, serie)
}
dataMap.Add(Req{}, output...)
return output, nil
}