-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_query.go
300 lines (281 loc) · 9.32 KB
/
cmd_query.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
package main
import (
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
"github.com/spf13/pflag"
)
var (
flagsQuery *pflag.FlagSet
flagQueryText string
flagQueryFile string
flagQueryInputs []string
flagQueryJSON bool
flagQueryCSV bool
flagQueryStartTime string
flagQueryEndTime string
flagQueryRelative time.Duration
flagQueryColWidth int
flagQueryExtended bool
flagQueryLiteralStrings bool
flagQueryFormat string
)
func init() {
flagsQuery = pflag.NewFlagSet("query", pflag.ContinueOnError)
flagsQuery.StringVarP(&flagQueryText, "query", "q", "", "OPAL query text")
flagsQuery.StringVarP(&flagQueryFile, "file", "f", "", "file containing OPAL query text")
flagsQuery.StringSliceVarP(&flagQueryInputs, "input", "i", nil, "input datasets: ID or workspace.name")
flagsQuery.BoolVarP(&flagQueryJSON, "json", "j", false, "output in nd-JSON format")
flagsQuery.Lookup("json").NoOptDefVal = "true"
flagsQuery.BoolVarP(&flagQueryCSV, "csv", "c", false, "output in CSV format")
flagsQuery.Lookup("csv").NoOptDefVal = "true"
flagsQuery.StringVarP(&flagQueryStartTime, "start-time", "s", "", "start time of query window")
flagsQuery.StringVarP(&flagQueryEndTime, "end-time", "e", "", "end time of query window")
flagsQuery.DurationVarP(&flagQueryRelative, "relative", "r", 0, "duration of query window, anchored at either end")
flagsQuery.IntVarP(&flagQueryColWidth, "col-width", "w", 64, "maximum column width for table format; 0 for unlimited")
flagsQuery.BoolVarP(&flagQueryExtended, "extended", "x", false, "print one column value per row rather than table format")
flagsQuery.Lookup("extended").NoOptDefVal = "true"
flagsQuery.BoolVarP(&flagQueryLiteralStrings, "literal-strings", "l", false, "print embedded control characters literally")
flagsQuery.Lookup("literal-strings").NoOptDefVal = "true"
flagsQuery.StringVar(&flagQueryFormat, "format", "", "specify output format: table, extended, csv, ndjson")
RegisterCommand(&Command{
Name: "query",
Help: "Run an OPAL query.",
Flags: flagsQuery,
Func: cmdQuery,
})
}
const DefaultQueryWindowDuration = time.Hour
const MaxQueryTextLength = 100000
var ErrNeedQueryOrFile = ObserveError{Msg: "need one of --query and --file for the query text"}
var ErrOnlyOneQueryOrFile = ObserveError{Msg: "only one of --query and --file may be specified"}
var ErrAtMostTwoTimeSpecifiers = ObserveError{Msg: "at most two of --start-time, --end-time, and --relative may be specified"}
var ErrValidToMustBeAfterValidFrom = ObserveError{Msg: "--end-time time must be after --start-time time"}
var ErrTooLongQueryText = ObserveError{Msg: fmt.Sprintf("the query text is longer than %d characters", MaxQueryTextLength)}
var ErrAtMostOneOutputFormat = ObserveError{Msg: "at most one of --csv and --json may be specified"}
var ErrAnInputIsRequired = ObserveError{Msg: "at least one --input is required"}
var ErrQueryTooManyFormats = ObserveError{Msg: "at most one of --format and the format-specific flags may be specified"}
var ErrUnknownFormat = ObserveError{Msg: "the --format is not known"}
func cmdQuery(fa FuncArgs) error {
nowTime := time.Now().Truncate(time.Second)
// parse query arguments
nText := CountFlags(flagsQuery, "query", "file")
var queryText string
var err error
switch nText {
default:
return ErrOnlyOneQueryOrFile
case 0:
return ErrNeedQueryOrFile
case 1:
// I have to grudgingly accept this
if flagsQuery.Lookup("file").Changed {
queryText, err = LoadQueryTextFromFile(fa.fs, flagQueryFile)
} else {
queryText = flagQueryText
}
}
if err != nil {
return err
}
if len(queryText) > MaxQueryTextLength {
return ErrTooLongQueryText
}
var fromTime, toTime time.Time
nTime := CountFlags(flagsQuery, "start-time", "end-time", "relative")
switch nTime {
case 0:
toTime = nowTime.Add(-15 * time.Second).Truncate(time.Minute)
fromTime = toTime.Add(-DefaultQueryWindowDuration)
case 1:
if flagsQuery.Lookup("start-time").Changed {
fromTime, err = ParseTime(flagQueryStartTime, nowTime)
toTime = fromTime.Add(DefaultQueryWindowDuration)
} else if flagsQuery.Lookup("end-time").Changed {
toTime, err = ParseTime(flagQueryEndTime, nowTime)
fromTime = toTime.Add(-DefaultQueryWindowDuration)
} else {
toTime = nowTime.Add(-15 * time.Second).Truncate(time.Minute)
fromTime = toTime.Add(-flagQueryRelative)
}
case 2:
if !flagsQuery.Lookup("start-time").Changed {
toTime, err = ParseTime(flagQueryEndTime, nowTime)
fromTime = toTime.Add(-flagQueryRelative)
} else if !flagsQuery.Lookup("end-time").Changed {
fromTime, err = ParseTime(flagQueryStartTime, nowTime)
toTime = fromTime.Add(flagQueryRelative)
} else {
fromTime, err = ParseTime(flagQueryStartTime, nowTime)
if err == nil {
toTime, err = ParseTime(flagQueryEndTime, nowTime)
}
}
default:
return ErrAtMostTwoTimeSpecifiers
}
if err != nil {
return NewObserveError(err, "bad time format")
}
if toTime.Sub(fromTime) <= 0 {
return ErrValidToMustBeAfterValidFrom
}
nFmt := CountFlags(flagsQuery, "csv", "json")
switch nFmt {
case 0:
// format is "table text"
case 1:
// format is specified
default:
return ErrAtMostOneOutputFormat
}
// TODO: we can remove this when in-text inputs are complete
if len(flagQueryInputs) == 0 {
return ErrAnInputIsRequired
}
var inputs []StageQueryInput
for i, in := range flagQueryInputs {
pieces := strings.SplitN(in, "=", 2)
if len(pieces) == 1 {
if i == 0 {
pieces = append([]string{"_"}, pieces[0])
} else {
return NewObserveError(nil, "input at index %d must be of the form id=dataset", i)
}
}
for j, k := range inputs {
if k.InputName == pieces[0] {
return NewObserveError(nil, "input at index %d duplicates input name %q from index %d", j, k.InputName, i)
}
}
var i64 int64
if i64, err = strconv.ParseInt(pieces[1], 10, 64); err == nil {
inputs = append(inputs, StageQueryInput{
InputName: pieces[0],
DatasetID: &i64,
})
fa.op.Debug("input[%d] @%s <- datasetId(%d)\n", i, pieces[0], i64)
} else {
if !strings.Contains(pieces[1], ".") {
workspaceName := mustGetWorkspaceName(fa.cfg, fa.hc)
fa.op.Debug("default workspace=%s\n", workspaceName)
pieces[1] = workspaceName + "." + pieces[1]
}
inputs = append(inputs, StageQueryInput{
InputName: pieces[0],
DatasetPath: &pieces[1],
})
fa.op.Debug("input[%d] @%s <- datasetPath(%q)\n", i, pieces[0], pieces[1])
}
}
// I'm now ready to formulate the query
noLinkify := false
req := V1ExportQueryRequest{
Query: OpalQuery{
OutputStage: "query",
Stages: []StageQuery{
{
Inputs: inputs,
StageID: "query",
Pipeline: queryText,
},
},
},
Presentation: &Presentation{
Linkify: &noLinkify,
},
}
nfmts := 0
if flagQueryFormat != "" {
fa.op.Debug("formatFlag=%s\n", flagQueryFormat)
nfmts++
}
if flagQueryJSON {
fa.op.Debug("format=JSON\n")
nfmts++
}
if flagQueryCSV {
fa.op.Debug("format=CSV\n")
nfmts++
}
if flagQueryExtended {
fa.op.Debug("format=Extended\n")
nfmts++
}
if nfmts > 1 {
fa.op.Debug("too many formats: %d\n", nfmts)
return ErrQueryTooManyFormats
}
// I know that if flagQueryFormat is non-empty, none of the format flags are specified
switch flagQueryFormat {
case "json", "JSON", "ndjson", "NDJSON", "nd-json", "ND-JSON":
flagQueryJSON = true
case "csv", "CSV":
flagQueryCSV = true
case "extended":
flagQueryExtended = true
case "":
// don't change what's configured
default:
return ErrUnknownFormat
}
var output io.Writer
acceptHeader := "text/csv"
switch {
case flagQueryJSON:
output = fa.op
acceptHeader = "application/x-ndjson"
case flagQueryCSV:
output = fa.op
default:
// text format
tfmt := &CSVParsingColumnFormatter{ColumnFormatter: ColumnFormatter{Output: fa.op, ColWidth: flagQueryColWidth, ExtendedFormat: flagQueryExtended, LiteralStrings: flagQueryLiteralStrings}}
defer tfmt.Close()
output = tfmt
}
uri := fmt.Sprintf("/v1/meta/export/query?startTime=%s&endTime=%s",
url.QueryEscape(fromTime.Format(time.RFC3339)),
url.QueryEscape(toTime.Format(time.RFC3339)))
err, _ = RequestPOSTWithBodyOutput(fa.cfg, fa.op, fa.hc, uri, &req, headers("Accept", acceptHeader, "Authorization", fa.cfg.AuthHeader()), output)
if err != nil {
return err
}
return nil
}
type OpalQuery struct {
OutputStage string `json:"outputStage"`
// The API wants this marshaled as an array, but we only do one stage,
// because inline stages are now a thing.
Stages []StageQuery `json:"stages"`
// no parameters
// no parameterValues
// no layout
}
type StageQuery struct {
// The API wants this, but once inline inputs are complete, we may be able
// to remove them. Might want to support them still, though.
Inputs []StageQueryInput `json:"input"`
StageID string `json:"stageID"`
Pipeline string `json:"pipeline"`
// no layout
// no parameters
// no parameterValues
// no stageIndex
}
type StageQueryInput struct {
InputName string `json:"inputName"`
DatasetID *int64 `json:"datasetId,string,omitempty"`
DatasetPath *string `json:"datasetPath,omitempty"`
}
type Presentation struct {
Limit *int64 `json:"limit,omitempty"`
Linkify *bool `json:"linkify,omitempty"`
}
type V1ExportQueryRequest struct {
Query OpalQuery `json:"query"`
// no rowCount
Presentation *Presentation `json:"presentation,omitempty"`
}