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

     1  package legendValue
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/go-graphite/carbonapi/expr/consolidations"
    10  	"github.com/go-graphite/carbonapi/expr/helper"
    11  	"github.com/go-graphite/carbonapi/expr/interfaces"
    12  	"github.com/go-graphite/carbonapi/expr/types"
    13  	"github.com/go-graphite/carbonapi/pkg/parser"
    14  )
    15  
    16  type legendValue struct{}
    17  
    18  func GetOrder() interfaces.Order {
    19  	return interfaces.Any
    20  }
    21  
    22  func New(configFile string) []interfaces.FunctionMetadata {
    23  	res := make([]interfaces.FunctionMetadata, 0)
    24  	f := &legendValue{}
    25  	functions := []string{"legendValue"}
    26  	for _, n := range functions {
    27  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    28  	}
    29  	return res
    30  }
    31  
    32  // legendValue(seriesList, newName)
    33  func (f *legendValue) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    34  	if e.ArgsLen() < 2 {
    35  		return nil, parser.ErrMissingArgument
    36  	}
    37  
    38  	arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	var system string
    44  	var methods []string
    45  	for i := 1; i < e.ArgsLen(); i++ {
    46  		method, err := e.GetStringArg(i)
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  		if method == "si" || method == "binary" {
    51  			system = method
    52  		} else {
    53  			if err := consolidations.CheckValidConsolidationFunc(method); err != nil {
    54  				return nil, err
    55  			}
    56  			methods = append(methods, method)
    57  		}
    58  	}
    59  
    60  	results := make([]*types.MetricData, len(arg))
    61  	for i, a := range arg {
    62  		r := a.CopyLink()
    63  		var nameBuf strings.Builder
    64  		nameBuf.Grow(len(r.Name) + len(methods)*5)
    65  		nameBuf.WriteString(r.Name)
    66  		for _, method := range methods {
    67  			summary := consolidations.SummarizeValues(method, a.Values, a.XFilesFactor)
    68  			nameBuf.WriteString(" (")
    69  			nameBuf.WriteString(method)
    70  			nameBuf.WriteString(": ")
    71  			if system == "" {
    72  				nameBuf.WriteString(strconv.FormatFloat(summary, 'g', -1, 64))
    73  			} else {
    74  				v, prefix := helper.FormatUnits(summary, system)
    75  				if prefix != "" {
    76  					prefix += " "
    77  				}
    78  
    79  				if math.Abs(v) < 0.1 {
    80  					nameBuf.WriteString(strconv.FormatFloat(v, 'g', 9, 64))
    81  				} else {
    82  					nameBuf.WriteString(strconv.FormatFloat(v, 'f', 2, 64))
    83  				}
    84  
    85  				nameBuf.WriteString(prefix)
    86  			}
    87  			nameBuf.WriteString(")")
    88  		}
    89  		r.Name = nameBuf.String()
    90  
    91  		results[i] = r
    92  	}
    93  	return results, nil
    94  }
    95  
    96  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    97  func (f *legendValue) Description() map[string]types.FunctionDescription {
    98  	return map[string]types.FunctionDescription{
    99  		"legendValue": {
   100  			Description: "Takes one metric or a wildcard seriesList and a string in quotes.\nAppends a value to the metric name in the legend.  Currently one or several of: `last`, `avg`,\n`total`, `min`, `max`.\nThe last argument can be `si` (default) or `binary`, in that case values will be formatted in the\ncorresponding system.\n\n.. code-block:: none\n\n  &target=legendValue(Sales.widgets.largeBlue, 'avg', 'max', 'si')",
   101  			Function:    "legendValue(seriesList, *valueTypes)",
   102  			Group:       "Alias",
   103  			Module:      "graphite.render.functions",
   104  			Name:        "legendValue",
   105  			Params: []types.FunctionParam{
   106  				{
   107  					Name:     "seriesList",
   108  					Required: true,
   109  					Type:     types.SeriesList,
   110  				},
   111  				{
   112  					Multiple: true,
   113  					Name:     "valuesTypes",
   114  					Options:  types.StringsToSuggestionList(consolidations.AvailableSummarizers),
   115  					Type:     types.String,
   116  				},
   117  			},
   118  			NameChange: true, // name changed
   119  		},
   120  	}
   121  }