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

     1  package derivative
     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 derivative 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 := &derivative{}
    22  	functions := []string{"derivative"}
    23  	for _, n := range functions {
    24  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    25  	}
    26  	return res
    27  }
    28  
    29  // derivative(seriesList)
    30  func (f *derivative) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    31  	return helper.ForEachSeriesDo(ctx, eval, e, from, until, values, func(a *types.MetricData, r *types.MetricData) *types.MetricData {
    32  		prev := math.NaN()
    33  		for i, v := range a.Values {
    34  			// We don't need to check for special case here. value-NaN == NaN
    35  
    36  			r.Values[i] = v - prev
    37  			if !math.IsNaN(v) {
    38  				prev = v
    39  			}
    40  		}
    41  		r.Tags["derivative"] = "1"
    42  		return r
    43  	})
    44  }
    45  
    46  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    47  func (f *derivative) Description() map[string]types.FunctionDescription {
    48  	return map[string]types.FunctionDescription{
    49  		"derivative": {
    50  			Description: "This is the opposite of the integral function.  This is useful for taking a\nrunning total metric and calculating the delta between subsequent data points.\n\nThis function does not normalize for periods of time, as a true derivative would.\nInstead see the perSecond() function to calculate a rate of change over time.\n\nExample:\n\n.. code-block:: none\n\n  &target=derivative(company.server.application01.ifconfig.TXPackets)\n\nEach time you run ifconfig, the RX and TXPackets are higher (assuming there\nis network traffic.) By applying the derivative function, you can get an\nidea of the packets per minute sent or received, even though you're only\nrecording the total.",
    51  			Function:    "derivative(seriesList)",
    52  			Group:       "Transform",
    53  			Module:      "graphite.render.functions",
    54  			Name:        "derivative",
    55  			Params: []types.FunctionParam{
    56  				{
    57  					Name:     "seriesList",
    58  					Required: true,
    59  					Type:     types.SeriesList,
    60  				},
    61  			},
    62  			ValuesChange: true, // values changed
    63  		},
    64  	}
    65  }