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

     1  package logarithm
     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 logarithm 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 := &logarithm{}
    23  	functions := []string{"logarithm", "log"}
    24  	for _, n := range functions {
    25  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    26  	}
    27  	return res
    28  }
    29  
    30  // logarithm(seriesList, base=10)
    31  // Alias: log
    32  func (f *logarithm) 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  	base, err := e.GetIntNamedOrPosArgDefault("base", 1, 10)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	baseStr := strconv.Itoa(base)
    43  
    44  	baseLog := math.Log(float64(base))
    45  
    46  	results := make([]*types.MetricData, len(arg))
    47  
    48  	for j, a := range arg {
    49  
    50  		var name string
    51  		if base != 10 {
    52  			name = "logarithm(" + a.Name + "," + baseStr + ")"
    53  		} else {
    54  			name = "logarithm(" + a.Name + ")"
    55  		}
    56  
    57  		r := a.CopyLink()
    58  		r.Name = name
    59  		r.Values = make([]float64, len(a.Values))
    60  		r.Tags["log"] = baseStr
    61  
    62  		for i, v := range a.Values {
    63  			r.Values[i] = math.Log(v) / baseLog
    64  		}
    65  		results[j] = r
    66  	}
    67  	return results, nil
    68  }
    69  
    70  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    71  func (f *logarithm) Description() map[string]types.FunctionDescription {
    72  	return map[string]types.FunctionDescription{
    73  		"logarithm": {
    74  			Description: "Takes one metric or a wildcard seriesList, a base, and draws the y-axis in logarithmic\nformat.  If base is omitted, the function defaults to base 10.\n\nExample:\n\n.. code-block:: none\n\n  &target=log(carbon.agents.hostname.avgUpdateTime,2)",
    75  			Function:    "log(seriesList, base=10)",
    76  			Group:       "Transform",
    77  			Module:      "graphite.render.functions",
    78  			Name:        "log",
    79  			Params: []types.FunctionParam{
    80  				{
    81  					Name:     "seriesList",
    82  					Required: true,
    83  					Type:     types.SeriesList,
    84  				},
    85  				{
    86  					Default: types.NewSuggestion(10),
    87  					Name:    "base",
    88  					Type:    types.Integer,
    89  				},
    90  			},
    91  		},
    92  		"log": {
    93  			Description: "Takes one metric or a wildcard seriesList, a base, and draws the y-axis in logarithmic\nformat.  If base is omitted, the function defaults to base 10.\n\nExample:\n\n.. code-block:: none\n\n  &target=log(carbon.agents.hostname.avgUpdateTime,2)",
    94  			Function:    "log(seriesList, base=10)",
    95  			Group:       "Transform",
    96  			Module:      "graphite.render.functions",
    97  			Name:        "log",
    98  			Params: []types.FunctionParam{
    99  				{
   100  					Name:     "seriesList",
   101  					Required: true,
   102  					Type:     types.SeriesList,
   103  				},
   104  				{
   105  					Default: types.NewSuggestion(10),
   106  					Name:    "base",
   107  					Type:    types.Integer,
   108  				},
   109  			},
   110  			NameChange:   true, // name changed
   111  			ValuesChange: true, // values changed
   112  		},
   113  	}
   114  }