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

     1  package timeStack
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/go-graphite/carbonapi/expr/helper"
     9  	"github.com/go-graphite/carbonapi/expr/interfaces"
    10  	"github.com/go-graphite/carbonapi/expr/types"
    11  	"github.com/go-graphite/carbonapi/pkg/parser"
    12  )
    13  
    14  type timeStack 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 := &timeStack{}
    23  	functions := []string{"timeStack"}
    24  	for _, n := range functions {
    25  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    26  	}
    27  	return res
    28  }
    29  
    30  // timeStack(seriesList, timeShiftUnit, timeShiftStart, timeShiftEnd)
    31  func (f *timeStack) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    32  	unit, err := e.GetIntervalArg(1, -1)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	unitStr := e.Arg(1).StringValue()
    37  
    38  	start, err := e.GetIntArgDefault(2, 0)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	end, err := e.GetIntArgDefault(3, 7)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	var results []*types.MetricData
    49  	for i := int64(start); i < int64(end); i++ {
    50  		offs := i * int64(unit)
    51  		fromNew := from + offs
    52  		untilNew := until + offs
    53  		arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), fromNew, untilNew, values)
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  
    58  		offsStr := strconv.FormatInt(offs, 10)
    59  		for _, a := range arg {
    60  			r := a.CopyLink()
    61  			r.Name = fmt.Sprintf("timeShift(%s,%s,%d)", a.Name, unitStr, offs)
    62  			r.StartTime = a.StartTime - offs
    63  			r.StopTime = a.StopTime - offs
    64  			r.Tags["timeShiftUnit"] = unitStr
    65  			r.Tags["timeShift"] = offsStr
    66  			results = append(results, r)
    67  		}
    68  	}
    69  
    70  	return results, nil
    71  }
    72  
    73  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    74  func (f *timeStack) Description() map[string]types.FunctionDescription {
    75  	return map[string]types.FunctionDescription{
    76  		"timeStack": {
    77  			Description: "Takes one metric or a wildcard seriesList, followed by a quoted string with the\nlength of time (See ``from / until`` in the render\\_api_ for examples of time formats).\nAlso takes a start multiplier and end multiplier for the length of time\n\ncreate a seriesList which is composed the original metric series stacked with time shifts\nstarting time shifts from the start multiplier through the end multiplier\n\nUseful for looking at history, or feeding into averageSeries or stddevSeries.\n\nExample:\n\n.. code-block:: none\n\n  &target=timeStack(Sales.widgets.largeBlue,\"1d\",0,7)    # create a series for today and each of the previous 7 days",
    78  			Function:    "timeStack(seriesList, timeShiftUnit='1d', timeShiftStart=0, timeShiftEnd=7)",
    79  			Group:       "Transform",
    80  			Module:      "graphite.render.functions",
    81  			Name:        "timeStack",
    82  			Params: []types.FunctionParam{
    83  				{
    84  					Name:     "seriesList",
    85  					Required: true,
    86  					Type:     types.SeriesList,
    87  				},
    88  				{
    89  					Default: types.NewSuggestion("1d"),
    90  					Name:    "timeShiftUnit",
    91  					Suggestions: types.NewSuggestions(
    92  						"1h",
    93  						"6h",
    94  						"12h",
    95  						"1d",
    96  						"2d",
    97  						"7d",
    98  						"14d",
    99  						"30d",
   100  					),
   101  					Type: types.Interval,
   102  				},
   103  				{
   104  					Default: types.NewSuggestion(0),
   105  					Name:    "timeShiftStart",
   106  					Type:    types.Integer,
   107  				},
   108  				{
   109  					Default: types.NewSuggestion(7),
   110  					Name:    "timeShiftEnd",
   111  					Type:    types.Integer,
   112  				},
   113  			},
   114  		},
   115  	}
   116  }