github.com/go-graphite/carbonapi@v0.17.0/expr/functions/round/function.go (about) 1 package round 2 3 import ( 4 "context" 5 "strconv" 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 round struct{} 14 15 func GetOrder() interfaces.Order { 16 return interfaces.Any 17 } 18 19 func New(_ string) []interfaces.FunctionMetadata { 20 res := make([]interfaces.FunctionMetadata, 0) 21 f := &round{} 22 functions := []string{"round"} 23 for _, n := range functions { 24 res = append(res, interfaces.FunctionMetadata{Name: n, F: f}) 25 } 26 return res 27 } 28 29 // round(seriesList,precision) 30 func (f *round) 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 withPrecision bool 36 var precisionStr string 37 precision, withPrecision, err := e.GetIntNamedOrPosArgWithIndication("precision", 1) 38 if err != nil { 39 return nil, err 40 } 41 precisionStr = strconv.Itoa(precision) 42 43 results := make([]*types.MetricData, len(arg)) 44 for j, a := range arg { 45 r := a.CopyLink() 46 if withPrecision { 47 r.Name = "round(" + a.Name + "," + precisionStr + ")" 48 } else { 49 r.Name = "round(" + a.Name + ")" 50 } 51 r.Values = make([]float64, len(a.Values)) 52 53 for i, v := range a.Values { 54 r.Values[i] = helper.SafeRound(v, precision) 55 } 56 57 r.Tags["round"] = precisionStr 58 results[j] = r 59 60 } 61 return results, nil 62 } 63 64 // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web 65 func (f *round) Description() map[string]types.FunctionDescription { 66 return map[string]types.FunctionDescription{ 67 "round": { 68 Description: "Takes one metric or a wildcard seriesList optionally followed by a precision, and rounds each\ndatapoint to the specified precision.\n\nExample:\n\n.. code-block:: none\n\n &target=round(Server.instance01.threads.busy)\n &target=round(Server.instance01.threads.busy,2)", 69 Function: "round(seriesList, precision)", 70 Group: "Transform", 71 Module: "graphite.render.functions", 72 Name: "round", 73 Params: []types.FunctionParam{ 74 { 75 Name: "seriesList", 76 Required: true, 77 Type: types.SeriesList, 78 }, 79 { 80 Name: "precision", 81 Required: false, 82 Type: types.Integer, 83 }, 84 }, 85 NameChange: true, // name changed 86 ValuesChange: true, // values changed 87 }, 88 } 89 }