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

     1  package lowPass
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  	"strconv"
     7  
     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 lowPass 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 := &lowPass{}
    23  	functions := []string{"lowPass", "lpf"}
    24  	for _, n := range functions {
    25  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    26  	}
    27  	return res
    28  }
    29  
    30  // lowPass(seriesList, cutPercent)
    31  func (f *lowPass) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    32  	arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	cutPercent, err := e.GetFloatArg(1)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	cutPercentStr := strconv.FormatFloat(cutPercent, 'g', -1, 64)
    42  
    43  	results := make([]*types.MetricData, len(arg))
    44  	for j, a := range arg {
    45  		r := a.CopyLinkTags()
    46  		r.Name = "lowPass(" + a.Name + "," + cutPercentStr + ")"
    47  		r.Values = make([]float64, len(a.Values))
    48  		lowCut := int((cutPercent / 200) * float64(len(a.Values)))
    49  		highCut := len(a.Values) - lowCut
    50  		for i, v := range a.Values {
    51  			if i < lowCut || i >= highCut {
    52  				r.Values[i] = v
    53  			} else {
    54  				r.Values[i] = math.NaN()
    55  			}
    56  		}
    57  
    58  		results[j] = r
    59  	}
    60  	return results, nil
    61  }
    62  
    63  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    64  func (f *lowPass) Description() map[string]types.FunctionDescription {
    65  	return map[string]types.FunctionDescription{
    66  		"lpf": {
    67  			Description: "Low-pass filters provide a smoother form of a signal, removing the short-term fluctuations, and leaving the longer-term trend. https://en.wikipedia.org/wiki/Low-pass_filter",
    68  			Function:    "lpf(seriesList, cutPercent)",
    69  			Group:       "Transform",
    70  			Module:      "graphite.render.functions",
    71  			Name:        "lpf",
    72  			Params: []types.FunctionParam{
    73  				{
    74  					Name:     "seriesList",
    75  					Required: true,
    76  					Type:     types.SeriesList,
    77  				},
    78  				{
    79  					Name:     "cutPercent",
    80  					Required: true,
    81  					Type:     types.Float,
    82  				},
    83  			},
    84  			NameChange:   true, // name changed
    85  			ValuesChange: true, // values changed
    86  		},
    87  		"lowPass": {
    88  			Description: "Low-pass filters provide a smoother form of a signal, removing the short-term fluctuations, and leaving the longer-term trend. https://en.wikipedia.org/wiki/Low-pass_filter",
    89  			Function:    "lowPass(seriesList, cutPercent)",
    90  			Group:       "Transform",
    91  			Module:      "graphite.render.functions.custom",
    92  			Name:        "lowPass",
    93  			Params: []types.FunctionParam{
    94  				{
    95  					Name:     "seriesList",
    96  					Required: true,
    97  					Type:     types.SeriesList,
    98  				},
    99  				{
   100  					Name:     "cutPercent",
   101  					Required: true,
   102  					Type:     types.Float,
   103  				},
   104  			},
   105  			NameChange:   true, // name changed
   106  			ValuesChange: true, // values changed
   107  		},
   108  	}
   109  }