github.com/go-graphite/carbonapi@v0.17.0/expr/functions/absolute/function.go (about) 1 package absolute 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 absolute 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 := &absolute{} 22 for _, n := range []string{"absolute"} { 23 res = append(res, interfaces.FunctionMetadata{Name: n, F: f}) 24 } 25 return res 26 } 27 28 func (f *absolute) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) { 29 return helper.ForEachSeriesDo(ctx, eval, e, from, until, values, func(a *types.MetricData, r *types.MetricData) *types.MetricData { 30 for i, v := range a.Values { 31 if math.IsNaN(a.Values[i]) { 32 r.Values[i] = math.NaN() 33 continue 34 } 35 r.Values[i] = math.Abs(v) 36 } 37 r.Tags["absolute"] = "1" 38 return r 39 }) 40 } 41 42 // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web 43 func (f *absolute) Description() map[string]types.FunctionDescription { 44 return map[string]types.FunctionDescription{ 45 "absolute": { 46 Description: "Takes one metric or a wildcard seriesList and applies the mathematical abs function to each\ndatapoint transforming it to its absolute value.\n\nExample:\n\n.. code-block:: none\n\n &target=absolute(Server.instance01.threads.busy)\n &target=absolute(Server.instance*.threads.busy)", 47 Function: "absolute(seriesList)", 48 Group: "Transform", 49 Module: "graphite.render.functions", 50 Name: "absolute", 51 Params: []types.FunctionParam{ 52 { 53 Name: "seriesList", 54 Required: true, 55 Type: types.SeriesList, 56 }, 57 }, 58 ValuesChange: true, 59 }, 60 } 61 }