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

     1  package aliasByBase64
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"strings"
     7  
     8  	"github.com/go-graphite/carbonapi/expr/helper"
     9  	"github.com/go-graphite/carbonapi/expr/interfaces"
    10  	"github.com/go-graphite/carbonapi/expr/types"
    11  	"github.com/go-graphite/carbonapi/pkg/parser"
    12  	"github.com/msaf1980/go-stringutils"
    13  )
    14  
    15  type aliasByBase64 struct{}
    16  
    17  func GetOrder() interfaces.Order {
    18  	return interfaces.Any
    19  }
    20  
    21  func New(configFile string) []interfaces.FunctionMetadata {
    22  	res := make([]interfaces.FunctionMetadata, 0)
    23  	f := &aliasByBase64{}
    24  	for _, n := range []string{"aliasByBase64"} {
    25  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    26  	}
    27  	return res
    28  }
    29  
    30  func (f *aliasByBase64) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    31  	args, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	field, err := e.GetIntArg(1)
    37  	field--
    38  	withoutFieldArg := err != nil
    39  
    40  	results := make([]*types.MetricData, len(args))
    41  
    42  	for k, a := range args {
    43  		var r *types.MetricData
    44  		if withoutFieldArg {
    45  			decoded, err := base64.StdEncoding.DecodeString(a.Name)
    46  			if err == nil {
    47  				r = a.CopyName(string(decoded))
    48  			} else {
    49  				r = a
    50  			}
    51  		} else {
    52  			var changed bool
    53  			metric := a.Tags["name"]
    54  			nodeList := strings.Split(metric, ".")
    55  			if field < len(nodeList) {
    56  				decoded, err := base64.StdEncoding.DecodeString(nodeList[field])
    57  				if err == nil {
    58  					n := stringutils.UnsafeString(decoded)
    59  					nodeList[field] = n
    60  					changed = true
    61  				}
    62  			}
    63  			if changed {
    64  				r = a.CopyName(strings.Join(nodeList, "."))
    65  			} else {
    66  				r = a
    67  			}
    68  		}
    69  
    70  		results[k] = r
    71  	}
    72  
    73  	return results, nil
    74  }
    75  
    76  func (f *aliasByBase64) Description() map[string]types.FunctionDescription {
    77  	return map[string]types.FunctionDescription{
    78  		"aliasByBase64": {
    79  			Description: "Takes a seriesList and decodes its name with base64\n" +
    80  				"if node not specified, whole metric name will be decoded as bas64, otherwise only specific node will be\n\n" +
    81  				".. code-block:: none\n\n" +
    82  				"  &target=aliasByBase64(bWV0cmljLm5hbWU=)",
    83  			Function: "aliasByBase64(seriesList)",
    84  			Group:    "Alias",
    85  			Module:   "graphite.render.functions",
    86  			Name:     "aliasByBase64",
    87  			Params: []types.FunctionParam{
    88  				{
    89  					Name:     "seriesList",
    90  					Required: true,
    91  					Type:     types.SeriesList,
    92  				},
    93  				{
    94  					Name:     "nodeNum",
    95  					Required: false,
    96  					Type:     types.NodeOrTag,
    97  				},
    98  			},
    99  			NameChange: true, // name changed
   100  			TagsChange: true, // name tag changed
   101  		},
   102  	}
   103  }