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

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