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

     1  package grep
     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 grep 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 := &grep{}
    22  	functions := []string{"grep"}
    23  	for _, n := range functions {
    24  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    25  	}
    26  	return res
    27  }
    28  
    29  // grep(seriesList, pattern)
    30  func (f *grep) 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  	arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	pat, err := e.GetStringArg(1)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	patre, err := regexp.Compile(pat)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	results := make([]*types.MetricData, 0, len(arg))
    51  
    52  	for _, a := range arg {
    53  		if patre.MatchString(a.Name) {
    54  			results = append(results, a)
    55  		}
    56  	}
    57  
    58  	return results, nil
    59  }
    60  
    61  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    62  func (f *grep) Description() map[string]types.FunctionDescription {
    63  	return map[string]types.FunctionDescription{
    64  		"grep": {
    65  			Description: "Takes a metric or a wildcard seriesList, followed by a regular expression\nin double quotes.  Excludes metrics that don't match the regular expression.\n\nExample:\n\n.. code-block:: none\n\n  &target=grep(servers*.instance*.threads.busy,\"server02\")",
    66  			Function:    "grep(seriesList, pattern)",
    67  			Group:       "Filter Series",
    68  			Module:      "graphite.render.functions",
    69  			Name:        "grep",
    70  			Params: []types.FunctionParam{
    71  				{
    72  					Name:     "seriesList",
    73  					Required: true,
    74  					Type:     types.SeriesList,
    75  				},
    76  				{
    77  					Name:     "pattern",
    78  					Required: true,
    79  					Type:     types.String,
    80  				},
    81  			},
    82  			SeriesChange: true, // function aggregate metrics or change series items count
    83  		},
    84  	}
    85  }