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

     1  package minMax
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"math"
     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 minMax struct{}
    16  
    17  func GetOrder() interfaces.Order {
    18  	return interfaces.Any
    19  }
    20  
    21  func New(configFile string) []interfaces.FunctionMetadata {
    22  	res := make([]interfaces.FunctionMetadata, 0)
    23  	f := &minMax{}
    24  	functions := []string{"minMax"}
    25  	for _, n := range functions {
    26  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    27  	}
    28  	return res
    29  }
    30  
    31  // highestAverage(seriesList, n) , highestCurrent(seriesList, n), highestMax(seriesList, n)
    32  func (f *minMax) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    33  	arg, err := helper.GetSeriesArg(ctx, eval, e.Arg(0), from, until, values)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	var results []*types.MetricData
    39  
    40  	for _, a := range arg {
    41  		r := a.CopyLinkTags()
    42  		r.Name = fmt.Sprintf("minMax(%s)", a.Name)
    43  		r.Values = make([]float64, len(a.Values))
    44  
    45  		min := consolidations.MinValue(a.Values)
    46  		if math.IsInf(min, 1) {
    47  			min = 0.0
    48  		}
    49  		max := consolidations.MaxValue(a.Values)
    50  		if math.IsInf(max, -1) {
    51  			max = 0.0
    52  		}
    53  
    54  		for i, v := range a.Values {
    55  			if !math.IsNaN(v) {
    56  				if max != min {
    57  					r.Values[i] = (v - min) / (max - min)
    58  				} else {
    59  					r.Values[i] = 0.0
    60  				}
    61  			} else {
    62  				r.Values[i] = math.NaN()
    63  			}
    64  		}
    65  		results = append(results, r)
    66  	}
    67  
    68  	return results, nil
    69  }
    70  
    71  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    72  func (f *minMax) Description() map[string]types.FunctionDescription {
    73  	return map[string]types.FunctionDescription{
    74  		"minMax": {
    75  			Description: "Applies the popular min max normalization technique, which takes each point and applies the following normalization transformation to it: normalized = (point - min) / (max - min).\n\nExample:\n\n.. code-block:: none\n\n  &target=minMax(Server.instance01.threads.busy)",
    76  			Function:    "minMax(seriesList)",
    77  			Group:       "Transform",
    78  			Module:      "graphite.render.functions",
    79  			Name:        "minMax",
    80  			Params: []types.FunctionParam{
    81  				{
    82  					Name:     "seriesList",
    83  					Required: true,
    84  					Type:     types.SeriesList,
    85  				},
    86  			},
    87  		},
    88  	}
    89  }