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

     1  package aboveSeries
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"regexp"
     7  
     8  	"github.com/go-graphite/carbonapi/expr/consolidations"
     9  	"github.com/go-graphite/carbonapi/expr/helper"
    10  	"github.com/go-graphite/carbonapi/expr/interfaces"
    11  	"github.com/go-graphite/carbonapi/expr/types"
    12  	"github.com/go-graphite/carbonapi/pkg/parser"
    13  )
    14  
    15  type aboveSeries struct {
    16  	interfaces.Function
    17  }
    18  
    19  func GetOrder() interfaces.Order {
    20  	return interfaces.Any
    21  }
    22  
    23  func New(configFile string) []interfaces.RewriteFunctionMetadata {
    24  	res := make([]interfaces.RewriteFunctionMetadata, 0)
    25  	f := &aboveSeries{}
    26  	functions := []string{"useSeriesAbove", "aboveSeries"}
    27  	for _, n := range functions {
    28  		res = append(res, interfaces.RewriteFunctionMetadata{Name: n, F: f})
    29  	}
    30  	return res
    31  }
    32  
    33  func (f *aboveSeries) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) (bool, []string, error) {
    34  	args, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    35  	if err != nil {
    36  		return false, nil, err
    37  	}
    38  
    39  	max, err := e.GetFloatArg(1)
    40  	if err != nil {
    41  		return false, nil, err
    42  	}
    43  
    44  	search, err := e.GetStringArg(2)
    45  	if err != nil {
    46  		return false, nil, err
    47  	}
    48  
    49  	replace, err := e.GetStringArg(3)
    50  	if err != nil {
    51  		return false, nil, err
    52  	}
    53  
    54  	rre, err := regexp.Compile(search)
    55  	if err != nil {
    56  		return false, nil, err
    57  	}
    58  
    59  	var rv []string
    60  	for _, a := range args {
    61  		if consolidations.MaxValue(a.Values) > max {
    62  			rv = append(rv, rre.ReplaceAllString(a.Name, replace))
    63  		}
    64  	}
    65  
    66  	fmt.Printf("\n\n\n\n%+v\n\n\n", rv)
    67  
    68  	return true, rv, nil
    69  }
    70  
    71  func (f *aboveSeries) Description() map[string]types.FunctionDescription {
    72  	return map[string]types.FunctionDescription{
    73  		"useSeriesAbove": {
    74  			Name:        "useSeriesAbove",
    75  			Description: "Takes a seriesList and compares the maximum of each series against the given value. If the series maximum is greater than value, the regular expression search and replace is applied against the series name to plot a related metric e.g. given useSeriesAbove(ganglia.metric1.reqs,10,’reqs’,’time’), the response time metric will be plotted only when the maximum value of the corresponding request/s metric is > 10\n\nShort form: aboveSeries()",
    76  			Function:    "useSeriesAbove(seriesList, value, search, replace)",
    77  			Group:       "Filter Series",
    78  			Module:      "graphite.render.functions",
    79  			Params: []types.FunctionParam{
    80  				{
    81  					Name:     "seriesList",
    82  					Required: true,
    83  					Type:     types.SeriesList,
    84  				},
    85  				{
    86  					Name:     "value",
    87  					Required: true,
    88  					Type:     types.Float,
    89  				},
    90  				{
    91  					Name:     "search",
    92  					Required: true,
    93  					Type:     types.String,
    94  				},
    95  				{
    96  					Name:     "replace",
    97  					Required: true,
    98  					Type:     types.String,
    99  				},
   100  			},
   101  		},
   102  	}
   103  }