github.com/go-graphite/carbonapi@v0.17.0/expr/functions/timeFunction/function.go (about) 1 package timeFunction 2 3 import ( 4 "context" 5 "errors" 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 timeFunction 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 := &timeFunction{} 22 functions := []string{"timeFunction", "time"} 23 for _, n := range functions { 24 res = append(res, interfaces.FunctionMetadata{Name: n, F: f}) 25 } 26 return res 27 } 28 29 func (f *timeFunction) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) { 30 name, err := e.GetStringArg(0) 31 if err != nil { 32 return nil, err 33 } 34 35 stepInt, err := e.GetIntArgDefault(1, 60) 36 if err != nil { 37 return nil, err 38 } 39 if stepInt <= 0 { 40 return nil, errors.New("step can't be less than 0") 41 } 42 step := int64(stepInt) 43 44 // emulate the behavior of this Python code: 45 // while when < requestContext["endTime"]: 46 // newValues.append(time.mktime(when.timetuple())) 47 // when += delta 48 49 newValues := make([]float64, (until-from-1+step)/step) 50 value := from 51 for i := 0; i < len(newValues); i++ { 52 newValues[i] = float64(value) 53 value += step 54 } 55 56 p := types.MetricData{ 57 FetchResponse: pb.FetchResponse{ 58 Name: name, 59 StartTime: from, 60 StopTime: until, 61 StepTime: step, 62 Values: newValues, 63 }, 64 Tags: map[string]string{"name": name}, 65 } 66 67 return []*types.MetricData{&p}, nil 68 } 69 70 // Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web 71 func (f *timeFunction) Description() map[string]types.FunctionDescription { 72 return map[string]types.FunctionDescription{ 73 "timeFunction": { 74 Description: "Short Alias: time()\n\nJust returns the timestamp for each X value. T\n\nExample:\n\n.. code-block:: none\n\n &target=time(\"The.time.series\")\n\nThis would create a series named \"The.time.series\" that contains in Y the same\nvalue (in seconds) as X.\nAccepts optional second argument as 'step' parameter (default step is 60 sec)", 75 Function: "timeFunction(name, step=60)", 76 Group: "Transform", 77 Module: "graphite.render.functions", 78 Name: "timeFunction", 79 Params: []types.FunctionParam{ 80 { 81 Name: "name", 82 Required: true, 83 Type: types.String, 84 }, 85 { 86 Default: types.NewSuggestion(60), 87 Name: "step", 88 Type: types.Integer, 89 }, 90 }, 91 }, 92 "time": { 93 Description: "Short Alias: time()\n\nJust returns the timestamp for each X value. T\n\nExample:\n\n.. code-block:: none\n\n &target=time(\"The.time.series\")\n\nThis would create a series named \"The.time.series\" that contains in Y the same\nvalue (in seconds) as X.\nAccepts optional second argument as 'step' parameter (default step is 60 sec)", 94 Function: "time(name, step=60)", 95 Group: "Transform", 96 Module: "graphite.render.functions", 97 Name: "time", 98 Params: []types.FunctionParam{ 99 { 100 Name: "name", 101 Required: true, 102 Type: types.String, 103 }, 104 { 105 Default: types.NewSuggestion(60), 106 Name: "step", 107 Type: types.Integer, 108 }, 109 }, 110 }, 111 } 112 }