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

     1  package fallbackSeries
     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 fallbackSeries 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 := &fallbackSeries{}
    21  	functions := []string{"fallbackSeries"}
    22  	for _, n := range functions {
    23  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    24  	}
    25  	return res
    26  }
    27  
    28  // fallbackSeries( seriesList, fallback )
    29  func (f *fallbackSeries) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    30  	/*
    31  		Takes a wildcard seriesList, and a second fallback metric.
    32  		If the wildcard does not match any series, draws the fallback metric.
    33  	*/
    34  	if e.ArgsLen() < 2 {
    35  		return nil, parser.ErrMissingTimeseries
    36  	}
    37  
    38  	seriesList, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    39  	fallback, errFallback := helper.GetSeriesArg(ctx, eval, e.Arg(1), from, until, values)
    40  	if errFallback != nil && err != nil {
    41  		return nil, errFallback
    42  	}
    43  
    44  	if len(seriesList) > 0 {
    45  		return seriesList, nil
    46  	}
    47  	return fallback, nil
    48  }
    49  
    50  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    51  func (f *fallbackSeries) Description() map[string]types.FunctionDescription {
    52  	return map[string]types.FunctionDescription{
    53  		"fallbackSeries": {
    54  			Description: "Takes a wildcard seriesList, and a second fallback metric.\nIf the wildcard does not match any series, draws the fallback metric.\n\nExample:\n\n.. code-block:: none\n\n  &target=fallbackSeries(server*.requests_per_second, constantLine(0))\n\nDraws a 0 line when server metric does not exist.",
    55  			Function:    "fallbackSeries(seriesList, fallback)",
    56  			Group:       "Special",
    57  			Module:      "graphite.render.functions",
    58  			Name:        "fallbackSeries",
    59  			Params: []types.FunctionParam{
    60  				{
    61  					Name:     "seriesList",
    62  					Required: true,
    63  					Type:     types.SeriesList,
    64  				},
    65  				{
    66  					Name:     "fallback",
    67  					Required: true,
    68  					Type:     types.SeriesList,
    69  				},
    70  			},
    71  		},
    72  	}
    73  }