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

     1  package integralByInterval
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  
     7  	"github.com/go-graphite/carbonapi/expr/helper"
     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  )
    12  
    13  type integralByInterval struct{}
    14  
    15  func GetOrder() interfaces.Order {
    16  	return interfaces.Any
    17  }
    18  
    19  func New(configFile string) []interfaces.FunctionMetadata {
    20  	res := make([]interfaces.FunctionMetadata, 0)
    21  	f := &integralByInterval{}
    22  	functions := []string{"integralByInterval"}
    23  	for _, n := range functions {
    24  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    25  	}
    26  	return res
    27  }
    28  
    29  // integralByInterval(seriesList, intervalString)
    30  func (f *integralByInterval) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    31  	if e.ArgsLen() < 2 {
    32  		return nil, parser.ErrMissingArgument
    33  	}
    34  
    35  	args, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	if len(args) == 0 {
    40  		return nil, nil
    41  	}
    42  
    43  	bucketSizeInt32, err := e.GetIntervalArg(1, 1)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	bucketSize := int64(bucketSizeInt32)
    48  	intervalString, err := e.GetStringArg(1)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	startTime := from
    54  	results := make([]*types.MetricData, len(args))
    55  	for j, arg := range args {
    56  		current := 0.0
    57  		currentTime := arg.StartTime
    58  
    59  		name := "integralByInterval(" + arg.Name + ",'" + intervalString + "')"
    60  		result := arg.CopyLink()
    61  		result.Name = name
    62  		result.PathExpression = name
    63  		result.Values = make([]float64, len(arg.Values))
    64  
    65  		result.Tags["integralByInterval"] = intervalString
    66  
    67  		for i, v := range arg.Values {
    68  			if (currentTime-startTime)/bucketSize != (currentTime-startTime-arg.StepTime)/bucketSize {
    69  				current = 0
    70  			}
    71  			if math.IsNaN(v) {
    72  				v = 0
    73  			}
    74  			current += v
    75  			result.Values[i] = current
    76  			currentTime += arg.StepTime
    77  		}
    78  
    79  		results[j] = result
    80  	}
    81  
    82  	return results, nil
    83  }
    84  
    85  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    86  func (f *integralByInterval) Description() map[string]types.FunctionDescription {
    87  	return map[string]types.FunctionDescription{
    88  		"integralByInterval": {
    89  			Description: "This will do the same as integralByInterval() funcion, except resetting the total to 0 at the given time in the parameter “from” Useful for finding totals per hour/day/week/..",
    90  			Function:    "integralByInterval(seriesList, intervalString)",
    91  			Group:       "Transform",
    92  			Module:      "graphite.render.functions",
    93  			Name:        "integralByInterval",
    94  			Params: []types.FunctionParam{
    95  				{
    96  					Name:     "seriesList",
    97  					Required: true,
    98  					Type:     types.SeriesList,
    99  				}, {
   100  					Name:     "intervalString",
   101  					Required: true,
   102  					Suggestions: types.NewSuggestions(
   103  						"10min",
   104  						"1h",
   105  						"1d",
   106  					),
   107  					Type: types.Interval,
   108  				},
   109  			},
   110  			NameChange:   true, // name changed
   111  			ValuesChange: true, // values changed
   112  		},
   113  	}
   114  }