-
Notifications
You must be signed in to change notification settings - Fork 153
/
hour_selection.go
204 lines (173 loc) · 5.78 KB
/
hour_selection.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package universe
import (
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/date"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
)
const HourSelectionKind = "_hourSelection"
type HourSelectionOpSpec struct {
Start int64 `json:"start"`
Stop int64 `json:"stop"`
Location string `json:"location"`
Offset values.Duration `json:"offset"`
TimeColumn string `json:"timeColumn"`
}
func init() {
hourSelectionSignature := runtime.MustLookupBuiltinType("universe", "_hourSelection")
runtime.RegisterPackageValue("universe", HourSelectionKind, flux.MustValue(flux.FunctionValue(HourSelectionKind, createHourSelectionOpSpec, hourSelectionSignature)))
plan.RegisterProcedureSpec(HourSelectionKind, newHourSelectionProcedure, HourSelectionKind)
execute.RegisterTransformation(HourSelectionKind, createHourSelectionTransformation)
}
func createHourSelectionOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(HourSelectionOpSpec)
start, err := args.GetRequiredInt("start")
if err != nil {
return nil, err
}
spec.Start = start
stop, err := args.GetRequiredInt("stop")
if err != nil {
return nil, err
}
spec.Stop = stop
location, offset, err := date.GetLocationFromFluxArgs(args)
if err != nil {
return nil, err
}
spec.Location = location
spec.Offset = offset
if label, ok, err := args.GetString("timeColumn"); err != nil {
return nil, err
} else if ok {
spec.TimeColumn = label
} else {
spec.TimeColumn = execute.DefaultTimeColLabel
}
return spec, nil
}
func (s *HourSelectionOpSpec) Kind() flux.OperationKind {
return HourSelectionKind
}
type HourSelectionProcedureSpec struct {
plan.DefaultCost
Start int64 `json:"start"`
Stop int64 `json:"stop"`
Location string `json:"location"`
Offset values.Duration `json:"offset"`
TimeColumn string `json:"timeColumn"`
}
func newHourSelectionProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*HourSelectionOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &HourSelectionProcedureSpec{
Start: spec.Start,
Stop: spec.Stop,
Location: spec.Location,
Offset: spec.Offset,
TimeColumn: spec.TimeColumn,
}, nil
}
func (s *HourSelectionProcedureSpec) Kind() plan.ProcedureKind {
return HourSelectionKind
}
func (s *HourSelectionProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(HourSelectionProcedureSpec)
*ns = *s
return ns
}
func (s *HourSelectionProcedureSpec) TriggerSpec() plan.TriggerSpec {
return plan.NarrowTransformationTriggerSpec{}
}
func createHourSelectionTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*HourSelectionProcedureSpec)
if !ok {
return nil, nil, errors.Newf(codes.Internal, "invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t := NewHourSelectionTransformation(d, cache, s)
return t, d, nil
}
type hourSelectionTransformation struct {
execute.ExecutionNode
d execute.Dataset
cache execute.TableBuilderCache
start int64
stop int64
location string
offset values.Duration
timeCol string
}
func NewHourSelectionTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *HourSelectionProcedureSpec) *hourSelectionTransformation {
return &hourSelectionTransformation{
d: d,
cache: cache,
start: spec.Start,
stop: spec.Stop,
location: spec.Location,
offset: spec.Offset,
timeCol: spec.TimeColumn,
}
}
func (t *hourSelectionTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
func (t *hourSelectionTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
builder, created := t.cache.TableBuilder(tbl.Key())
if !created {
return errors.Newf(codes.FailedPrecondition, "hour selection found duplicate table with key: %v", tbl.Key())
}
if err := execute.AddTableCols(tbl, builder); err != nil {
return err
}
colIdx := execute.ColIdx(t.timeCol, tbl.Cols())
if colIdx < 0 {
return errors.Newf(codes.FailedPrecondition, "invalid time column")
}
if t.start < 0 || t.start > 23 {
return errors.Newf(codes.Invalid, "start must be between 0 and 23")
}
if t.stop < 0 || t.stop > 23 {
return errors.Newf(codes.Invalid, "stop must be between 0 and 23")
}
return tbl.Do(func(cr flux.ColReader) error {
l := cr.Len()
for i := 0; i < l; i++ {
if nullCheck := cr.Times(colIdx); nullCheck.IsNull(i) {
continue
}
lTime, err := date.GetTimeInLocation(execute.Time(cr.Times(colIdx).Value(i)), t.location, t.offset)
if err != nil {
return nil
}
lHour := int64(lTime.Time().Time().Hour())
if (lHour >= t.start && lHour <= t.stop) || (t.start > t.stop && (lHour >= t.start || lHour <= t.stop)) {
for k := range cr.Cols() {
if err := builder.AppendValue(k, execute.ValueForRow(cr, i, k)); err != nil {
return err
}
}
}
}
return nil
})
}
func (t *hourSelectionTransformation) UpdateWatermark(id execute.DatasetID, mark execute.Time) error {
return t.d.UpdateWatermark(mark)
}
func (t *hourSelectionTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *hourSelectionTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}