forked from go-graphite/carbonapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.go
172 lines (152 loc) · 4.56 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package smartSummarize
import (
"context"
"fmt"
"math"
pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
"github.com/grafana/carbonapi/expr/consolidations"
"github.com/grafana/carbonapi/expr/helper"
"github.com/grafana/carbonapi/expr/interfaces"
"github.com/grafana/carbonapi/expr/types"
"github.com/grafana/carbonapi/pkg/parser"
)
type smartSummarize struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &smartSummarize{}
functions := []string{"smartSummarize"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// smartSummarize(seriesList, intervalString, alignToInterval=False)
func (f *smartSummarize) Do(ctx context.Context, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
// TODO(dgryski): make sure the arrays are all the same 'size'
args, err := helper.GetSeriesArg(ctx, e.Args()[0], from, until, values)
if err != nil {
return nil, err
}
bucketSizeInt32, err := e.GetIntervalArg(1, 1)
if err != nil {
return nil, err
}
bucketSize := int64(bucketSizeInt32)
summarizeFunction, err := e.GetStringNamedOrPosArgDefault("func", 2, "sum")
if err != nil {
return nil, err
}
alignToInterval, err := e.GetStringNamedOrPosArgDefault("alignTo", 3, "")
if err != nil {
return nil, err
}
start := args[0].StartTime
stop := args[0].StopTime
if alignToInterval != "" {
interval, err := parser.IntervalString(alignToInterval, 1)
if err != nil {
return nil, err
}
start = helper.AlignStartToInterval(start, stop, int64(interval))
}
buckets := helper.GetBuckets(start, stop, bucketSize)
results := make([]*types.MetricData, 0, len(args))
for _, arg := range args {
name := fmt.Sprintf("smartSummarize(%s,'%s','%s'", arg.Name, e.Args()[1].StringValue(), summarizeFunction)
if alignToInterval != "" {
name += fmt.Sprintf(",'%s')", alignToInterval)
} else {
name += ")"
}
r := types.MetricData{
FetchResponse: pb.FetchResponse{
Name: name,
Values: make([]float64, buckets, buckets+1),
StepTime: bucketSize,
StartTime: start,
StopTime: stop,
ConsolidationFunc: summarizeFunction,
},
Tags: arg.Tags,
}
t := arg.StartTime // unadjusted
bucketEnd := start + bucketSize
values := make([]float64, 0, bucketSize/arg.StepTime)
ridx := 0
bucketItems := 0
for _, v := range arg.Values {
bucketItems++
if !math.IsNaN(v) {
values = append(values, v)
}
t += arg.StepTime
if t >= stop {
break
}
if t >= bucketEnd {
rv := consolidations.SummarizeValues(summarizeFunction, values, arg.XFilesFactor)
r.Values[ridx] = rv
ridx++
bucketEnd += bucketSize
bucketItems = 0
values = values[:0]
}
}
// last partial bucket
if bucketItems > 0 {
rv := consolidations.SummarizeValues(summarizeFunction, values, arg.XFilesFactor)
r.Values[ridx] = rv
}
results = append(results, &r)
}
return results, nil
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *smartSummarize) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"smartSummarize": {
Description: "Smarter version of summarize.\nThe alignToFrom boolean parameter has been replaced by alignTo and no longer has any effect. Alignment can be to years, months, weeks, days, hours, and minutes.\nThis function can be used with aggregation functions average, median, sum, min, max, diff, stddev, count, range, multiply & last.",
Function: "smartSummarize(seriesList, intervalString, func='sum', alignTo=None)",
Group: "Transform",
Module: "graphite.render.functions",
Name: "smartSummarize",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Name: "intervalString",
Required: true,
Suggestions: types.NewSuggestions(
"10min",
"1h",
"1d",
),
Type: types.Interval,
},
{
Default: types.NewSuggestion("sum"),
Name: "func",
Options: types.StringsToSuggestionList(consolidations.AvailableSummarizers),
Type: types.AggFunc,
},
{
Name: "alignTo",
Suggestions: types.NewSuggestions(
"1m",
"1d",
"1y",
),
Type: types.Interval,
},
},
},
}
}