forked from go-graphite/carbonapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.go
158 lines (137 loc) · 4 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
package hitcount
import (
"context"
"fmt"
"math"
pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
"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 hitcount struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &hitcount{}
functions := []string{"hitcount"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// hitcount(seriesList, intervalString, alignToInterval=False)
func (f *hitcount) 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)
alignToInterval, err := e.GetBoolNamedOrPosArgDefault("alignToInterval", 2, false)
if err != nil {
return nil, err
}
_, ok := e.NamedArgs()["alignToInterval"]
if !ok {
ok = len(e.Args()) > 2
}
start := args[0].StartTime
stop := args[0].StopTime
if alignToInterval {
start = helper.AlignStartToInterval(start, stop, bucketSize)
}
buckets := helper.GetBuckets(start, stop, bucketSize)
results := make([]*types.MetricData, 0, len(args))
for _, arg := range args {
name := fmt.Sprintf("hitcount(%s,'%s'", arg.Name, e.Args()[1].StringValue())
if ok {
name += fmt.Sprintf(",%v", alignToInterval)
}
name += ")"
r := types.MetricData{
FetchResponse: pb.FetchResponse{
Name: name,
Values: make([]float64, buckets, buckets+1),
StepTime: bucketSize,
StartTime: start,
StopTime: stop,
ConsolidationFunc: "max",
},
Tags: arg.Tags,
}
bucketEnd := start + bucketSize
t := arg.StartTime
ridx := 0
var count float64
bucketItems := 0
for _, v := range arg.Values {
bucketItems++
if !math.IsNaN(v) {
if math.IsNaN(count) {
count = 0
}
count += v * float64(arg.StepTime)
}
t += arg.StepTime
if t >= stop {
break
}
if t >= bucketEnd {
r.Values[ridx] = count
ridx++
bucketEnd += bucketSize
count = math.NaN()
bucketItems = 0
}
}
// remaining values
if bucketItems > 0 {
r.Values[ridx] = count
}
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 *hitcount) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"hitcount": {
Description: "Estimate hit counts from a list of time series.\n\nThis function assumes the values in each time series represent\nhits per second. It calculates hits per some larger interval\nsuch as per day or per hour. This function is like summarize(),\nexcept that it compensates automatically for different time scales\n(so that a similar graph results from using either fine-grained\nor coarse-grained records) and handles rarely-occurring events\ngracefully.",
Function: "hitcount(seriesList, intervalString, alignToInterval=False)",
Group: "Transform",
Module: "graphite.render.functions",
Name: "hitcount",
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(false),
Name: "alignToInterval",
Type: types.Boolean,
},
},
},
}
}