This repository has been archived by the owner on Nov 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tsq.lua
687 lines (629 loc) · 17.6 KB
/
tsq.lua
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
--[[
-- An abstraction to the InfluxDB Query language. Specifically only SELECT statements.
--
-- InfluxDB is quiet picky about placement of spaces and quotes. So this attempts
-- to programically handle that by abstrating things a bit. There will be some
-- queries that won't work from here, but most should.
--
-- Because this is abstracted away from string concatenation a bit, it can also do
-- some parameter validation.
--
--]]
------------------------------------------------------------------------------
TSQ = {}
TSQ.__index = TSQ
TSQ._sel = '*'
TSQ._from = '*'
--[[ Quoting Rules:
-- https://docs.influxdata.com/influxdb/v0.13/troubleshooting/frequently_encountered_issues/#single-quoting-and-double-quoting-in-queries
--
-- Single quote string values (for example, tag values) but do not single quote
-- identifiers (database names, retention policy names, user names, measurement
-- names, tag keys, and field keys).
--
-- Double quote identifiers if they start with a digit, contain characters
-- other than [A-z,0-9,_], or if they are an InfluxQL keyword. You can double
-- quote identifiers even if they don't fall into one of those categories but
-- it isn't necessary.
--
-- Examples:
-- Yes: SELECT bikes_available FROM bikes WHERE station_id='9'
-- Yes: SELECT "bikes_available" FROM "bikes" WHERE "station_id"='9'
-- Yes: SELECT * from "cr@zy" where "p^e"='2'
-- No: SELECT 'bikes_available' FROM 'bikes' WHERE 'station_id'="9"
-- No: SELECT * from cr@zy where p^e='2'
--
-- Single quote date time strings. InfluxDB returns an error (ERR: invalid operation: time and *influxql.VarRef are not compatible) if you double quote a date time string.
--
-- Examples:
-- Yes: SELECT water_level FROM h2o_feet WHERE time > '2015-08-18T23:00:01.232000000Z' AND time < '2015-09-19'
-- No: SELECT water_level FROM h2o_feet WHERE time > "2015-08-18T23:00:01.232000000Z" AND time < "2015-09-19"
--
--]]
function TSQ.q()
local ts = {}
setmetatable(ts, TSQ)
return ts
end
function TSQ.func_field_quoter(str)
local fn, pms = string.match(str, "^(%a+)(%b())$")
if fn == nil then
-- Not a function, check for quotes.
if string.sub(str, 1, 1) == '"' and string.sub(str, -1, -1) == '"' then
return str
else
return string.format('%q', str)
end
else
-- Is a function, check params and return
local prm = string.sub(pms, 2, -2) -- remove parens
-- If there is a second param, it is a number or duration.
-- Only the first param could be a function/tag/field.
local _, cidx = string.find(prm, ".*,") -- want the last comma
local rebuilt = ""
if cidx ~= nil then
rebuilt = TSQ.func_field_quoter(string.sub(prm, 1, cidx-1))
rebuilt = rebuilt .. string.sub(prm, cidx)
else
rebuilt = TSQ.func_field_quoter(prm)
end
return fn .. '(' .. rebuilt .. ')'
end
end
function TSQ:fields(...)
if type(self._sel) ~= "table" then
local ft = {}
local ftm = {}
ftm.__tostring = function(v)
local w = {}
for i,v in ipairs(v) do
w[#w + 1] = tostring(v)
end
return table.concat(w, ',')
end
setmetatable(ft, ftm)
self._sel = ft
end
local tbl = {}
if select('#', ...) == 1 then
local v = select(1, ...)
if type(v) == "table" then
if v._field ~= nil then
-- if this is a TSF object, store it in a table.
tbl = {v}
else
-- Otherwise it is an array of something.
tbl = v
end
else
tbl[1] = tostring(v)
end
else
tbl = table.pack(...)
end
for i,v in ipairs(tbl) do -- FIXME Needs to be smarter since can be expr.
local vs = ''
if v._field ~= nil then
-- This is a TSF object.
vs = v
else
-- This is might be just a field name, or an old formatted func-field
vs = self.func_field_quoter(tostring(v))
end
self._sel[#self._sel + 1] = vs
end
return self
end
TSF = {}
TSF.__index = TSF
function TSF.new(field)
return setmetatable({_field = field},TSF)
end
function TSF:as(name)
self._as = name
return self
end
local select_single_functions = {'count', 'distinct', 'mean', 'median', 'spread',
'sum', 'first', 'last', 'max', 'min', 'difference', 'stddev'}
for i,v in ipairs(select_single_functions) do
TSF[v] = function(me)
me._func = string.upper(v)
return me
end
end
local select_number_functions = {'bottom', 'percentile', 'top', 'moving_average'}
for i,v in ipairs(select_number_functions) do
TSF[v] = function(me, num)
if type(num) ~= 'number' then
error("Param to '".. v .."' must be a number")
end
me._func = {string.upper(v), num}
return me
end
end
local select_duration_functions = {'derivative', 'elapsed', 'non_negative_derivative'}
for i,v in ipairs(select_duration_functions) do
TSF[v] = function(me, dur)
if not TSQ.is_a_duration(dur) then
error("Not a duration (" .. dur .. ")")
end
me._func = {string.upper(v), dur}
return me
end
end
function TSF:__tostring()
local s = ''
local fld = ''
if type(self._field) == 'table' then
fld = tostring(self._field)
else
fld = string.format('%q', self._field)
end
if type(self._func) == 'string' then
s = tostring(self._func) .. '(' .. fld .. ')'
elseif type(self._func) == 'table' then
s = tostring(self._func[1]) .. '('
s = s .. fld .. ', '
s = s .. tostring(self._func[2])
s = s .. ')'
else
s = fld
end
if self._as ~= nil then
s = s .. ' AS ' .. string.format('%q', self._as)
end
return s
end
function TSQ:into(into)
-- TODO: what kind of checking/quoting goes here?
self._into = into
end
function TSQ:from(...)
if type(self._from) ~= "table" then
local ft = {}
local ftm = {}
ftm.__tostring = function(v)
return table.concat(v, ',')
end
setmetatable(ft, ftm)
self._from = ft
end
local tbl = {}
if select('#', ...) == 1 then
local v = select(1, ...)
if type(v) == "table" then
tbl = v
else
tbl[1] = tostring(v)
end
else
tbl = table.pack(...)
end
for i,v in ipairs(tbl) do
-- if quotes or / already surround string, don't add quotes.
-- It would be awesome to grab the bit inside and make sure it is correctly
-- escaped.
local vs = tostring(v)
if (vs:sub(1,1) == '"' and vs:sub(-1,-1) == '"') or
(vs:sub(1,1) == '/' and vs:sub(-1,-1) == '/') then
else
vs = string.format('%q', vs)
end
self._from[#self._from + 1] = vs
end
return self
end
function TSQ:limit(value)
if type(value) ~= "number" then
error("limit ("..value..") is not a number")
end
self._limit = value
return self
end
function TSQ:slimit(value)
if type(value) ~= "number" then
error("slimit ("..value..") is not a number")
end
self._slimit = value
return self
end
function TSQ:offset(value)
if type(value) ~= "number" then
error("offset ("..value..") is not a number")
end
self._offset = value
return self
end
function TSQ:soffset(value)
if type(value) ~= "number" then
error("soffset ("..value..") is not a number")
end
self._soffset = value
return self
end
function TSQ:groupby(...)
if type(self._groupby) ~= "table" then
local ft = {}
local ftm = {}
ftm.__tostring = function(v)
return table.concat(v, ',')
end
setmetatable(ft, ftm)
self._groupby = ft
end
local tbl = {}
if select('#', ...) == 1 then
local v = select(1, ...)
if type(v) == "table" then
tbl = v
else
tbl[1] = tostring(v)
end
else
tbl = table.pack(...)
end
for i,v in ipairs(tbl) do
local vs = string.format('%q', tostring(v))
self._groupby[#self._groupby + 1] = vs
end
return self
end
function TSQ.is_a_duration(value)
local vs = tostring(value)
local dig, units = string.match(vs, "^(%d+)([usmhdw]*)$")
if dig == nil then
return false
end
local allowed_units = {'u', 'ms', 's', 'm', 'h', 'd', 'w'}
for i,v in ipairs(allowed_units) do
if v == units then
return true
end
end
return false
end
function TSQ:groupbytime(time, offset)
if not TSQ.is_a_duration(time) then
return nil
end
if offset ~= nil and not TSQ.is_a_duration(offset) then
return nil
end
local gbt = {time, offset}
local gbtmeta = {}
gbtmeta.__tostring = function(v)
if #v <= 0 then return "" end
local time = "time( " .. tostring(v[1])
if #v > 1 then
time = time .. ", " .. tostring(v[2])
end
return time .. " )"
end
setmetatable(gbt, gbtmeta)
self._groupbytime = gbt
return self
end
function TSQ:fill(opt)
if type(opt) == "number" then
self._fill = opt
elseif type(opt) == "string" then
if opt == "null" or opt == "none" or opt == "previous" then
self._fill = opt
elseif opt == "prev" then
self._fill = "previous"
end
else
return nil
end
-- Added a fill, make sure there is a groupby
if self._groupby == nil then
self:groupby('*')
end
return self
end
function TSQ:orderby(field, asc)
if type(self._orderby) ~= "table" then
local ft = {}
local ftm = {}
ftm.__tostring = function(v)
return table.concat(v, ',')
end
setmetatable(ft, ftm)
self._orderby = ft
end
local vs = string.format('%q', tostring(field))
if asc == nil then
elseif type(asc) == 'boolean' then
if asc then
vs = vs .. ' ASC'
else
vs = vs .. ' DESC'
end
else
if string.lower(tostring(asc)) == "asc" then
vs = vs .. ' ASC'
else
vs = vs .. ' DESC'
end
end
self._orderby[#self._orderby + 1] = vs
return self
end
function TSQ.is_an_op(op)
--local binary_op = {"+", "-", "*", "/", "AND", "OR", "=", "!=", "<>", "<", "<=", ">", ">="}
local binary_op = {"=", "!=", "<>", "<", "<=", ">", ">=", "=~", "!~"}
for i,v in ipairs(binary_op) do
if v == op then
return true
end
end
return false
end
function TSQ.packageExpr(a, op, b)
if a == nil or b == nil or op == nil then
error("no nils allowed (" .. a .. ', ' .. op .. ', ' .. b ..")")
end
if not TSQ.is_an_op(op) then
error("operation (" .. op .. ") is not valid")
end
-- quoting is handled somwhat by the callers
return tostring(a) .. ' ' .. op .. ' ' .. tostring(b)
end
-- This has become more of an internal function.
function TSQ:where(a, op, b)
local exr = self.packageExpr(a, op, b)
if type(self._where) ~= "table" then
local andtbl = {}
local andmeta = {}
andmeta.__tostring = function(v)
local w = {}
for i,v in ipairs(v) do
w[#w + 1] = tostring(v)
end
return table.concat(w, ' AND ')
end
setmetatable(andtbl, andmeta)
self._where = andtbl
end
self._where[#self._where + 1] = exr
-- ANDs push to the end of where.
return self
end
-- This has become more of an internal function.
TSQ.AND = TSQ.where
-- This has become more of an internal function.
function TSQ:OR(a, op, b)
local exr = self.packageExpr(a, op, b)
local ortbl = {}
if type(self._where[#self._where]) ~= "table" then
-- convert last item into a table
local ormeta = {}
ormeta.__tostring = function(v)
local w = {}
for i,v in ipairs(v) do
w[#w + 1] = tostring(v)
end
return '( ' .. table.concat(w, ' OR ') .. ' )'
end
setmetatable(ortbl, ormeta)
ortbl[1] = self._where[#self._where]
self._where[#self._where] = ortbl
else
ortbl = self._where[#self._where]
end
ortbl[#ortbl + 1] = exr
-- ORs push onto an array that is at the end of self.where
return self
end
function TSQ.escape_identifier(id)
if string.match(id, "^[A-Za-z_][A-Za-z_0-9]*$") == nil then
-- Need to escape it.
local vs = string.format('%q', tostring(id))
return vs
end
return id
end
-- Specific where clauses for tags.
for i,v in ipairs{'where','AND','OR'} do
TSQ[v .. "_tag_is"] = function(me, tag, value)
local s = string.gsub(tostring(value), "'", "\\'")
return me[v](me, me.escape_identifier(tag), '=', "'" .. s .. "'")
end
TSQ[v .. "_tag_isnot"] = function(me, tag, value)
local s = string.gsub(tostring(value), "'", "\\'")
return me[v](me, me.escape_identifier(tag), '!=', "'" .. s .. "'")
end
TSQ[v .. "_tag_matches"] = function(me, tag, value)
return me[v](me, me.escape_identifier(tag), '=~', "/" .. tostring(value) .. "/")
end
TSQ[v .. "_tag_not_matching"] = function(me, tag, value)
return me[v](me, me.escape_identifier(tag), '!~', "/" .. tostring(value) .. "/")
end
end
-- Specific where clauses for fields.
for i,v in ipairs{'where','AND','OR'} do
for act,op in pairs{is='=', isnot='!=', greater='>', less='<'} do
TSQ[v .. "_field_" .. act] = function(me, field, value)
if type(value) == "boolean" then
value = tostring(value):upper()
elseif type(value) ~= "number" then
local s = string.gsub(tostring(value), "'", "\\'")
value = "'" .. s .. "'"
end
return me[v](me, me.escape_identifier(field), op, value)
end
end
end
function TSQ.validate_date(date)
-- Specify time with date time strings. Date time strings can take two formats:
-- YYYY-MM-DD HH:MM:SS.nnnnnnnnn and YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ, where the
-- second specification is RFC3339. Nanoseconds (nnnnnnnnn) are optional in
-- both formats.
-- It appeasr that the entire time portion is also optional.
-- Patterns in Lua are a bit limited, So we need to break down the string and
-- work with the parts.
local ts = {}
ts.year, ts.month, ts.day = string.match(date, "^(%d%d%d%d)%-(%d%d)%-(%d%d)")
-- only the year-month-day, so stop.
if #date > 10 then
tchar = string.sub(date, 11, 11)
if tchar ~= 'T' and tchar ~= ' ' then
error("Invalid date, missing time seperator. ("..date..")")
end
ts.hour, ts.minute, ts.second = string.match(date, "(%d%d):(%d%d):(%d%d)", 12)
ts.nano = string.match(date, "%.(%d+)", 20)
if ts.nano == nil then ts.nano = 0 end
if tchar == 'T' and string.sub(date, -1, -1) ~= 'Z' then
error("Invalid date, RFC3339 style missing final Z. ("..date..")")
end
else
ts.hour, ts.minute, ts.second, ts.nano = 0,0,0,0
end
-- Make them all numbers.
for k,v in pairs(ts) do
ts[k] = tonumber(v)
end
-- Now check that numbera are valid.
if ts.month > 12 then
error("Month (" .. ts.month .. ") out of range")
end
local daysinmonth = {31,28,31,30,31,30,31,31,30,31,30,31}
local daylimit = daysinmonth[ts.month]
if ts.month == 2 then
-- is leap?
-- missing the ts.year % 400 check. are you using this in 2416?
if ts.year % 4 == 0 and not ts.year % 100 == 0 then
daylimit = 29
end
elseif ts.day > daylimit then
error("Month (" .. ts.month .. ") has too many days (" .. ts.day .. " > " .. daylimit .. ")")
end
if ts.hour > 23 then
error("Too many hours (".. ts.hour .. ")")
end
if ts.minute > 59 then
error("Too many minutes (".. ts.hour .. ")")
end
if ts.second > 59 then
error("Too many seconds (".. ts.hour .. ")")
end
return true
end
function TSQ.is_a_date(date)
return pcall(TSQ.validate_date, date)
end
-- Specific where clauses for times.
for i,v in ipairs{'where','AND','OR'} do
TSQ[v .. "_time_after"] = function(me, time)
time = tostring(time)
if not me.is_a_duration(time) then
me.validate_date(time)
time = "'" .. time .. "'"
end
return me[v](me, 'time', '>', time)
end
TSQ[v .. "_time_before"] = function(me, time)
time = tostring(time)
if not me.is_a_duration(time) then
me.validate_date(time)
time = "'" .. time .. "'"
end
return me[v](me, 'time', '<', time)
end
TSQ[v .. "_time_ago"] = function(me, time)
time = tostring(time)
if not me.is_a_duration(time) then
error("Not a duration (" .. time .. ")")
end
return me[v](me, 'time', '>', "now() - " .. time)
end
TSQ[v .. "_time_since"] = function(me, time)
time = tostring(time)
if not me.is_a_duration(time) then
error("Not a duration (" .. time .. ")")
end
return me[v](me, 'time', '<', "now() + " .. time)
end
end
function TSQ:__tostring()
local s = 'SELECT '
s = s .. tostring(self._sel)
if type(self._into) == "string" then
s = s .. ' INTO ' .. tostring(self._into)
end
s = s .. ' FROM '.. tostring(self._from)
if type(self._where) == "table" then
s = s .. ' WHERE ' .. tostring(self._where)
end
if type(self._groupby) == "table" or type(self._groupbytime) == "table" then
local tags = {}
if type(self._groupby) == "table" then
tags[#tags + 1] = tostring(self._groupby)
end
if type(self._groupbytime) == "table" and #self._groupbytime > 0 then
tags[#tags + 1] = tostring(self._groupbytime)
end
s = s .. ' GROUP BY ' .. table.concat(tags, ",")
if self._fill ~= nil then
s = s .. ' fill(' .. self._fill .. ')'
end
end
if type(self._orderby) == "table" then
s = s .. ' ORDER BY ' .. tostring(self._orderby)
end
if type(self._limit) == "string" or type(self._limit) == "number" then
s = s .. ' LIMIT ' .. tostring(self._limit)
end
if type(self._offset) == "string" or type(self._offset) == "number" then
s = s .. ' OFFSET ' .. tostring(self._offset)
end
if type(self._slimit) == "string" or type(self._slimit) == "number" then
s = s .. ' SLIMIT ' .. tostring(self._slimit)
end
if type(self._soffset) == "string" or type(self._soffset) == "number" then
s = s .. ' SOFFSET ' .. tostring(self._soffset)
end
return s
end
if _VERSION == "Lua 5.1" then
-- From https://github.com/keplerproject/lua-compat-5.2
table.unpack = unpack
table.pack = function(...)
return { n = select('#', ...), ... }
end
end
------------------------------------------------------------------------------
---
-- Return the next values in the series returned from Timeseries.
-- This works much like Lua's next() function.
-- If there are multiple series, then the next for each is returned
--
-- This returns the next index or nil if at the end.
-- Following are tables for each series where the keys are the column names and the
-- values are from the current row.
function TSQ.next_in_series(serieses, idx)
if idx == nil then idx = 1 else idx = idx + 1 end
local result = {}
local has_values = 0
for i,series in ipairs(serieses) do
local value = series.values[idx]
local ret = {}
if value ~= nil then
has_values = has_values + 1
for i,v in ipairs(series.columns) do
ret[v] = value[i]
end
end
result[#result + 1] = ret
end
-- if all of the series are empty, then stop.
if has_values == 0 then
return nil
else
return idx, table.unpack(result)
end
end
function TSQ.series_ipairs(series)
return TSQ.next_in_series, series, nil
end
-- vim: set ai sw=4 ts=4 :