-
Notifications
You must be signed in to change notification settings - Fork 140
/
function.go
79 lines (67 loc) · 2.23 KB
/
function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package identity
import (
"context"
"github.com/go-graphite/carbonapi/expr/interfaces"
"github.com/go-graphite/carbonapi/expr/types"
"github.com/go-graphite/carbonapi/pkg/parser"
pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
)
type identity struct {
interfaces.FunctionBase
}
func GetOrder() interfaces.Order {
return interfaces.Any
}
func New(configFile string) []interfaces.FunctionMetadata {
res := make([]interfaces.FunctionMetadata, 0)
f := &identity{}
functions := []string{"identity"}
for _, n := range functions {
res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
}
return res
}
// identity(name)
func (f *identity) Do(ctx context.Context, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
name, err := e.GetStringArg(0)
if err != nil {
return nil, err
}
step := int64(60)
newValues := make([]float64, (until-from-1+step)/step)
value := from
for i := 0; i < len(newValues); i++ {
newValues[i] = float64(value)
value += step
}
p := types.MetricData{
FetchResponse: pb.FetchResponse{
Name: "identity(" + name + ")",
StartTime: from,
StopTime: until,
StepTime: step,
Values: newValues,
},
Tags: map[string]string{"name": name},
}
return []*types.MetricData{&p}, nil
}
// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *identity) Description() map[string]types.FunctionDescription {
return map[string]types.FunctionDescription{
"identity": {
Description: "Identity function: Returns datapoints where the value equals the timestamp of the datapoint.\n Useful when you have another series where the value is a timestamp, and you want to compare it to the time of the datapoint, to render an age\n\nExample:\n\n.. code-block:: none\n\n &target=identity(\"The.time.series\")\n This would create a series named “The.time.series” that contains points where x(t) == t.)",
Function: "identity(name)",
Group: "Calculate",
Module: "graphite.render.functions",
Name: "identity",
Params: []types.FunctionParam{
{
Name: "name",
Required: true,
Type: types.String,
},
},
},
}
}