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

     1  package invert
     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 invert 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 := &invert{}
    22  	functions := []string{"invert"}
    23  	for _, n := range functions {
    24  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    25  	}
    26  	return res
    27  }
    28  
    29  // invert(seriesList)
    30  func (f *invert) 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  		for i, v := range a.Values {
    33  			if v == 0 {
    34  				r.Values[i] = math.NaN()
    35  			} else {
    36  				r.Values[i] = 1 / v
    37  			}
    38  		}
    39  		r.Tags["invert"] = "1"
    40  
    41  		return r
    42  	})
    43  }
    44  
    45  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    46  func (f *invert) Description() map[string]types.FunctionDescription {
    47  	return map[string]types.FunctionDescription{
    48  		"invert": {
    49  			Description: "Takes one metric or a wildcard seriesList, and inverts each datapoint (i.e. 1/x).\n\nExample:\n\n.. code-block:: none\n\n  &target=invert(Server.instance01.threads.busy)",
    50  			Function:    "invert(seriesList)",
    51  			Group:       "Transform",
    52  			Module:      "graphite.render.functions",
    53  			Name:        "invert",
    54  			Params: []types.FunctionParam{
    55  				{
    56  					Name:     "seriesList",
    57  					Required: true,
    58  					Type:     types.SeriesList,
    59  				},
    60  			},
    61  			NameChange:   true, // name changed
    62  			ValuesChange: true, // values changed
    63  		},
    64  	}
    65  }