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

     1  package removeBelowSeries
     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 removeBelowSeries 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 := &removeBelowSeries{}
    25  	functions := []string{"removeBelowValue", "removeAboveValue", "removeBelowPercentile", "removeAbovePercentile"}
    26  	for _, n := range functions {
    27  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    28  	}
    29  	return res
    30  }
    31  
    32  // removeBelowValue(seriesLists, n), removeAboveValue(seriesLists, n), removeBelowPercentile(seriesLists, percent), removeAbovePercentile(seriesLists, percent)
    33  func (f *removeBelowSeries) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    34  	args, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	number, err := e.GetFloatArg(1)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	numberStr := e.Arg(1).StringValue()
    44  
    45  	condition := func(v float64, threshold float64) bool {
    46  		return v < threshold
    47  	}
    48  
    49  	if strings.HasPrefix(e.Target(), "removeAbove") {
    50  		condition = func(v float64, threshold float64) bool {
    51  			return v > threshold
    52  		}
    53  	}
    54  
    55  	results := make([]*types.MetricData, len(args))
    56  
    57  	for n, a := range args {
    58  		threshold := number
    59  		if strings.HasSuffix(e.Target(), "Percentile") {
    60  			var values []float64
    61  			for _, v := range a.Values {
    62  				if !math.IsNaN(v) {
    63  					values = append(values, v)
    64  				}
    65  			}
    66  
    67  			threshold = consolidations.Percentile(values, number, true)
    68  		}
    69  
    70  		r := a.CopyLink()
    71  		r.Name = e.Target() + "(" + a.Name + ", " + numberStr + ")"
    72  		r.Values = make([]float64, len(a.Values))
    73  		r.Tags["removeBelowSeries"] = strconv.FormatFloat(threshold, 'f', -1, 64)
    74  
    75  		for i, v := range a.Values {
    76  			if math.IsNaN(v) || condition(v, threshold) {
    77  				r.Values[i] = math.NaN()
    78  			} else {
    79  				r.Values[i] = v
    80  			}
    81  		}
    82  
    83  		results[n] = r
    84  	}
    85  
    86  	return results, nil
    87  }
    88  
    89  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    90  func (f *removeBelowSeries) Description() map[string]types.FunctionDescription {
    91  	return map[string]types.FunctionDescription{
    92  		"removeBelowValue": {
    93  			Description: "Removes data below the given threshold from the series or list of series provided.\nValues below this threshold are assigned a value of None.",
    94  			Function:    "removeBelowValue(seriesList, n)",
    95  			Group:       "Filter Data",
    96  			Module:      "graphite.render.functions",
    97  			Name:        "removeBelowValue",
    98  			Params: []types.FunctionParam{
    99  				{
   100  					Name:     "seriesList",
   101  					Required: true,
   102  					Type:     types.SeriesList,
   103  				},
   104  				{
   105  					Name:     "n",
   106  					Required: true,
   107  					Type:     types.Integer,
   108  				},
   109  			},
   110  			NameChange:   true, // name changed
   111  			ValuesChange: true, // values changed
   112  		},
   113  		"removeAboveValue": {
   114  			Description: "Removes data above the given threshold from the series or list of series provided.\nValues above this threshold are assigned a value of None.",
   115  			Function:    "removeAboveValue(seriesList, n)",
   116  			Group:       "Filter Data",
   117  			Module:      "graphite.render.functions",
   118  			Name:        "removeAboveValue",
   119  			Params: []types.FunctionParam{
   120  				{
   121  					Name:     "seriesList",
   122  					Required: true,
   123  					Type:     types.SeriesList,
   124  				},
   125  				{
   126  					Name:     "n",
   127  					Required: true,
   128  					Type:     types.Integer,
   129  				},
   130  			},
   131  			NameChange:   true, // name changed
   132  			ValuesChange: true, // values changed
   133  		},
   134  		"removeBelowPercentile": {
   135  			Description: "Removes data below the nth percentile from the series or list of series provided.\nValues below this percentile are assigned a value of None.",
   136  			Function:    "removeBelowPercentile(seriesList, n)",
   137  			Group:       "Filter Data",
   138  			Module:      "graphite.render.functions",
   139  			Name:        "removeBelowPercentile",
   140  			Params: []types.FunctionParam{
   141  				{
   142  					Name:     "seriesList",
   143  					Required: true,
   144  					Type:     types.SeriesList,
   145  				},
   146  				{
   147  					Name:     "n",
   148  					Required: true,
   149  					Type:     types.Integer,
   150  				},
   151  			},
   152  			NameChange:   true, // name changed
   153  			ValuesChange: true, // values changed
   154  		},
   155  		"removeAbovePercentile": {
   156  			Description: "Removes data above the nth percentile from the series or list of series provided.\nValues above this percentile are assigned a value of None.",
   157  			Function:    "removeAbovePercentile(seriesList, n)",
   158  			Group:       "Filter Data",
   159  			Module:      "graphite.render.functions",
   160  			Name:        "removeAbovePercentile",
   161  			Params: []types.FunctionParam{
   162  				{
   163  					Name:     "seriesList",
   164  					Required: true,
   165  					Type:     types.SeriesList,
   166  				},
   167  				{
   168  					Name:     "n",
   169  					Required: true,
   170  					Type:     types.Integer,
   171  				},
   172  			},
   173  			NameChange:   true, // name changed
   174  			ValuesChange: true, // values changed
   175  		},
   176  	}
   177  }