github.com/go-graphite/carbonapi@v0.17.0/expr/functions/holtWintersConfidenceArea/function_cairo.go (about)

     1  //go:build cairo
     2  // +build cairo
     3  
     4  package holtWintersConfidenceArea
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"github.com/go-graphite/carbonapi/expr/helper"
    11  	"github.com/go-graphite/carbonapi/expr/holtwinters"
    12  	"github.com/go-graphite/carbonapi/expr/interfaces"
    13  	"github.com/go-graphite/carbonapi/expr/types"
    14  	"github.com/go-graphite/carbonapi/pkg/parser"
    15  	pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
    16  )
    17  
    18  type holtWintersConfidenceArea struct{}
    19  
    20  func GetOrder() interfaces.Order {
    21  	return interfaces.Any
    22  }
    23  
    24  func New(configFile string) []interfaces.FunctionMetadata {
    25  	res := make([]interfaces.FunctionMetadata, 0)
    26  	f := &holtWintersConfidenceArea{}
    27  	functions := []string{"holtWintersConfidenceArea"}
    28  	for _, n := range functions {
    29  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    30  	}
    31  	return res
    32  }
    33  
    34  func (f *holtWintersConfidenceArea) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    35  	bootstrapInterval, err := e.GetIntervalNamedOrPosArgDefault("bootstrapInterval", 2, 1, holtwinters.DefaultBootstrapInterval)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	args, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from-bootstrapInterval, until, values)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	delta, err := e.GetFloatNamedOrPosArgDefault("delta", 1, 3)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	seasonality, err := e.GetIntervalNamedOrPosArgDefault("seasonality", 3, 1, holtwinters.DefaultSeasonality)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	results := make([]*types.MetricData, 0, len(args)*2)
    56  	for _, arg := range args {
    57  		stepTime := arg.StepTime
    58  
    59  		lowerBand, upperBand := holtwinters.HoltWintersConfidenceBands(arg.Values, stepTime, delta, bootstrapInterval, seasonality)
    60  
    61  		lowerSeries := types.MetricData{
    62  			FetchResponse: pb.FetchResponse{
    63  				Name:              fmt.Sprintf("holtWintersConfidenceArea(%s)", arg.Name),
    64  				Values:            lowerBand,
    65  				StepTime:          arg.StepTime,
    66  				StartTime:         arg.StartTime + bootstrapInterval,
    67  				StopTime:          arg.StopTime,
    68  				ConsolidationFunc: arg.ConsolidationFunc,
    69  				XFilesFactor:      arg.XFilesFactor,
    70  				PathExpression:    fmt.Sprintf("holtWintersConfidenceArea(%s)", arg.Name),
    71  			},
    72  			Tags: helper.CopyTags(arg),
    73  			GraphOptions: types.GraphOptions{
    74  				Stacked:   true,
    75  				StackName: types.DefaultStackName,
    76  				Invisible: true,
    77  			},
    78  		}
    79  		lowerSeries.Tags["holtWintersConfidenceArea"] = "1"
    80  
    81  		upperSeries := types.MetricData{
    82  			FetchResponse: pb.FetchResponse{
    83  				Name:              fmt.Sprintf("holtWintersConfidenceArea(%s)", arg.Name),
    84  				Values:            upperBand,
    85  				StepTime:          arg.StepTime,
    86  				StartTime:         arg.StartTime + bootstrapInterval,
    87  				StopTime:          arg.StopTime,
    88  				ConsolidationFunc: arg.ConsolidationFunc,
    89  				XFilesFactor:      arg.XFilesFactor,
    90  				PathExpression:    fmt.Sprintf("holtWintersConfidenceArea(%s)", arg.Name),
    91  			},
    92  			Tags: helper.CopyTags(arg),
    93  			GraphOptions: types.GraphOptions{
    94  				Stacked:   true,
    95  				StackName: types.DefaultStackName,
    96  			},
    97  		}
    98  
    99  		upperSeries.Tags["holtWintersConfidenceArea"] = "1"
   100  
   101  		results = append(results, &lowerSeries, &upperSeries)
   102  	}
   103  	return results, nil
   104  }
   105  
   106  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
   107  func (f *holtWintersConfidenceArea) Description() map[string]types.FunctionDescription {
   108  	return map[string]types.FunctionDescription{
   109  		"holtWintersConfidenceArea": {
   110  			Description: "Performs a Holt-Winters forecast using the series as input data and plots\n the area between the upper and lower bands of the predicted forecast deviations.",
   111  			Function:    "holtWintersConfidenceArea(seriesList, delta=3, bootstrapInterval='7d')",
   112  			Group:       "Calculate",
   113  			Module:      "graphite.render.functions",
   114  			Name:        "holtWintersConfidenceArea",
   115  			Params: []types.FunctionParam{
   116  				{
   117  					Name:     "seriesList",
   118  					Required: true,
   119  					Type:     types.SeriesList,
   120  				},
   121  				{
   122  					Default: types.NewSuggestion(3),
   123  					Name:    "delta",
   124  					Type:    types.Integer,
   125  				},
   126  				{
   127  					Default: types.NewSuggestion("7d"),
   128  					Name:    "bootstrapInterval",
   129  					Suggestions: types.NewSuggestions(
   130  						"7d",
   131  						"30d",
   132  					),
   133  					Type: types.Interval,
   134  				},
   135  			},
   136  		},
   137  	}
   138  }