Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various issues with holtWinters* functions #781

Merged
merged 5 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions expr/functions/holtWintersAberration/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,60 @@ func New(configFile string) []interfaces.FunctionMetadata {
}

func (f *holtWintersAberration) 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)
bootstrapInterval, err := e.GetIntervalNamedOrPosArgDefault("bootstrapInterval", 2, 1, holtwinters.DefaultBootstrapInterval)
if err != nil {
return nil, err
}

args, err := helper.GetSeriesArg(ctx, e.Arg(0), from-bootstrapInterval, until, values)
args, err := helper.GetSeriesArg(ctx, e.Arg(0), from, until, values)
if err != nil {
return nil, err
}

// Note: additional fetch requests are added with an adjusted start time in expr.Metrics() (in
// pkg/parser/parser.go) so that the appropriate data corresponding to the adjusted start time
// can be pre-fetched.
adjustedStartArgs, err := helper.GetSeriesArg(ctx, e.Arg(0), from-bootstrapInterval, until, values)
if err != nil {
return nil, err
}

adjustedStartSeries := make(map[string]*types.MetricData)
for _, serie := range adjustedStartArgs {
adjustedStartSeries[serie.Name] = serie
}

delta, err := e.GetFloatNamedOrPosArgDefault("delta", 1, 3)
if err != nil {
return nil, err
}

seasonality, err := e.GetIntervalNamedOrPosArgDefault("seasonality", 3, 1, holtwinters.DefaultSeasonality)
if err != nil {
return nil, err
}

