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

     1  package below
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/go-graphite/carbonapi/expr/consolidations"
     8  	"github.com/go-graphite/carbonapi/expr/helper"
     9  	"github.com/go-graphite/carbonapi/expr/interfaces"
    10  	"github.com/go-graphite/carbonapi/expr/types"
    11  	"github.com/go-graphite/carbonapi/pkg/parser"
    12  )
    13  
    14  type below struct{}
    15  
    16  func GetOrder() interfaces.Order {
    17  	return interfaces.Any
    18  }
    19  
    20  func New(configFile string) []interfaces.FunctionMetadata {
    21  	res := make([]interfaces.FunctionMetadata, 0)
    22  	f := &below{}
    23  	functions := []string{"averageAbove", "averageBelow", "currentAbove", "currentBelow", "maximumAbove", "maximumBelow", "minimumAbove", "minimumBelow"}
    24  	for _, n := range functions {
    25  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    26  	}
    27  	return res
    28  }
    29  
    30  // averageAbove(seriesList, n), averageBelow(seriesList, n), currentAbove(seriesList, n), currentBelow(seriesList, n), maximumAbove(seriesList, n), maximumBelow(seriesList, n), minimumAbove(seriesList, n), minimumBelow
    31  func (f *below) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    32  	if e.ArgsLen() < 2 {
    33  		return nil, parser.ErrMissingArgument
    34  	}
    35  
    36  	args, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	n, err := e.GetFloatArg(1)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	isAbove := strings.HasSuffix(e.Target(), "Above")
    47  	var compute func([]float64) float64
    48  	switch {
    49  	case strings.HasPrefix(e.Target(), "average"):
    50  		compute = consolidations.AvgValue
    51  	case strings.HasPrefix(e.Target(), "current"):
    52  		compute = consolidations.CurrentValue
    53  	case strings.HasPrefix(e.Target(), "maximum"):
    54  		compute = consolidations.MaxValue
    55  	case strings.HasPrefix(e.Target(), "minimum"):
    56  		compute = consolidations.MinValue
    57  	}
    58  	results := make([]*types.MetricData, 0, len(args))
    59  	for _, a := range args {
    60  		value := compute(a.Values)
    61  		if isAbove {
    62  			if value > n {
    63  				results = append(results, a)
    64  			}
    65  		} else if value <= n {
    66  			results = append(results, a)
    67  		}
    68  	}
    69  
    70  	return results, err
    71  }
    72  
    73  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    74  func (f *below) Description() map[string]types.FunctionDescription {
    75  	return map[string]types.FunctionDescription{
    76  		"averageAbove": {
    77  			Description: "Takes one metric or a wildcard seriesList followed by an integer N.\nOut of all metrics passed, draws only the metrics with an average value\nabove N for the time period specified.\n\nExample:\n\n.. code-block:: none\n\n  &target=averageAbove(server*.instance*.threads.busy,25)\n\nDraws the servers with average values above 25.",
    78  			Function:    "averageAbove(seriesList, n)",
    79  			Group:       "Filter Series",
    80  			Module:      "graphite.render.functions",
    81  			Name:        "averageAbove",
    82  			Params: []types.FunctionParam{
    83  				{
    84  					Name:     "seriesList",
    85  					Required: true,
    86  					Type:     types.SeriesList,
    87  				},
    88  				{
    89  					Name:     "n",
    90  					Required: true,
    91  					Type:     types.Integer,
    92  				},
    93  			},
    94  			SeriesChange: true, // function aggregate metrics
    95  			NameChange:   true, // name changed
    96  			TagsChange:   true, // name tag changed
    97  		},
    98  		"averageBelow": {
    99  			Description: "Takes one metric or a wildcard seriesList followed by an integer N.\nOut of all metrics passed, draws only the metrics with an average value\nbelow N for the time period specified.\n\nExample:\n\n.. code-block:: none\n\n  &target=averageBelow(server*.instance*.threads.busy,25)\n\nDraws the servers with average values below 25.",
   100  			Function:    "averageBelow(seriesList, n)",
   101  			Group:       "Filter Series",
   102  			Module:      "graphite.render.functions",
   103  			Name:        "averageBelow",
   104  			Params: []types.FunctionParam{
   105  				{
   106  					Name:     "seriesList",
   107  					Required: true,
   108  					Type:     types.SeriesList,
   109  				},
   110  				{
   111  					Name:     "n",
   112  					Required: true,
   113  					Type:     types.Integer,
   114  				},
   115  			},
   116  			SeriesChange: true, // function aggregate metrics
   117  			NameChange:   true, // name changed
   118  			TagsChange:   true, // name tag changed
   119  		},
   120  		"currentAbove": {
   121  			Description: "Takes one metric or a wildcard seriesList followed by an integer N.\nOut of all metrics passed, draws only the  metrics whose value is above N\nat the end of the time period specified.\n\nExample:\n\n.. code-block:: none\n\n  &target=currentAbove(server*.instance*.threads.busy,50)\n\nDraws the servers with more than 50 busy threads.",
   122  			Function:    "currentAbove(seriesList, n)",
   123  			Group:       "Filter Series",
   124  			Module:      "graphite.render.functions",
   125  			Name:        "currentAbove",
   126  			Params: []types.FunctionParam{
   127  				{
   128  					Name:     "seriesList",
   129  					Required: true,
   130  					Type:     types.SeriesList,
   131  				},
   132  				{
   133  					Name:     "n",
   134  					Required: true,
   135  					Type:     types.Integer,
   136  				},
   137  			},
   138  			SeriesChange: true, // function aggregate metrics
   139  			NameChange:   true, // name changed
   140  			TagsChange:   true, // name tag changed
   141  		},
   142  		"currentBelow": {
   143  			Description: "Takes one metric or a wildcard seriesList followed by an integer N.\nOut of all metrics passed, draws only the  metrics whose value is below N\nat the end of the time period specified.\n\nExample:\n\n.. code-block:: none\n\n  &target=currentBelow(server*.instance*.threads.busy,3)\n\nDraws the servers with less than 3 busy threads.",
   144  			Function:    "currentBelow(seriesList, n)",
   145  			Group:       "Filter Series",
   146  			Module:      "graphite.render.functions",
   147  			Name:        "currentBelow",
   148  			Params: []types.FunctionParam{
   149  				{
   150  					Name:     "seriesList",
   151  					Required: true,
   152  					Type:     types.SeriesList,
   153  				},
   154  				{
   155  					Name:     "n",
   156  					Required: true,
   157  					Type:     types.Integer,
   158  				},
   159  			},
   160  			SeriesChange: true, // function aggregate metrics
   161  			NameChange:   true, // name changed
   162  			TagsChange:   true, // name tag changed
   163  		},
   164  		"maximumAbove": {
   165  			Description: "Takes one metric or a wildcard seriesList followed by a constant n.\nDraws only the metrics with a maximum value above n.\n\nExample:\n\n.. code-block:: none\n\n  &target=maximumAbove(system.interface.eth*.packetsSent,1000)\n\nThis would only display interfaces which sent more than 1000 packets/min.",
   166  			Function:    "maximumAbove(seriesList, n)",
   167  			Group:       "Filter Series",
   168  			Module:      "graphite.render.functions",
   169  			Name:        "maximumAbove",
   170  			Params: []types.FunctionParam{
   171  				{
   172  					Name:     "seriesList",
   173  					Required: true,
   174  					Type:     types.SeriesList,
   175  				},
   176  				{
   177  					Name:     "n",
   178  					Required: true,
   179  					Type:     types.Integer,
   180  				},
   181  			},
   182  			SeriesChange: true, // function aggregate metrics
   183  			NameChange:   true, // name changed
   184  			TagsChange:   true, // name tag changed
   185  		},
   186  		"maximumBelow": {
   187  			Description: "Takes one metric or a wildcard seriesList followed by a constant n.\nDraws only the metrics with a maximum value below n.\n\nExample:\n\n.. code-block:: none\n\n  &target=maximumBelow(system.interface.eth*.packetsSent,1000)\n\nThis would only display interfaces which sent less than 1000 packets/min.",
   188  			Function:    "maximumBelow(seriesList, n)",
   189  			Group:       "Filter Series",
   190  			Module:      "graphite.render.functions",
   191  			Name:        "maximumBelow",
   192  			Params: []types.FunctionParam{
   193  				{
   194  					Name:     "seriesList",
   195  					Required: true,
   196  					Type:     types.SeriesList,
   197  				},
   198  				{
   199  					Name:     "n",
   200  					Required: true,
   201  					Type:     types.Integer,
   202  				},
   203  			},
   204  			SeriesChange: true, // function aggregate metrics
   205  			NameChange:   true, // name changed
   206  			TagsChange:   true, // name tag changed
   207  		},
   208  		"minimumAbove": {
   209  			Description: "Takes one metric or a wildcard seriesList followed by a constant n.\nDraws only the metrics with a minimum value above n.\n\nExample:\n\n.. code-block:: none\n\n  &target=minimumAbove(system.interface.eth*.packetsSent,1000)\n\nThis would only display interfaces which sent more than 1000 packets/min.",
   210  			Function:    "minimumAbove(seriesList, n)",
   211  			Group:       "Filter Series",
   212  			Module:      "graphite.render.functions",
   213  			Name:        "minimumAbove",
   214  			Params: []types.FunctionParam{
   215  				{
   216  					Name:     "seriesList",
   217  					Required: true,
   218  					Type:     types.SeriesList,
   219  				},
   220  				{
   221  					Name:     "n",
   222  					Required: true,
   223  					Type:     types.Integer,
   224  				},
   225  			},
   226  			SeriesChange: true, // function aggregate metrics
   227  			NameChange:   true, // name changed
   228  			TagsChange:   true, // name tag changed
   229  		},
   230  		"minimumBelow": {
   231  			Description: "Takes one metric or a wildcard seriesList followed by a constant n.\nDraws only the metrics with a minimum value below n.\n\nExample:\n\n.. code-block:: none\n\n  &target=minimumBelow(system.interface.eth*.packetsSent,1000)\n\nThis would only display interfaces which at one point sent less than 1000 packets/min.",
   232  			Function:    "minimumBelow(seriesList, n)",
   233  			Group:       "Filter Series",
   234  			Module:      "graphite.render.functions",
   235  			Name:        "minimumBelow",
   236  			Params: []types.FunctionParam{
   237  				{
   238  					Name:     "seriesList",
   239  					Required: true,
   240  					Type:     types.SeriesList,
   241  				},
   242  				{
   243  					Name:     "n",
   244  					Required: true,
   245  					Type:     types.Integer,
   246  				},
   247  			},
   248  			SeriesChange: true, // function aggregate metrics
   249  			NameChange:   true, // name changed
   250  			TagsChange:   true, // name tag changed
   251  		},
   252  	}
   253  }