forked from go-graphite/carbonapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.go
103 lines (91 loc) · 3.11 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
package holtWintersForecast
import (
"context"
"fmt"
pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
"github.com/grafana/carbonapi/expr/helper"
"github.com/grafana/carbonapi/expr/holtwinters"
"github.com/grafana/carbonapi/expr/interfaces"
"github.com/grafana/carbonapi/expr/types"
"github.com/grafana/carbonapi/pkg/parser"
)
type holtWintersForecast struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &holtWintersForecast{}
functions := []string{"holtWintersForecast"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
func (f *holtWintersForecast) Do(ctx context.Context, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
bootstrapInterval, err := e.GetIntervalNamedOrPosArgDefault("bootstrapInterval", 2, 1, 7*86400)
if err != nil {
return nil, err
}
args, err := helper.GetSeriesArgsAndRemoveNonExisting(ctx, e, from-bootstrapInterval, until, values)
if err != nil {
return nil, err
}
var predictionsOfInterest []float64
results := make([]*types.MetricData, 0, len(args))
for _, arg := range args {
stepTime := arg.StepTime
predictions, _ := holtwinters.HoltWintersAnalysis(arg.Values, stepTime)
windowPoints := int(bootstrapInterval / stepTime)
if len(predictions) < windowPoints {
predictionsOfInterest = predictions
} else {
predictionsOfInterest = predictions[windowPoints:]
}
r := types.MetricData{
FetchResponse: pb.FetchResponse{
Name: fmt.Sprintf("holtWintersForecast(%s)", arg.Name),
Values: predictionsOfInterest,
StepTime: arg.StepTime,
StartTime: arg.StartTime + bootstrapInterval,
StopTime: arg.StopTime,
PathExpression: fmt.Sprintf("holtWintersForecast(%s)", arg.Name),
XFilesFactor: arg.XFilesFactor,
ConsolidationFunc: arg.ConsolidationFunc,
},
Tags: arg.Tags,
}
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 *holtWintersForecast) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"holtWintersForecast": {
Description: "Performs a Holt-Winters forecast using the series as input data. Data from\n`bootstrapInterval` (one week by default) previous to the series is used to bootstrap the initial forecast.",
Function: "holtWintersForecast(seriesList, bootstrapInterval='7d')",
Group: "Calculate",
Module: "graphite.render.functions",
Name: "holtWintersForecast",
Params: []types.FunctionParam{
{
Name: "seriesList",
Required: true,
Type: types.SeriesList,
},
{
Default: types.NewSuggestion("7d"),
Name: "bootstrapInterval",
Suggestions: types.NewSuggestions(
"7d",
"30d",
),
Type: types.Interval,
},
},
},
}
}