-
Notifications
You must be signed in to change notification settings - Fork 152
/
to.go
326 lines (287 loc) · 9.4 KB
/
to.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package experimental
import (
"context"
"fmt"
"sort"
"time"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/stdlib/influxdata/influxdb"
"github.com/influxdata/flux/values"
lp "github.com/influxdata/line-protocol"
)
const ToKind = "experimental-to"
func init() {
toSignature := runtime.MustLookupBuiltinType("experimental", "to")
runtime.RegisterPackageValue("experimental", "to", flux.MustValue(flux.FunctionValueWithSideEffect("to", createToOpSpec, toSignature)))
plan.RegisterProcedureSpecWithSideEffect(ToKind, newToProcedure, ToKind)
execute.RegisterTransformation(ToKind, createToTransformation)
}
// ToOpSpec is the flux.OperationSpec for the `to` flux function.
type ToOpSpec struct {
Org influxdb.NameOrID
Bucket influxdb.NameOrID
Host string
Token string
}
// ReadArgs reads the args from flux.Arguments into the op spec
func (s *ToOpSpec) ReadArgs(args flux.Arguments) error {
if b, ok, err := influxdb.GetNameOrID(args, "bucket", "bucketID"); err != nil {
return err
} else if !ok {
return errors.New(codes.Invalid, "must specify bucket or bucketID")
} else {
s.Bucket = b
}
if o, ok, err := influxdb.GetNameOrID(args, "org", "orgID"); err != nil {
return err
} else if ok {
s.Org = o
}
if host, ok, err := args.GetString("host"); err != nil {
return err
} else if ok {
s.Host = host
}
if token, ok, err := args.GetString("token"); err != nil {
return err
} else if ok {
s.Token = token
}
return nil
}
func createToOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
s := &ToOpSpec{}
if err := s.ReadArgs(args); err != nil {
return nil, err
}
return s, nil
}
// Kind returns the kind for the ToOpSpec function.
func (ToOpSpec) Kind() flux.OperationKind {
return ToKind
}
// ToProcedureSpec is the procedure spec for the `to` flux function.
type ToProcedureSpec struct {
plan.DefaultCost
Config influxdb.Config
}
// Kind returns the kind for the procedure spec for the `to` flux function.
func (o *ToProcedureSpec) Kind() plan.ProcedureKind {
return ToKind
}
// Copy clones the procedure spec for `to` flux function.
func (o *ToProcedureSpec) Copy() plan.ProcedureSpec {
ns := *o
return &ns
}
func newToProcedure(qs flux.OperationSpec, a plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*ToOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &ToProcedureSpec{
Config: influxdb.Config{
Org: spec.Org,
Bucket: spec.Bucket,
Host: spec.Host,
Token: spec.Token,
},
}, nil
}
func createToTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*ToProcedureSpec)
if !ok {
return nil, nil, fmt.Errorf("invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t, err := NewToTransformation(a.Context(), d, cache, s)
if err != nil {
return nil, nil, err
}
return t, d, nil
}
// ToTransformation is the transformation for the `to` flux function.
type ToTransformation struct {
execute.ExecutionNode
ctx context.Context
d execute.Dataset
cache execute.TableBuilderCache
writer influxdb.Writer
}
// RetractTable retracts the table for the transformation for the `to` flux function.
func (t *ToTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
// NewToTransformation returns a new *ToTransformation with the appropriate fields set.
func NewToTransformation(ctx context.Context, d execute.Dataset, cache execute.TableBuilderCache, s *ToProcedureSpec) (*ToTransformation, error) {
provider := influxdb.GetProvider(ctx)
writer, err := provider.WriterFor(ctx, s.Config)
if err != nil {
return nil, err
}
return &ToTransformation{
ctx: ctx,
d: d,
cache: cache,
writer: writer,
}, nil
}
// Process does the actual work for the ToTransformation.
func (t *ToTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
return t.writeTable(tbl)
}
// UpdateWatermark updates the watermark for the transformation for the `to` flux function.
func (t *ToTransformation) UpdateWatermark(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateWatermark(pt)
}
// UpdateProcessingTime updates the processing time for the transformation for the `to` flux function.
func (t *ToTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
// Finish is called after the `to` flux function's transformation is done processing.
func (t *ToTransformation) Finish(id execute.DatasetID, err error) {
writeErr := t.writer.Close()
if err == nil {
err = writeErr
}
t.d.Finish(err)
}
const (
defaultFieldColLabel = influxdb.DefaultFieldColLabel
defaultMeasurementColLabel = influxdb.DefaultMeasurementColLabel
defaultTimeColLabel = execute.DefaultTimeColLabel
defaultStartColLabel = execute.DefaultStartColLabel
defaultStopColLabel = execute.DefaultStopColLabel
)
type LabelAndOffset struct {
Label string
Offset int
}
// tablePointsMetadata stores state needed to write the points from one table.
type tablePointsMetadata struct {
// Name is the measurement name for this table.
Name string
// Tags holds the tags in the table excluding the measurement.
Tags []*lp.Tag
// The column offset in the input table where the _time column is stored
TimestampOffset int
// The labels and offsets of all the fields in the table
Fields []LabelAndOffset
}
func getTablePointsMetadata(tbl flux.Table) (md tablePointsMetadata, err error) {
// Find measurement, tags
foundMeasurement := false
md.Tags = make([]*lp.Tag, 0, len(tbl.Key().Cols()))
isTag := make(map[string]bool)
for j, col := range tbl.Key().Cols() {
switch col.Label {
case defaultStartColLabel:
continue
case defaultStopColLabel:
continue
case defaultFieldColLabel:
return md, errors.Newf(codes.FailedPrecondition, "found column %q in the group key; experimental.to() expects pivoted data", col.Label)
case defaultMeasurementColLabel:
foundMeasurement = true
if col.Type != flux.TString {
return md, errors.Newf(codes.FailedPrecondition, "group key column %q has type %v; type %v is required", col.Label, col.Type, flux.TString)
}
md.Name = tbl.Key().ValueString(j)
default:
if col.Type != flux.TString {
return md, errors.Newf(codes.FailedPrecondition, "group key column %q has type %v; type %v is required", col.Label, col.Type, flux.TString)
}
isTag[col.Label] = true
md.Tags = append(md.Tags, &lp.Tag{
Key: col.Label,
Value: tbl.Key().ValueString(j),
})
}
}
sort.SliceStable(md.Tags, func(i, j int) bool {
return md.Tags[i].Key < md.Tags[j].Key
})
if !foundMeasurement {
return md, errors.Newf(codes.FailedPrecondition, "required column %q not in group key", defaultMeasurementColLabel)
}
// Find the time column as it is required.
md.TimestampOffset = execute.ColIdx(defaultTimeColLabel, tbl.Cols())
if md.TimestampOffset < 0 {
return md, errors.Newf(codes.FailedPrecondition, "input table is missing required column %q", defaultTimeColLabel)
} else if col := tbl.Cols()[md.TimestampOffset]; col.Type != flux.TTime {
return md, errors.Newf(codes.FailedPrecondition, "column %q has type %s; type %s is required", defaultTimeColLabel, col.Type, flux.TTime)
}
// Loop over all of the remaining columns to find the fields.
// By this point, we know all of the tags and we can exclude the time
// column from the list of fields so we can allocate an appropriate size.
md.Fields = make([]LabelAndOffset, 0, len(tbl.Cols())-len(md.Tags)-1)
for j, col := range tbl.Cols() {
switch col.Label {
case defaultStartColLabel, defaultStopColLabel, defaultMeasurementColLabel, defaultTimeColLabel:
continue
default:
if !isTag[col.Label] {
md.Fields = append(md.Fields, LabelAndOffset{
Label: col.Label,
Offset: j,
})
}
}
}
return md, nil
}
func (t *ToTransformation) writeTable(tbl flux.Table) error {
builder, new := t.cache.TableBuilder(tbl.Key())
if new {
if err := execute.AddTableCols(tbl, builder); err != nil {
return err
}
}
tmd, err := getTablePointsMetadata(tbl)
if err != nil {
return err
}
return tbl.Do(func(cr flux.ColReader) error {
if cr.Len() == 0 {
// Nothing to do
return nil
}
metrics := make([]lp.Metric, 0, cr.Len())
for i := 0; i < cr.Len(); i++ {
timestamp := cr.Times(tmd.TimestampOffset).Value(i)
metric := &influxdb.RowMetric{
NameStr: tmd.Name,
TS: time.Unix(0, timestamp),
Tags: tmd.Tags,
Fields: make([]*lp.Field, 0, len(tmd.Fields)),
}
for _, lao := range tmd.Fields {
fieldVal := execute.ValueForRow(cr, i, lao.Offset)
// Skip this iteration if field value is null
if fieldVal.IsNull() {
continue
}
metric.Fields = append(metric.Fields, &lp.Field{
Key: lao.Label,
Value: values.Unwrap(fieldVal),
})
}
if len(metric.Fields) > 0 {
metrics = append(metrics, metric)
}
if err := execute.AppendRecord(i, cr, builder); err != nil {
return err
}
}
return t.writer.Write(metrics...)
})
}