github.com/go-graphite/carbonapi@v0.17.0/expr/functions/aliasSub/function.go (about)

     1  package aliasSub
     2  
     3  import (
     4  	"context"
     5  	"regexp"
     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 aliasSub 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 := &aliasSub{}
    22  	for _, n := range []string{"aliasSub"} {
    23  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    24  	}
    25  	return res
    26  }
    27  
    28  func (f *aliasSub) 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() < 3 {
    30  		return nil, parser.ErrMissingTimeseries
    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  	search, err := e.GetStringArg(1)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	replace, err := e.GetStringArg(2)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	re, err := regexp.Compile(search)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	replace = helper.Backref.ReplaceAllString(replace, "$${$1}")
    54  
    55  	results := make([]*types.MetricData, len(args))
    56  
    57  	for i, a := range args {
    58  		r := a.CopyLinkTags()
    59  		r.Name = re.ReplaceAllString(a.Name, replace)
    60  		results[i] = r
    61  	}
    62  
    63  	return results, nil
    64  }
    65  
    66  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    67  func (f *aliasSub) Description() map[string]types.FunctionDescription {
    68  	return map[string]types.FunctionDescription{
    69  		"aliasSub": {
    70  			Description: "Runs series names through a regex search/replace.\n\n.. code-block:: none\n\n  &target=aliasSub(ip.*TCP*,\"^.*TCP(\\d+)\",\"\\1\")",
    71  			Function:    "aliasSub(seriesList, search, replace)",
    72  			Group:       "Alias",
    73  			Module:      "graphite.render.functions",
    74  			Name:        "aliasSub",
    75  			Params: []types.FunctionParam{
    76  				{
    77  					Name:     "seriesList",
    78  					Required: true,
    79  					Type:     types.SeriesList,
    80  				},
    81  				{
    82  					Name:     "search",
    83  					Required: true,
    84  					Type:     types.String,
    85  				},
    86  				{
    87  					Name:     "replace",
    88  					Required: true,
    89  					Type:     types.String,
    90  				},
    91  			},
    92  			NameChange: true, // name changed
    93  			TagsChange: true, // name tag changed
    94  		},
    95  	}
    96  }