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

     1  package cumulative
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/go-graphite/carbonapi/expr/helper"
     7  	"github.com/go-graphite/carbonapi/expr/interfaces"
     8  	"github.com/go-graphite/carbonapi/expr/types"
     9  	"github.com/go-graphite/carbonapi/pkg/parser"
    10  )
    11  
    12  type cumulative struct{}
    13  
    14  func GetOrder() interfaces.Order {
    15  	return interfaces.Any
    16  }
    17  
    18  func New(configFile string) []interfaces.FunctionMetadata {
    19  	res := make([]interfaces.FunctionMetadata, 0)
    20  	f := &cumulative{}
    21  	functions := []string{"cumulative"}
    22  	for _, n := range functions {
    23  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    24  	}
    25  	return res
    26  }
    27  
    28  // cumulative(seriesList)
    29  func (f *cumulative) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    30  	arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	results := make([]*types.MetricData, len(arg))
    35  
    36  	for i, a := range arg {
    37  		r := a.CopyLink()
    38  		r.ConsolidationFunc = "sum"
    39  		r.Name = "consolidateBy(" + a.Name + ",\"sum\")"
    40  		r.Tags["consolidateBy"] = "sum"
    41  		results[i] = r
    42  	}
    43  	return results, nil
    44  }
    45  
    46  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    47  func (f *cumulative) Description() map[string]types.FunctionDescription {
    48  	return map[string]types.FunctionDescription{
    49  		"cumulative": {
    50  			Description: "Takes one metric or a wildcard seriesList.\n\nWhen a graph is drawn where width of the graph size in pixels is smaller than\nthe number of datapoints to be graphed, Graphite consolidates the values to\nto prevent line overlap. The cumulative() function changes the consolidation\nfunction from the default of 'average' to 'sum'. This is especially useful in\nsales graphs, where fractional values make no sense and a 'sum' of consolidated\nvalues is appropriate.\n\nAlias for :func:`consolidateBy(series, 'sum') <graphite.render.functions.consolidateBy>`\n\n.. code-block:: none\n\n  &target=cumulative(Sales.widgets.largeBlue)",
    51  			Function:    "cumulative(seriesList)",
    52  			Group:       "Special",
    53  			Module:      "graphite.render.functions",
    54  			Name:        "cumulative",
    55  			Params: []types.FunctionParam{
    56  				{
    57  					Name:     "seriesList",
    58  					Required: true,
    59  					Type:     types.SeriesList,
    60  				},
    61  			},
    62  		},
    63  	}
    64  }