results := make([]*types.MetricData, 0, len(args))
for _, arg := range args {
var (
aberration []float64
series []float64
)

stepTime := arg.StepTime

lowerBand, upperBand := holtwinters.HoltWintersConfidenceBands(arg.Values, stepTime, delta, bootstrapInterval/86400)

windowPoints := int(bootstrapInterval / stepTime)
if len(arg.Values) > windowPoints {
series = arg.Values[windowPoints:]
if v, ok := adjustedStartSeries[arg.Name]; !ok || v == nil {
continue
}

for i := range series {
if math.IsNaN(series[i]) {
lowerBand, upperBand := holtwinters.HoltWintersConfidenceBands(adjustedStartSeries[arg.Name].Values, stepTime, delta, bootstrapInterval, seasonality)

for i, v := range arg.Values {
if math.IsNaN(v) {
aberration = append(aberration, 0)
} else if !math.IsNaN(upperBand[i]) && series[i] > upperBand[i] {
aberration = append(aberration, series[i]-upperBand[i])
} else if !math.IsNaN(lowerBand[i]) && series[i] < lowerBand[i] {
aberration = append(aberration, series[i]-lowerBand[i])
} else if !math.IsNaN(upperBand[i]) && v > upperBand[i] {
aberration = append(aberration, v-upperBand[i])
} else if !math.IsNaN(lowerBand[i]) && v < lowerBand[i] {
aberration = append(aberration, v-lowerBand[i])
} else {
aberration = append(aberration, 0)
}
Expand All @@ -80,7 +96,7 @@ func (f *holtWintersAberration) Do(ctx context.Context, e parser.Expr, from, unt
Name: name,
Values: aberration,
StepTime: arg.StepTime,
StartTime: arg.StartTime + bootstrapInterval,
StartTime: arg.StartTime,
StopTime: arg.StopTime,
PathExpression: name,
ConsolidationFunc: arg.ConsolidationFunc,
Expand Down
126 changes: 126 additions & 0 deletions expr/functions/holtWintersAberration/function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package holtWintersAberration

import (
"github.com/go-graphite/carbonapi/expr/holtwinters"
"testing"

"github.com/go-graphite/carbonapi/expr/helper"
"github.com/go-graphite/carbonapi/expr/metadata"
"github.com/go-graphite/carbonapi/expr/types"
"github.com/go-graphite/carbonapi/pkg/parser"
th "github.com/go-graphite/carbonapi/tests"
)

func init() {
md := New("")
evaluator := th.EvaluatorFromFunc(md[0].F)
metadata.SetEvaluator(evaluator)
helper.SetEvaluator(evaluator)
for _, m := range md {
metadata.RegisterFunction(m.Name, m.F)
}
}

func TestHoltWintersAberration(t *testing.T) {
var startTime int64 = 2678400
var step int64 = 600
var points int64 = 10
var seconds int64 = 86400

tests := []th.EvalTestItemWithRange{
{
Target: "holtWintersAberration(metric*)",
M: map[parser.MetricRequest][]*types.MetricData{
{"metric*", startTime, startTime + step*points}: {
types.MakeMetricData("metric1", generateHwRange(0, points*step, step, 0), step, startTime),
types.MakeMetricData("metric2", generateHwRange(0, points*step, step, 10), step, startTime),
},
{"metric*", startTime - holtwinters.DefaultBootstrapInterval, startTime + step*points}: {
types.MakeMetricData("metric1", generateHwRange(0, ((holtwinters.DefaultBootstrapInterval/step)+points)*step, step, 0), step, startTime-holtwinters.DefaultBootstrapInterval),
types.MakeMetricData("metric2", generateHwRange(0, ((holtwinters.DefaultBootstrapInterval/step)+points)*step, step, 10), step, startTime-holtwinters.DefaultBootstrapInterval),
},
},
Want: []*types.MetricData{
types.MakeMetricData("holtWintersAberration(metric1)", []float64{-0.2841206166091448, -0.05810270987744115, 0, 0, 0, 0, 0, 0, 0, 0}, step, startTime).SetTag("holtWintersAberration", "1"),
types.MakeMetricData("holtWintersAberration(metric2)", []float64{-0.284120616609151, -0.05810270987744737, 0, 0, 0, 0, 0, 0, 0, 0}, step, startTime).SetTag("holtWintersAberration", "1"),
},
From: startTime,
Until: startTime + step*points,
},
{
Target: "holtWintersAberration(metric*,4,'4d')",
M: map[parser.MetricRequest][]*types.MetricData{
{"metric*", startTime, startTime + step*points}: {
types.MakeMetricData("metric1", generateHwRange(0, points*step, step, 0), step, startTime),
types.MakeMetricData("metric2", generateHwRange(0, points*step, step, 10), step, startTime),
},
{"metric*", startTime - 4*seconds, startTime + step*points}: {
types.MakeMetricData("metric1", generateHwRange(0, ((4*seconds/step)+points)*step, step, 0), step, startTime-4*seconds),
types.MakeMetricData("metric2", generateHwRange(0, ((4*seconds/step)+points)*step, step, 10), step, startTime-4*seconds),
},
},
Want: []*types.MetricData{
types.MakeMetricData("holtWintersAberration(metric1)", []float64{-1.4410544085511923, -0.5199507849641569, 0, 0, 0, 0, 0, 0, 0, 0.09386319244056907}, step, startTime).SetTag("holtWintersAberration", "1"),
types.MakeMetricData("holtWintersAberration(metric2)", []float64{-1.4410544085511923, -0.5199507849641609, 0, 0, 0, 0, 0, 0, 0, 0.09386319244056551}, step, startTime).SetTag("holtWintersAberration", "1"),
},
From: startTime,
Until: startTime + step*points,
},
{
Target: "holtWintersAberration(metric*,4,'1d','2d')",
M: map[parser.MetricRequest][]*types.MetricData{
{"metric*", startTime, startTime + step*points}: {
types.MakeMetricData("metric1", generateHwRange(0, points*step, step, 0), step, startTime),
types.MakeMetricData("metric2", generateHwRange(0, points*step, step, 10), step, startTime),
},
{"metric*", startTime - seconds, startTime + step*points}: {
types.MakeMetricData("metric1", generateHwRange(0, ((seconds/step)+points)*step, step, 0), step, startTime-seconds),
types.MakeMetricData("metric2", generateHwRange(0, ((seconds/step)+points)*step, step, 10), step, startTime-seconds),
},
},
Want: []*types.MetricData{
types.MakeMetricData("holtWintersAberration(metric1)", []float64{-4.106587168490873, -2.8357974803355406, -1.5645896296885762, -0.4213549577359168, 0, 0, 0, 0.5073914761326588, 2.4432248533746543, 4.186719764193769}, step, startTime).SetTag("holtWintersAberration", "1"),
types.MakeMetricData("holtWintersAberration(metric2)", []float64{-4.1065871684908775, -2.8357974803355486, -1.5645896296885837, -0.42135495773592346, 0, 0, 0, 0.5073914761326499, 2.4432248533746446, 4.186719764193759}, step, startTime).SetTag("holtWintersAberration", "1"),
},
From: startTime,
Until: startTime + step*points,
},
{
Target: "holtWintersAberration(metric*)",
M: map[parser.MetricRequest][]*types.MetricData{
{"metric*", startTime, startTime + step*points}: {
types.MakeMetricData("metric1", generateHwRange(0, points*step, step, 0), step, startTime),
types.MakeMetricData("metric2", generateHwRange(0, points*step, step, 10), step, startTime),
},
{"metric*", startTime - holtwinters.DefaultBootstrapInterval, startTime + step*points}: {
types.MakeMetricData("metric1", generateHwRange(0, ((holtwinters.DefaultBootstrapInterval/step)+points)*step, step, 0), step, startTime-holtwinters.DefaultBootstrapInterval),
types.MakeMetricData("metric2", generateHwRange(0, ((holtwinters.DefaultBootstrapInterval/step)+points)*step, step, 10), step, startTime-holtwinters.DefaultBootstrapInterval),
types.MakeMetricData("metric3", generateHwRange(0, ((holtwinters.DefaultBootstrapInterval/step)+points)*step, step, 20), step, startTime-holtwinters.DefaultBootstrapInterval), // Verify that metrics that don't match those fetched with the unadjusted start time are not included in the results
},
},
Want: []*types.MetricData{
types.MakeMetricData("holtWintersAberration(metric1)", []float64{-0.2841206166091448, -0.05810270987744115, 0, 0, 0, 0, 0, 0, 0, 0}, step, startTime).SetTag("holtWintersAberration", "1"),
types.MakeMetricData("holtWintersAberration(metric2)", []float64{-0.284120616609151, -0.05810270987744737, 0, 0, 0, 0, 0, 0, 0, 0}, step, startTime).SetTag("holtWintersAberration", "1"),
},
From: startTime,
Until: startTime + step*points,
},
}

for _, tt := range tests {
testName := tt.Target
t.Run(testName, func(t *testing.T) {
th.TestEvalExprWithRange(t, &tt)
})
}
}

func generateHwRange(x, y, jump, t int64) []float64 {
var valuesList []float64
for x < y {
val := float64(t + (x/jump)%10)
valuesList = append(valuesList, val)
x += jump
}
return valuesList
}
17 changes: 11 additions & 6 deletions expr/functions/holtWintersConfidenceArea/function_cairo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func New(configFile string) []interfaces.FunctionMetadata {
}

func (f *holtWintersConfidenceArea) 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)
bootstrapInterval, err := e.GetIntervalNamedOrPosArgDefault("bootstrapInterval", 2, 1, holtwinters.DefaultBootstrapInterval)
if err != nil {
return nil, err
}
Expand All @@ -49,22 +49,27 @@ func (f *holtWintersConfidenceArea) Do(ctx context.Context, e parser.Expr, from,
return nil, err
}

seasonality, err := e.GetIntervalNamedOrPosArgDefault("seasonality", 3, 1, holtwinters.DefaultSeasonality)
if err != nil {
return nil, err
}

results := make([]*types.MetricData, 0, len(args)*2)
for _, arg := range args {
stepTime := arg.StepTime

lowerBand, upperBand := holtwinters.HoltWintersConfidenceBands(arg.Values, stepTime, delta, bootstrapInterval/86400)
lowerBand, upperBand := holtwinters.HoltWintersConfidenceBands(arg.Values, stepTime, delta, bootstrapInterval, seasonality)

lowerSeries := types.MetricData{
FetchResponse: pb.FetchResponse{
Name: fmt.Sprintf("holtWintersConfidenceLower(%s)", arg.Name),
Name: fmt.Sprintf("holtWintersConfidenceArea(%s)", arg.Name),
Values: lowerBand,
StepTime: arg.StepTime,
StartTime: arg.StartTime + bootstrapInterval,
StopTime: arg.StopTime,
ConsolidationFunc: arg.ConsolidationFunc,
XFilesFactor: arg.XFilesFactor,
PathExpression: fmt.Sprintf("holtWintersConfidenceLower(%s)", arg.Name),
PathExpression: fmt.Sprintf("holtWintersConfidenceArea(%s)", arg.Name),
},
Tags: helper.CopyTags(arg),
GraphOptions: types.GraphOptions{
Expand All @@ -77,14 +82,14 @@ func (f *holtWintersConfidenceArea) Do(ctx context.Context, e parser.Expr, from,

upperSeries := types.MetricData{
FetchResponse: pb.FetchResponse{
Name: fmt.Sprintf("holtWintersConfidenceUpper(%s)", arg.Name),
Name: fmt.Sprintf("holtWintersConfidenceArea(%s)", arg.Name),
Values: upperBand,
StepTime: arg.StepTime,
StartTime: arg.StartTime + bootstrapInterval,
StopTime: arg.StopTime,
ConsolidationFunc: arg.ConsolidationFunc,
XFilesFactor: arg.XFilesFactor,
PathExpression: fmt.Sprintf("holtWintersConfidenceLower(%s)", arg.Name),
PathExpression: fmt.Sprintf("holtWintersConfidenceArea(%s)", arg.Name),
},
Tags: helper.CopyTags(arg),
GraphOptions: types.GraphOptions{
Expand Down
34 changes: 28 additions & 6 deletions expr/functions/holtWintersConfidenceArea/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package holtWintersConfidenceArea

import (
"github.com/go-graphite/carbonapi/expr/holtwinters"
"testing"

"github.com/go-graphite/carbonapi/expr/helper"
Expand All @@ -27,19 +28,40 @@ func TestHoltWintersConfidenceArea(t *testing.T) {
var startTime int64 = 2678400
var step int64 = 600
var points int64 = 10
var weekSeconds int64 = 7 * 86400

seriesValues := generateHwRange(0, ((weekSeconds/step)+points)*step, step)

tests := []th.EvalTestItemWithRange{
{
Target: "holtWintersConfidenceArea(metric1)",
M: map[parser.MetricRequest][]*types.MetricData{
{"metric1", startTime - weekSeconds, startTime + step*points}: {types.MakeMetricData("metric1", seriesValues, step, startTime-weekSeconds)},
{"metric1", startTime - holtwinters.DefaultBootstrapInterval, startTime + step*points}: {types.MakeMetricData("metric1", generateHwRange(0, ((holtwinters.DefaultBootstrapInterval/step)+points)*step, step), step, startTime-holtwinters.DefaultBootstrapInterval)},
},
Want: []*types.MetricData{
types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{0.2841206166091448, 1.0581027098774411, 0.3338172102994683, 0.5116859493263242, -0.18199175514936972, 0.2366173792019426, -1.2941554508809152, -0.513426806531049, -0.7970905542723132, 0.09868900726536012}, step, startTime).SetTag("holtWintersConfidenceArea", "1"),
types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{8.424944558327624, 9.409422251880809, 10.607070189221787, 10.288439865038768, 9.491556863132963, 9.474595784593738, 8.572310478053845, 8.897670449095346, 8.941566968508148, 9.409728797779282}, step, startTime).SetTag("holtWintersConfidenceArea", "1"),
},
From: startTime,
Until: startTime + step*points,
},
{
Target: "holtWintersConfidenceArea(metric1,4,'6d')",
M: map[parser.MetricRequest][]*types.MetricData{
{"metric1", startTime - 6*holtwinters.SecondsPerDay, startTime + step*points}: {types.MakeMetricData("metric1", generateHwRange(0, ((6*holtwinters.SecondsPerDay/step)+points)*step, step), step, startTime-6*holtwinters.SecondsPerDay)},
},
Want: []*types.MetricData{
types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{-0.6535362793382391, -0.26554972418633316, -1.1060549683277792, -0.5788026852576289, -1.4594446935142829, -0.6933311085203409, -1.6566119269969288, -1.251651025511391, -1.7938581852717226, -1.1791817117029604}, step, startTime).SetTag("holtWintersConfidenceArea", "1"),
types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{8.166528156512886, 8.759008839563066, 9.250962452510654, 9.994110161265208, 10.511931730022393, 11.34313475259535, 12.639554646464758, 11.972601342482212, 10.920216551100442, 10.618692557967133}, step, startTime).SetTag("holtWintersConfidenceArea", "1"),
},
From: startTime,
Until: startTime + step*points,
},
{
Target: "holtWintersConfidenceArea(metric1,4,'1d','2d')",
M: map[parser.MetricRequest][]*types.MetricData{
{"metric1", startTime - holtwinters.SecondsPerDay, startTime + step*points}: {types.MakeMetricData("metric1", generateHwRange(0, ((holtwinters.SecondsPerDay/step)+points)*step, step), step, startTime-holtwinters.SecondsPerDay)},
},
Want: []*types.MetricData{
types.MakeMetricData("holtWintersConfidenceLower(metric1)", []float64{0.2841206166091448, 1.0581027098774411, 0.3338172102994683, 0.5116859493263242, -0.18199175514936972, 0.2366173792019426, -1.2941554508809152, -0.513426806531049, -0.7970905542723132, 0.09868900726536012}, step, startTime).SetTag("holtWintersConfidenceArea", "1"),
types.MakeMetricData("holtWintersConfidenceUpper(metric1)", []float64{8.424944558327624, 9.409422251880809, 10.607070189221787, 10.288439865038768, 9.491556863132963, 9.474595784593738, 8.572310478053845, 8.897670449095346, 8.941566968508148, 9.409728797779282}, step, startTime).SetTag("holtWintersConfidenceArea", "1"),
types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{4.106587168490873, 3.8357974803355406, 3.564589629688576, 3.421354957735917, 3.393696278743315, 3.470415673952413, 3.2748850646377368, 3.3539750816574316, 3.5243322056965765, 3.7771201010598134}, step, startTime).SetTag("holtWintersConfidenceArea", "1"),
types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{4.24870339314537, 4.501056063000946, 4.956252698437961, 5.466294981886822, 6.0258698337471355, 6.630178145979606, 7.6413984841547204, 6.492608523867341, 5.556775146625346, 4.813280235806231}, step, startTime).SetTag("holtWintersConfidenceArea", "1"),
},
From: startTime,
Until: startTime + step*points,
Expand Down
10 changes: 7 additions & 3 deletions expr/functions/holtWintersConfidenceBands/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package holtWintersConfidenceBands

import (
"context"

"github.com/go-graphite/carbonapi/expr/helper"
"github.com/go-graphite/carbonapi/expr/holtwinters"
"github.com/go-graphite/carbonapi/expr/interfaces"
Expand Down Expand Up @@ -30,7 +29,7 @@ func New(configFile string) []interfaces.FunctionMetadata {
}

func (f *holtWintersConfidenceBands) 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)
bootstrapInterval, err := e.GetIntervalNamedOrPosArgDefault("bootstrapInterval", 2, 1, holtwinters.DefaultBootstrapInterval)
if err != nil {
return nil, err
}
Expand All @@ -45,11 +44,16 @@ func (f *holtWintersConfidenceBands) Do(ctx context.Context, e parser.Expr, from
return nil, err
}

seasonality, err := e.GetIntervalNamedOrPosArgDefault("seasonality", 3, 1, holtwinters.DefaultSeasonality)
if err != nil {
return nil, err
}

results := make([]*types.MetricData, 0, len(args)*2)
for _, arg := range args {
stepTime := arg.StepTime

lowerBand, upperBand := holtwinters.HoltWintersConfidenceBands(arg.Values, stepTime, delta, bootstrapInterval/86400)
lowerBand, upperBand := holtwinters.HoltWintersConfidenceBands(arg.Values, stepTime, delta, bootstrapInterval, seasonality)

name := "holtWintersConfidenceLower(" + arg.Name + ")"
lowerSeries := &types.MetricData{
Expand Down
Loading