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