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

     1  package limit
     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 limit 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 := &limit{}
    21  	functions := []string{"limit"}
    22  	for _, n := range functions {
    23  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    24  	}
    25  	return res
    26  }
    27  
    28  // limit(seriesList, n)
    29  func (f *limit) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    30  	if e.ArgsLen() < 2 {
    31  		return nil, parser.ErrMissingArgument
    32  	}
    33  
    34  	arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	limit, err := e.GetIntArg(1) // get limit
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	if limit >= len(arg) {
    45  		return arg, nil
    46  	}
    47  
    48  	return arg[:limit], nil
    49  }
    50  
    51  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    52  func (f *limit) Description() map[string]types.FunctionDescription {
    53  	return map[string]types.FunctionDescription{
    54  		"limit": {
    55  			Description: "Takes one metric or a wildcard seriesList followed by an integer N.\n\nOnly draw the first N metrics.  Useful when testing a wildcard in a metric.\n\nExample:\n\n.. code-block:: none\n\n  &target=limit(server*.instance*.memory.free,5)\n\nDraws only the first 5 instance's memory free.",
    56  			Function:    "limit(seriesList, n)",
    57  			Group:       "Filter Series",
    58  			Module:      "graphite.render.functions",
    59  			Name:        "limit",
    60  			Params: []types.FunctionParam{
    61  				{
    62  					Name:     "seriesList",
    63  					Required: true,
    64  					Type:     types.SeriesList,
    65  				},
    66  				{
    67  					Name:     "n",
    68  					Required: true,
    69  					Type:     types.Integer,
    70  				},
    71  			},
    72  			SeriesChange: true, // function aggregate metrics or change series items count
    73  		},
    74  	}
    75  }