-
Notifications
You must be signed in to change notification settings - Fork 152
/
from_influx_json.go
196 lines (164 loc) · 4.63 KB
/
from_influx_json.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
package v1
import (
"bufio"
"context"
stderrors "errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/influxql"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
)
const FromInfluxJSONKind = "fromInfluxJSON"
const bufferSize = 8192
func init() {
fromInfluxJSONSignature := runtime.MustLookupBuiltinType("influxdata/influxdb/v1", "json")
runtime.RegisterPackageValue("influxdata/influxdb/v1", "json", flux.MustValue(flux.FunctionValue(FromInfluxJSONKind, createFromInfluxJSONOpSpec, fromInfluxJSONSignature)))
flux.RegisterOpSpec(FromInfluxJSONKind, newFromInfluxJSONOp)
plan.RegisterProcedureSpec(FromInfluxJSONKind, newFromInfluxJSONProcedure, FromInfluxJSONKind)
execute.RegisterSource(FromInfluxJSONKind, createFromInfluxJSONSource)
}
func createFromInfluxJSONOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
var spec = new(FromInfluxJSONOpSpec)
if json, ok, err := args.GetString("json"); err != nil {
return nil, err
} else if ok {
spec.JSON = json
}
if file, ok, err := args.GetString("file"); err != nil {
return nil, err
} else if ok {
spec.File = file
}
if spec.JSON == "" && spec.File == "" {
return nil, stderrors.New("must provide json raw text or filename")
}
if spec.JSON != "" && spec.File != "" {
return nil, stderrors.New("must provide exactly one of the parameters json or file")
}
if spec.File != "" {
if _, err := os.Stat(spec.File); err != nil {
return nil, errors.Wrapf(err, codes.Inherit, "failed to stat json file: %s", spec.File)
}
}
return spec, nil
}
// FromInfluxJSONOpSpec defines the `fromInfluxJSON` function signature
type FromInfluxJSONOpSpec struct {
JSON string `json:"json"`
File string `json:"file"`
}
func newFromInfluxJSONOp() flux.OperationSpec {
return new(FromInfluxJSONOpSpec)
}
func (s *FromInfluxJSONOpSpec) Kind() flux.OperationKind {
return FromInfluxJSONKind
}
// FromInfluxJSONProcedureSpec describes the `fromInfluxJSON` prodecure
type FromInfluxJSONProcedureSpec struct {
plan.DefaultCost
JSON string
File string
}
func newFromInfluxJSONProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*FromInfluxJSONOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
return &FromInfluxJSONProcedureSpec{
JSON: spec.JSON,
File: spec.File,
}, nil
}
func (s *FromInfluxJSONProcedureSpec) Kind() plan.ProcedureKind {
return FromInfluxJSONKind
}
func (s *FromInfluxJSONProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(FromInfluxJSONProcedureSpec)
ns.JSON = s.JSON
ns.File = s.File
return ns
}
func createFromInfluxJSONSource(prSpec plan.ProcedureSpec, dsid execute.DatasetID, a execute.Administration) (execute.Source, error) {
spec, ok := prSpec.(*FromInfluxJSONProcedureSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", prSpec)
}
var jsonReader io.Reader
deps := flux.GetDependencies(a.Context())
fs, err := deps.FilesystemService()
if err != nil {
return nil, err
}
if spec.File != "" {
f, err := fs.Open(spec.File)
if err != nil {
return nil, err
}
jsonReader = bufio.NewReaderSize(f, bufferSize)
} else {
jsonReader = strings.NewReader(spec.JSON)
}
decoder := influxql.NewResultDecoder(a.Allocator())
results, err := decoder.Decode(ioutil.NopCloser(jsonReader))
if err != nil {
return nil, err
}
return &JSONSource{id: dsid, results: results}, nil
}
type JSONSource struct {
results flux.ResultIterator
id execute.DatasetID
ts []execute.Transformation
}
func (c *JSONSource) AddTransformation(t execute.Transformation) {
c.ts = append(c.ts, t)
}
func (c *JSONSource) Run(ctx context.Context) {
var err error
var max execute.Time
var maxSet bool
err = c.results.Next().Tables().Do(func(tbl flux.Table) error {
for _, t := range c.ts {
err := t.Process(c.id, tbl)
if err != nil {
return err
}
if idx := execute.ColIdx(execute.DefaultStopColLabel, tbl.Key().Cols()); idx >= 0 {
if stop := tbl.Key().ValueTime(idx); !maxSet || stop > max {
max = stop
maxSet = true
}
}
}
return nil
})
if err != nil {
goto FINISH
}
if maxSet {
for _, t := range c.ts {
err = t.UpdateWatermark(c.id, max)
if err != nil {
goto FINISH
}
}
}
if c.results.More() {
// It doesn't make sense to read multiple results
if err != nil {
err = errors.Wrap(err, codes.Inherit, "'fromInfluxJSON' supports only single results")
}
}
FINISH:
for _, t := range c.ts {
t.Finish(c.id, err)
}
}