github.com/go-graphite/carbonapi@v0.17.0/expr/functions/sinFunction/function.go (about) 1 package sinFunction 2 3 import ( 4 "context" 5 "math" 6 7 pb "github.com/go-graphite/protocol/carbonapi_v3_pb" 8 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 sinFunction 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 := &sinFunction{} 23 functions := []string{"sinFunction", "sin"} 24 for _, n := range functions { 25 res = append(res, interfaces.FunctionMetadata{Name: n, F: f}) 26 } 27 return res 28 } 29 30 // sinFunction(name, amplitude, step) 31 func (f *sinFunction) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) { 32 name, err := e.GetStringArg(0) 33 if err != nil { 34 return nil, err 35 } 36 37 var amplitude = 1.0 38 var stepInt = 60 39 if e.ArgsLen() >= 2 { 40 amplitude, err = e.GetFloatArgDefault(1, 1.0) 41 if err != nil { 42 return nil, err 43 } 44 } 45 if e.ArgsLen() == 3 { 46 stepInt, err = e.GetIntArgDefault(2, 60) 47 if err != nil { 48 return nil, err 49 } 50 } 51 step := int64(stepInt) 52 53 newValues := make([]float64, (until-from-1+step)/step) 54 value := from 55 for i := 0; i < len(newValues); i++ { 56 newValues[i] = math.Sin(float64(value)) * amplitude 57 value += step 58 } 59 60 r := types.MetricData{ 61 FetchResponse: pb.FetchResponse{ 62 Name: name, 63 Values: newValues, 64 StepTime: step, 65 StartTime: from, 66 StopTime: until, 67 }, 68 Tags: map[string]string{"name": name}, 69 } 70 71 return []*types.MetricData{&r}, nil 72 } 73 74 // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web 75 func (f *sinFunction) Description() map[string]types.FunctionDescription { 76 return map[string]types.FunctionDescription{ 77 "sin": { 78 Description: "Just returns the sine of the current time. The optional amplitude parameter changes the amplitude of the wave.\n" + 79 "Example:\n\n.. code-block:: none\n\n &target=sin(\"The.time.series\", 2)\n\n" + 80 "This would create a series named “The.time.series” that contains sin(x)*2. Accepts optional second argument as ‘amplitude’ parameter (default amplitude is 1)\n Accepts optional third argument as ‘step’ parameter (default step is 60 sec)\n\n" + 81 "Alias: sin", 82 Function: "sin(name, amplitude=1, step=60)", 83 Group: "Transform", 84 Module: "graphite.render.functions", 85 Name: "sin", 86 Params: []types.FunctionParam{ 87 { 88 Name: "name", 89 Required: true, 90 Type: types.String, 91 }, 92 { 93 Name: "amplitude", 94 Required: false, 95 Type: types.Integer, 96 Default: types.NewSuggestion(1), 97 }, 98 { 99 Name: "step", 100 Required: false, 101 Type: types.Integer, 102 Default: types.NewSuggestion(60), 103 }, 104 }, 105 }, 106 "sinFunction": { 107 Description: "Just returns the sine of the current time. The optional amplitude parameter changes the amplitude of the wave.\n" + 108 "Example:\n\n.. code-block:: none\n\n &target=sin(\"The.time.series\", 2)\n\n" + 109 "This would create a series named “The.time.series” that contains sin(x)*2. Accepts optional second argument as ‘amplitude’ parameter (default amplitude is 1)\n Accepts optional third argument as ‘step’ parameter (default step is 60 sec)\n\n" + 110 "Alias: sin", 111 Function: "sinFunction(name, amplitude=1, step=60)", 112 Group: "Transform", 113 Module: "graphite.render.functions", 114 Name: "sinFunction", 115 Params: []types.FunctionParam{ 116 { 117 Name: "name", 118 Required: true, 119 Type: types.String, 120 }, 121 { 122 Name: "amplitude", 123 Required: false, 124 Type: types.Integer, 125 Default: types.NewSuggestion(1), 126 }, 127 { 128 Name: "step", 129 Required: false, 130 Type: types.Integer, 131 Default: types.NewSuggestion(60), 132 }, 133 }, 134 }, 135 } 136 }