github.com/go-graphite/carbonapi@v0.17.0/expr/functions/alias/function.go (about) 1 package alias 2 3 import ( 4 "context" 5 "strings" 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 alias 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 := &alias{} 22 for _, n := range []string{"alias"} { 23 res = append(res, interfaces.FunctionMetadata{Name: n, F: f}) 24 } 25 return res 26 } 27 28 func (f *alias) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) { 29 if e.ArgsLen() < 2 { 30 return nil, parser.ErrMissingArgument 31 } 32 33 args, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values) 34 if err != nil { 35 return nil, err 36 } 37 38 alias, err := e.GetStringArg(1) 39 if err != nil { 40 return nil, err 41 } 42 43 allowFormatStr, err := e.GetBoolArgDefault(2, false) 44 if err != nil { 45 return nil, err 46 } 47 48 results := make([]*types.MetricData, len(args)) 49 for i, arg := range args { 50 name := alias 51 if allowFormatStr { 52 name = strings.ReplaceAll(name, "${expr}", arg.Name) 53 } 54 r := arg.CopyLinkTags() 55 r.Name = name 56 results[i] = r 57 } 58 59 return results, nil 60 } 61 62 func (f *alias) Description() map[string]types.FunctionDescription { 63 return map[string]types.FunctionDescription{ 64 "alias": { 65 Description: "Takes one metric or a wildcard seriesList and a string in quotes.\n" + 66 "Prints the string instead of the metric name in the legend.\n" + 67 "If set to True, `allowFormatStr` will replace all occurrences of `${expr}` with name of expression\n\n" + 68 ".. code-block:: none\n\n" + 69 " &target=alias(Sales.widgets.largeBlue,\"Large Blue Widgets\")", 70 Function: "alias(seriesList, newName)", 71 Group: "Alias", 72 Module: "graphite.render.functions", 73 Name: "alias", 74 Params: []types.FunctionParam{ 75 { 76 Name: "seriesList", 77 Required: true, 78 Type: types.SeriesList, 79 }, 80 { 81 Name: "newName", 82 Required: true, 83 Type: types.String, 84 }, 85 { 86 Default: types.NewSuggestion(false), 87 Name: "allowFormatStr", 88 Required: false, 89 Type: types.Boolean, 90 }, 91 }, 92 NameChange: true, // name changed 93 TagsChange: true, // name tag changed 94 }, 95 } 96 }