-
Notifications
You must be signed in to change notification settings - Fork 153
/
resets.go
167 lines (137 loc) · 4.24 KB
/
resets.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package promql
import (
"fmt"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
)
const ResetsKind = "resets"
type ResetsOpSpec struct{}
func init() {
resetsSignature := runtime.MustLookupBuiltinType("internal/promql", ResetsKind)
runtime.RegisterPackageValue("internal/promql", ResetsKind, flux.MustValue(flux.FunctionValue(ResetsKind, createResetsOpSpec, resetsSignature)))
flux.RegisterOpSpec(ResetsKind, newResetsOp)
plan.RegisterProcedureSpec(ResetsKind, newResetsProcedure, ResetsKind)
execute.RegisterTransformation(ResetsKind, createResetsTransformation)
}
func createResetsOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
return new(ResetsOpSpec), nil
}
func newResetsOp() flux.OperationSpec {
return new(ResetsOpSpec)
}
func (s *ResetsOpSpec) Kind() flux.OperationKind {
return ResetsKind
}
type ResetsProcedureSpec struct {
plan.DefaultCost
}
func newResetsProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
_, ok := qs.(*ResetsOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
return new(ResetsProcedureSpec), nil
}
func (s *ResetsProcedureSpec) Kind() plan.ProcedureKind {
return ResetsKind
}
func (s *ResetsProcedureSpec) Copy() plan.ProcedureSpec {
return new(ResetsProcedureSpec)
}
// TriggerSpec implements plan.TriggerAwareProcedureSpec
func (s *ResetsProcedureSpec) TriggerSpec() plan.TriggerSpec {
return plan.NarrowTransformationTriggerSpec{}
}
func createResetsTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*ResetsProcedureSpec)
if !ok {
return nil, nil, fmt.Errorf("invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t := NewResetsTransformation(d, cache, s)
return t, d, nil
}
type resetsTransformation struct {
execute.ExecutionNode
d execute.Dataset
cache execute.TableBuilderCache
}
func NewResetsTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *ResetsProcedureSpec) *resetsTransformation {
return &resetsTransformation{
d: d,
cache: cache,
}
}
func (t *resetsTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
func (t *resetsTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
// TODO: Check that all columns are part of the key, except _value and _time.
key := tbl.Key()
builder, created := t.cache.TableBuilder(key)
if !created {
return fmt.Errorf("resets found duplicate table with key: %v", tbl.Key())
}
if err := execute.AddTableKeyCols(key, builder); err != nil {
return err
}
cols := tbl.Cols()
valIdx := execute.ColIdx(execute.DefaultValueColLabel, cols)
if valIdx < 0 {
return fmt.Errorf("value column not found (cols: %v): %s", cols, execute.DefaultValueColLabel)
}
var (
numVals int
resets int
prev float64
)
err := tbl.Do(func(cr flux.ColReader) error {
vs := cr.Floats(valIdx)
for i := 0; i < cr.Len(); i++ {
if !vs.IsValid(i) {
continue
}
numVals++
if numVals == 1 {
prev = vs.Value(i)
continue
}
current := vs.Value(i)
if current < prev {
resets++
}
prev = current
}
return nil
})
if err != nil {
return err
}
// No output for empty input range vectors.
if numVals < 1 {
return nil
}
outValIdx, err := builder.AddCol(flux.ColMeta{Label: execute.DefaultValueColLabel, Type: flux.TFloat})
if err != nil {
return fmt.Errorf("error appending value column: %s", err)
}
if err := builder.AppendFloat(outValIdx, float64(resets)); err != nil {
return err
}
return execute.AppendKeyValues(key, builder)
}
func (t *resetsTransformation) UpdateWatermark(id execute.DatasetID, mark execute.Time) error {
return t.d.UpdateWatermark(mark)
}
func (t *resetsTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *resetsTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}