github.com/go-graphite/carbonapi@v0.17.0/expr/functions/pow/function.go (about) 1 package pow 2 3 import ( 4 "context" 5 "math" 6 "strconv" 7 8 "github.com/go-graphite/carbonapi/expr/helper" 9 "github.com/go-graphite/carbonapi/expr/interfaces" 10 "github.com/go-graphite/carbonapi/expr/types" 11 "github.com/go-graphite/carbonapi/pkg/parser" 12 ) 13 14 type pow struct{} 15 16 func GetOrder() interfaces.Order { 17 return interfaces.Any 18 } 19 20 func New(configFile string) []interfaces.FunctionMetadata { 21 res := make([]interfaces.FunctionMetadata, 0) 22 f := &pow{} 23 functions := []string{"pow"} 24 for _, n := range functions { 25 res = append(res, interfaces.FunctionMetadata{Name: n, F: f}) 26 } 27 return res 28 } 29 30 // pow(seriesList,factor) 31 func (f *pow) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) { 32 if e.ArgsLen() < 2 { 33 return nil, parser.ErrMissingArgument 34 } 35 36 arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values) 37 if err != nil { 38 return nil, err 39 } 40 factor, err := e.GetFloatArg(1) 41 if err != nil { 42 return nil, err 43 } 44 factorStr := strconv.FormatFloat(factor, 'g', -1, 64) 45 results := make([]*types.MetricData, len(arg)) 46 47 for j, a := range arg { 48 r := a.CopyLink() 49 r.Name = "pow(" + a.Name + "," + factorStr + ")" 50 r.Values = make([]float64, len(a.Values)) 51 r.Tags["pow"] = factorStr 52 53 for i, v := range a.Values { 54 if math.IsNaN(v) { 55 r.Values[i] = math.NaN() 56 } else { 57 r.Values[i] = math.Pow(v, factor) 58 } 59 } 60 results[j] = r 61 } 62 return results, nil 63 } 64 65 // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web 66 func (f *pow) Description() map[string]types.FunctionDescription { 67 return map[string]types.FunctionDescription{ 68 "pow": { 69 Description: "Takes one metric or a wildcard seriesList followed by a constant, and raises the datapoint\nby the power of the constant provided at each point.\n\nExample:\n\n.. code-block:: none\n\n &target=pow(Server.instance01.threads.busy,10)\n &target=pow(Server.instance*.threads.busy,10)", 70 Function: "pow(seriesList, factor)", 71 Group: "Transform", 72 Module: "graphite.render.functions", 73 Name: "pow", 74 Params: []types.FunctionParam{ 75 { 76 Name: "seriesList", 77 Required: true, 78 Type: types.SeriesList, 79 }, 80 { 81 Name: "factor", 82 Required: true, 83 Type: types.Float, 84 }, 85 }, 86 NameChange: true, // name changed 87 ValuesChange: true, // values changed 88 }, 89 } 90 }