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

     1  package squareRoot
     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 squareRoot 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 := &squareRoot{}
    22  	functions := []string{"squareRoot"}
    23  	for _, n := range functions {
    24  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    25  	}
    26  	return res
    27  }
    28  
    29  // squareRoot(seriesList)
    30  func (f *squareRoot) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    31  	arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	var results []*types.MetricData
    36  
    37  	for _, a := range arg {
    38  		r := a.CopyLink()
    39  		r.Name = "squareRoot(" + a.Name + ")"
    40  		r.Values = make([]float64, len(a.Values))
    41  		r.Tags["squareRoot"] = "1"
    42  
    43  		for i, v := range a.Values {
    44  			r.Values[i] = math.Sqrt(v)
    45  		}
    46  		results = append(results, r)
    47  	}
    48  	return results, nil
    49  }
    50  
    51  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    52  func (f *squareRoot) Description() map[string]types.FunctionDescription {
    53  	return map[string]types.FunctionDescription{
    54  		"squareRoot": {
    55  			Description: "Takes one metric or a wildcard seriesList, and computes the square root of each datapoint.\n\nExample:\n\n.. code-block:: none\n\n  &target=squareRoot(Server.instance01.threads.busy)",
    56  			Function:    "squareRoot(seriesList)",
    57  			Group:       "Transform",
    58  			Module:      "graphite.render.functions",
    59  			Name:        "squareRoot",
    60  			Params: []types.FunctionParam{
    61  				{
    62  					Name:     "seriesList",
    63  					Required: true,
    64  					Type:     types.SeriesList,
    65  				},
    66  			},
    67  			NameChange:   true, // name changed
    68  			ValuesChange: true, // values changed
    69  		},
    70  	}
    71  }