-
Notifications
You must be signed in to change notification settings - Fork 14
/
spl.peg
354 lines (296 loc) · 8.01 KB
/
spl.peg
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
/*
* Search Process Language for Elasticsearch
*/
{
var TIMESTAMP_FIELD = '@timestamp';
var RAW_MESSAGE_FIELD = 'message';
}
start
= first:QueryCommand rest:(_? '|' _? TailCommand)* _? {
var dsl = { "query" : first };
var postProcess;
if (!!rest && !!rest.length) {
var cmd = rest[0][3];
if (!!Array.isArray(cmd)) {
dsl["aggs"] = cmd[0];
postProcess = cmd[1];
} else {
dsl["aggs"] = cmd;
}
}
return [dsl, postProcess];
}
TailCommand
= StatsCommand
/ BinCommand
/ SigTermCommand
/ TransCommand
// JoinCommand
// EvalCommand
// Glue
SEP
= '='
_ "WhitespaceOrComment"
= Whitespace? Comment Whitespace?
/ Whitespace
Comment
= '#' [^\r\n]*
Whitespace
= WhitespaceChar+
WhitespaceChar
= [ \t\n\r]
// Field names and values
Field
= $[a-zA-Z0-9\._\-]+
Value
= $[a-zA-Z0-9\._\-]+
Boolean
= 'true' { return true; }
/ 'false' { return false; }
Integer
= num:[0-9]+ { return parseInt(num.join('')); }
// Special values
WildCardValue
= '"' chrs:DoubleQuotedChar* '"' { return chrs.join(''); }
/ "'" chrs:SingleQuotedChar* "'" { return chrs.join(''); }
/ chunks:WildCardChunk* { return chunks; }
WildCardChunk
= '*' { return '*'; }
/ '?' { return '?'; }
/ chrs:EscapedChar+ { return chrs.join(''); }
EscapedChar
= '\\' chr:MetaChar { return chr; }
/ SpecialChar
/ !MetaChar chr:. { return chr; }
LiteralValue
= '"' chrs:DoubleQuotedChar* '"' { return chrs.join(''); }
/ "'" chrs:SingleQuotedChar* "'" { return chrs.join(''); }
/ chrs:LiteralChar+ { return chrs.join(''); }
LiteralChar
= EscapedChar
DoubleQuotedChar
= '\\"' { return '"'; }
/ SpecialChar
/ '\\'? chr:[^"] { return chr; }
SingleQuotedChar
= "\\'" { return "'"; }
/ chr:[^'] { return chr; }
MetaChar
= chr:WhitespaceChar { return chr[0]; }
/ '(' / ')'
/ '{' / '}'
/ '[' / ']'
/ '+'
/ '-'
/ ':'
/ '\\'
/ '/'
/ '@'
/ '^'
/ '|'
/ '"'
/ "'"
/ '*'
/ '?'
SpecialChar
= '\\n' { return "\n"; }
/ '\\r' { return "\r"; }
/ '\\t' { return "\t"; }
// Query command
QueryCommand
= query:QueryExpression {
return {"query_string": {"query": query, "default_field": RAW_MESSAGE_FIELD}};
}
/ '*' { return {"match_all" : {}}; }
QueryExpression
= QueryOR
QueryOR
= a:QueryAND b:(_ 'OR' _ QueryAND)* {
if(!!(b) && !!(b.length)) {
return '(' + a + ' OR ' + b + ')';
}
return a;
}
QueryAND
= a:QueryNOT b:((_ 'AND')? _ QueryNOT)* {
if(!!(b) && !!(b.length)) {
return a + ' AND ' + b;
}
return a;
}
QueryNOT
= a:QueryUnaryNOT b:(_ 'NOT' _ QueryUnaryNOT)? {
if(!!(b) && !!(b.length)) {
return a + ' NOT ' + b;
}
return a;
}
QueryUnaryNOT
= neg:'-'? '(' _? expr:QueryExpression _? ')' {
if(neg) {
return '-(' + expr + ')';
}
return expr;
}
/ neg:'-'? clause:QueryClause {
if(neg) {
clause = '-' + clause;
}
return clause;
}
QueryClause
= '_exists_' SEP field:Field { return '_exists_:' + field; }
/ '_missing_' SEP field:Field { return '_missing_:' + field; }
/ field:Field SEP a:QueryRangeLow _? lo:QueryRangeValue _ 'TO' _ hi:QueryRangeValue _? b:QueryRangeHigh
{ return field + ':' + a + lo + ' TO ' + hi + b; }
/ field:Field SEP '^' val:LiteralValue { return field + ':^' + val; }
/ field:Field SEP '/' regex:RegexValue '/' { return field + ':/' + regex + '/'; }
/ field:Field SEP '(' _? first:WildCardValue rest:(_ WildCardValue)* _? ')' {
return [first, rest];
}
/ field:Field SEP val:WildCardValue { return field + ':' + val; }
RegexValue
= chrs:RegexChar* { return chrs.join(''); }
RegexChar
= '\\/' { return '/'; }
/ [^/]
QueryRangeLow
= '['
/ '{'
QueryRangeHigh
= ']'
/ '}'
QueryRangeValue
= '*' { return null; }
/ LiteralValue
// Direct Aggs command
StatsCommand
= 'stats' _ func:StatsFunction byGroups:(_ 'by' _ Groups)? {
var aggs = {};
if (!!byGroups) {
var by = byGroups[3];
var i = 0;
function setTerms(aggs, i) {
aggs[i] = {"terms":{"field":by[i]},"aggs":{}};
if (i < by.length-1) {
setTerms(aggs[i].aggs, i+1);
} else {
aggs[i].aggs[i] = func;
return;
}
}
setTerms(aggs, i);
} else {
aggs[0] = func;
}
return aggs;
}
StatsFunction
= type:AggType '(' _? field:(Field) _? ')' {
var agg = {};
if (typeof(type) == 'number') {
agg["percentiles"] = {"field": field, "percents": [type]};
} else {
agg[type] = {"field": field};
}
return agg;
}
AggType
= 'count' { return 'terms'; }
/ 'dc' { return 'cardinality'; }
/ 'min' { return 'min'; }
/ 'max' { return 'max'; }
/ 'avg' { return 'avg'; }
/ 'sum' { return 'sum'; }
/ 'pct' pct:(Integer) { return pct; }
/ 'es' { return 'extended_stats'; }
AggSettings
= first:AggSetting rest:MoreAggSettings* {
var arr = [first].concat(rest);
var obj = {};
for (var i=0; i<arr.length; i++) {
obj = Object.assign(obj, arr[i]);
}
return obj;
}
MoreAggSettings
= ','? _? arg:AggSetting { return arg; }
AggSetting
= field:Field SEP val:Value { var kv = {}; kv[field] = val; return kv; }
Groups
= first:Field rest:MoreGroups* {
return [first].concat(rest);
}
MoreGroups
= ','? _? field:Field { return field; }
SigTermCommand
= 'findkeywords' _ field:Field _ 'by' _ by:Groups {
var aggs = {};
var func = {"significant_terms":{"field": field}};
var i = 0;
function setTerms(aggs, i) {
aggs[i] = {"terms":{"field":by[i]},"aggs":{}};
if (i < by.length-1) {
setTerms(aggs[i].aggs, i+1);
} else {
aggs[i].aggs[i] = func;
return;
}
}
setTerms(aggs, i);
return aggs;
}
BinCommand
= 'bucket' _ field:Field _ 'span' SEP interval:Interval _? '|' _? stats:(StatsCommand / SigTermCommand / PredictCommand) {
return {"aggs":{"bucket":{"date_histogram":{"field":field,"interval":interval},"aggs":stats}}};
}
Interval
= chr:(Integer [smhdwy]) { return chr.join(''); }
PredictCommand
= 'predict' _ func:StatsFunction _ 'window' SEP window:Integer settings:(_ AggSettings)* _? {
var aggs = {};
aggs[0] = func;
aggs['predict'] = {"moving_avg":{"bucket_path":"0","window":window,"model": "holt_winters","settings": settings[0][1]}};
return aggs;
}
// Post-Process Commands
TransCommand
= 'transaction' _ field:Field startswith:(_ 'startswith' SEP WildCardValue)? endswith:(_ 'endswith' SEP WildCardValue)? maxevents:(_ 'maxevents' SEP Integer)? _? {
var topHitsSize = 3;
var groupSize = 100;
var startsWithChar, endsWithChar;
var aggs = {};
if (!!maxevents) { topHitsSize = maxevents[0][3] }
if (!!startswith) { startsWithChar = startswith[0][3] }
if (!!endswith) { endsWithChar = endswith[0][3] }
aggs['transId'] = {"terms":{"field":field,"size":groupSize},"aggs":{}};
aggs['transId']['aggs'] = {"transHits":{"top_hits":{"sort":TIMESTAMP_FIELD,"size":topHitsSize}}};
var postProcess = function(data) {
var transaction = data.aggregations.transId.buckets;
var transHits = [];
for (var i=0; i<transaction.length; i++) {
var topHits = transaction[i].transHits.hits.hits;
var results = [];
for (var i=0; i<topHits.length; i++) {
var msg = topHits[i]._source[RAW_MESSAGE_FIELD];
if (!!startsWithChar && !msg.indexOf(startsWithChar) && results.length == 0) {
break;
}
if (!!endsWithChar && !!msg.indexOf(endsWithChar)) {
results.push(topHits[i]._source);
break;
}
results.push(topHits[i]._source);
}
var count = results.length;
var duration = Date.parse(results[results.length-1][TIMESTAMP_FIELD]) - Date.parse(results[0][TIMESTAMP_FIELD]);
transHits.push({
count: count,
duration: duration,
results: results
});
}
return transHits;
}
return [aggs, postProcess];
}