-
Notifications
You must be signed in to change notification settings - Fork 139
/
function.go
110 lines (98 loc) · 3.03 KB
/
function.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package integralByInterval
import (
"fmt"
"math"
"github.com/go-graphite/carbonapi/expr/helper"
"github.com/go-graphite/carbonapi/expr/interfaces"
"github.com/go-graphite/carbonapi/expr/types"
"github.com/go-graphite/carbonapi/pkg/parser"
pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
)
type integralByInterval struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &integralByInterval{}
functions := []string{"integralByInterval"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// integralByInterval(seriesList, intervalString)
func (f *integralByInterval) Do(e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
args, err := helper.GetSeriesArg(e.Args()[0], from, until, values)
if err != nil {
return nil, err
}
if len(args) == 0 {
return nil, nil
}
bucketSizeInt32, err := e.GetIntervalArg(1, 1)
if err != nil {
return nil, err
}
bucketSize := int64(bucketSizeInt32)
startTime := from
results := make([]*types.MetricData, 0, len(args))
for _, arg := range args {
current := 0.0
currentTime := arg.StartTime
name := fmt.Sprintf("integralByInterval(%s,'%s')", arg.Name, e.Args()[1].StringValue())
result := types.MetricData{FetchResponse: pb.FetchResponse{
Name: name,
Values: make([]float64, len(arg.Values)),
StepTime: arg.StepTime,
StartTime: arg.StartTime,
StopTime: arg.StopTime,
XFilesFactor: arg.XFilesFactor,
PathExpression: name,
ConsolidationFunc: arg.ConsolidationFunc,
}}
for i, v := range arg.Values {
if (currentTime-startTime)/bucketSize != (currentTime-startTime-arg.StepTime)/bucketSize {
current = 0
}
if math.IsNaN(v) {
v = 0
}
current += v
result.Values[i] = current
currentTime += arg.StepTime
}
results = append(results, &result)
}
return results, nil
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *integralByInterval) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"integralByInterval": {
Description: "This will do the same as integralByInterval() funcion, except resetting the total to 0 at the given time in the parameter “from” Useful for finding totals per hour/day/week/..",
Function: "integralByInterval(seriesList, intervalString)",
Group: "Transform",
Module: "graphite.render.functions",
Name: "integralByInterval",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
}, {
Name: "intervalString",
Required: true,
Suggestions: types.NewSuggestions(
"10min",
"1h",
"1d",
),
Type: types.Interval,
},
},
},
}
}