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

     1  package randomWalk
     2  
     3  import (
     4  	"context"
     5  	"math/rand"
     6  
     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  	pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
    11  )
    12  
    13  type randomWalk struct{}
    14  
    15  func GetOrder() interfaces.Order {
    16  	return interfaces.Any
    17  }
    18  
    19  func New(configFile string) []interfaces.FunctionMetadata {
    20  	res := make([]interfaces.FunctionMetadata, 0)
    21  	f := &randomWalk{}
    22  	functions := []string{"randomWalk", "randomWalkFunction"}
    23  	for _, n := range functions {
    24  		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
    25  	}
    26  	return res
    27  }
    28  
    29  // randomWalk(name, step=60)
    30  func (f *randomWalk) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
    31  	name, err := e.GetStringArg(0)
    32  	if err != nil {
    33  		name = "randomWalk"
    34  	}
    35  	stepInt, err := e.GetIntNamedOrPosArgDefault("step", 1, 60)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	step := int64(stepInt)
    40  
    41  	size := (until - from) / step
    42  	until = from + step*size // Re-compute 'until' in case 'size' is a not a divisor of the range
    43  
    44  	r := types.MetricData{
    45  		FetchResponse: pb.FetchResponse{
    46  			Name:              name,
    47  			Values:            make([]float64, size),
    48  			StepTime:          step,
    49  			StartTime:         from,
    50  			StopTime:          until,
    51  			ConsolidationFunc: "average",
    52  		},
    53  		Tags: map[string]string{"name": name},
    54  	}
    55  
    56  	for i := 1; i < len(r.Values)-1; i++ {
    57  		r.Values[i+1] = r.Values[i] + (rand.Float64() - 0.5)
    58  	}
    59  	return []*types.MetricData{&r}, nil
    60  }
    61  
    62  // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
    63  func (f *randomWalk) Description() map[string]types.FunctionDescription {
    64  	return map[string]types.FunctionDescription{
    65  		"randomWalk": {
    66  			Description: "Short Alias: randomWalk()\n\nReturns a random walk starting at 0. This is great for testing when there is\nno real data in whisper.\n\nExample:\n\n.. code-block:: none\n\n  &target=randomWalk(\"The.time.series\")\n\nThis would create a series named \"The.time.series\" that contains points where\nx(t) == x(t-1)+random()-0.5, and x(0) == 0.\nAccepts optional second argument as 'step' parameter (default step is 60 sec)",
    67  			Function:    "randomWalk(name, step=60)",
    68  			Group:       "Special",
    69  			Module:      "graphite.render.functions",
    70  			Name:        "randomWalk",
    71  			Params: []types.FunctionParam{
    72  				{
    73  					Name:     "name",
    74  					Required: true,
    75  					Type:     types.String,
    76  				},
    77  				{
    78  					Default: types.NewSuggestion(60),
    79  					Name:    "step",
    80  					Type:    types.Integer,
    81  				},
    82  			},
    83  		},
    84  		"randomWalkFunction": {
    85  			Description: "Short Alias: randomWalk()\n\nReturns a random walk starting at 0. This is great for testing when there is\nno real data in whisper.\n\nExample:\n\n.. code-block:: none\n\n  &target=randomWalk(\"The.time.series\")\n\nThis would create a series named \"The.time.series\" that contains points where\nx(t) == x(t-1)+random()-0.5, and x(0) == 0.\nAccepts optional second argument as 'step' parameter (default step is 60 sec)",
    86  			Function:    "randomWalkFunction(name, step=60)",
    87  			Group:       "Special",
    88  			Module:      "graphite.render.functions",
    89  			Name:        "randomWalkFunction",
    90  			Params: []types.FunctionParam{
    91  				{
    92  					Name:     "name",
    93  					Required: true,
    94  					Type:     types.String,
    95  				},
    96  				{
    97  					Default: types.NewSuggestion(60),
    98  					Name:    "step",
    99  					Type:    types.Integer,
   100  				},
   101  			},
   102  		},
   103  	}
   104  }