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

     1  package mapSeries
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/go-graphite/carbonapi/expr/helper"
     7  	"github.com/go-graphite/carbonapi/expr/interfaces"
     8  	"github.com/go-graphite/carbonapi/expr/types"
     9  	"github.com/go-graphite/carbonapi/pkg/parser"
    10  )
    11  
    12  type mapSeries struct{}
    13  
    14  func GetOrder() interfaces.Order {
    15  	return interfaces.Any
    16  }
    17  
    18  func New(configFile string) []interfaces.FunctionMetadata {
    19  	res := make([]interfaces.FunctionMetadata, 0)
    20  	f := &mapSeries{}
    21  	functions := []string{"mapSeries", "map"}
    22  	for _, n := range functions {
    23  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    24  	}
    25  	return res
    26  }
    27  
    28  // mapSeries(seriesList, *mapNodes)
    29  // Alias: map
    30  func (f *mapSeries) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    31  	if e.ArgsLen() < 2 {
    32  		return nil, parser.ErrMissingArgument
    33  	}
    34  
    35  	args, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	nodesOrTags, err := e.GetNodeOrTagArgs(1, false)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	groups := make(map[string][]*types.MetricData)
    46  	var nodeList []string
    47  
    48  	for _, a := range args {
    49  		node := helper.AggKey(a, nodesOrTags)
    50  		if len(groups[node]) == 0 {
    51  			nodeList = append(nodeList, node)
    52  		}
    53  
    54  		groups[node] = append(groups[node], a)
    55  	}
    56  
    57  	results := make([]*types.MetricData, 0, len(args))
    58  	for _, node := range nodeList {
    59  		results = append(results, groups[node]...)
    60  	}
    61  
    62  	return results, nil
    63  }
    64  
    65  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    66  func (f *mapSeries) Description() map[string]types.FunctionDescription {
    67  	return map[string]types.FunctionDescription{
    68  		"mapSeries": {
    69  			Description: "Short form: ``map()``\n\nTakes a seriesList and maps it to a list of seriesList. Each seriesList has the\ngiven mapNodes in common.\n\n.. note:: This function is not very useful alone. It should be used with :py:func:`reduceSeries`\n\n.. code-block:: none\n\n  mapSeries(servers.*.cpu.*,1) =>\n\n    [\n      servers.server1.cpu.*,\n      servers.server2.cpu.*,\n      ...\n      servers.serverN.cpu.*\n    }\n\nEach node may be an integer referencing a node in the series name or a string identifying a tag.",
    70  			Function:    "mapSeries(seriesList, *mapNodes)",
    71  			Group:       "Combine",
    72  			Module:      "graphite.render.functions",
    73  			Name:        "mapSeries",
    74  			Params: []types.FunctionParam{
    75  				{
    76  					Name:     "seriesList",
    77  					Required: true,
    78  					Type:     types.SeriesList,
    79  				},
    80  				{
    81  					Multiple: true,
    82  					Name:     "mapNodes",
    83  					Required: true,
    84  					Type:     types.NodeOrTag,
    85  				},
    86  			},
    87  			SeriesChange: true, // function aggregate metrics or change series items count
    88  			NameChange:   true, // name changed
    89  			TagsChange:   true, // name tag changed
    90  			ValuesChange: true, // values changed
    91  		},
    92  		"map": {
    93  			Description: "Short form: ``map()``\n\nTakes a seriesList and maps it to a list of seriesList. Each seriesList has the\ngiven mapNodes in common.\n\n.. note:: This function is not very useful alone. It should be used with :py:func:`reduceSeries`\n\n.. code-block:: none\n\n  mapSeries(servers.*.cpu.*,1) =>\n\n    [\n      servers.server1.cpu.*,\n      servers.server2.cpu.*,\n      ...\n      servers.serverN.cpu.*\n    }\n\nEach node may be an integer referencing a node in the series name or a string identifying a tag.",
    94  			Function:    "map(seriesList, *mapNodes)",
    95  			Group:       "Combine",
    96  			Module:      "graphite.render.functions",
    97  			Name:        "map",
    98  			Params: []types.FunctionParam{
    99  				{
   100  					Name:     "seriesList",
   101  					Required: true,
   102  					Type:     types.SeriesList,
   103  				},
   104  				{
   105  					Multiple: true,
   106  					Name:     "mapNodes",
   107  					Required: true,
   108  					Type:     types.NodeOrTag,
   109  				},
   110  			},
   111  		},
   112  	}
   113  }