-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathLogFilter.js
More file actions
427 lines (393 loc) · 14.2 KB
/
Copy pathLogFilter.js
File metadata and controls
427 lines (393 loc) · 14.2 KB
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
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import { Observable } from '/js/src/index.js';
import { TEXT_FILTER_OPERATORS } from '../constants/text-filter-operators.const.js';
import { getDisabledSeverities } from '../constants/log-level-filters.const.js';
/**
* @typedef Criteria
* @type {object}
* @property {object} field - field name like pid, username, timestamp
* @property {string} field.operator - $match, $exclude, $since, $until, $min, $max, $in
*/
/**
* @typedef {Array.<Criteria>} Criteria
*/
/**
* This makes a criteria object with all properties initialized to empty or minimal value
* @returns {object} criteria object with all properties initialized
*/
const makeDefaultMatchExcludeOperators = () => ({
match: '',
$match: null,
exclude: '',
$exclude: null,
emptyFor: null,
$emptyFor: null,
});
/**
* This class stores raw filters from user (strings) and parsed ones (like Date object).
* It can generate a function to filter "messages" to be used
* on server side.
* It can also import and export an object representing its internal state,
* this is used to save this state on ILG URL bar.
*/
export default class LogFilter extends Observable {
/**
* Instantiate a LogFilter with criteria reset to empty or minimal value
* @param {Model} model - root model of the application
*/
constructor(model) {
super();
this.model = model;
this.resetCriteria();
}
/**
* Set a filter criteria to a field with an operator and value only if the new value is different than the current.
* For each field+operator a parsed property in criterias is made with associated cast (Date, number, Array).
* @param {string} field - field name like pid, username, timestamp
* @param {string} operator - $match, $exclude, $since, $until, $min, $max, $in
* @param {string} value - value to be set
* @returns {boolean} - true if value was set, false if value was the same as before
* @example
* setCriteria('severity', 'in', 'W E F')
* // severity is W or E or F
* //
*/
setCriteria(field, operator, value) {
if (!(operator in this.criterias[field])) {
throw new Error(`unknown operator ${operator} for ${field}`);
}
if (this.criterias[field][operator] !== value) {
this.criterias[field][operator] = value;
// auto-complete other properties / parse
switch (operator) {
case 'since':
this.criterias[field]['$since'] = this.model.timezone.parse(value);
break;
case 'until':
this.criterias[field]['$until'] = this.model.timezone.parse(value);
break;
case 'min':
this.criterias[field]['$min'] = parseInt(value, 10);
break;
case 'max':
this.criterias[field]['$max'] = parseInt(value, 10);
break;
case 'match':
this.criterias[field]['$match'] = value ? value : null;
break;
case 'exclude':
this.criterias[field]['$exclude'] = value ? value : null;
break;
case 'in':
this.criterias[field]['$in'] = value ? value.split(' ') : null;
break;
case 'emptyFor':
this.criterias[field]['$emptyFor'] = value === 'match' || value === 'exclude' ? value : null;
break;
default:
throw new Error('unknown operator');
}
// enforces on both severity and level as fromObject can set them in either order
if (field === 'severity' || field === 'level') {
this.enforceDisabledSeverities();
}
this.notify();
return true;
} else {
return false;
}
}
/**
* Exports all filled filters inputs
* @returns {object} minimal filter object
*/
toObject() {
// copy everything
const criterias = JSON.parse(JSON.stringify(this.criterias));
// clean-up the whole structure
for (const field in criterias) {
for (const operator in criterias[field]) {
// remote parsed properties (generated with fromJSON)
if (operator.includes('$')) {
delete criterias[field][operator];
}
// remote empty inputs
if (!criterias[field][operator]) {
delete criterias[field][operator];
} else if (operator === 'match' || operator === 'exclude') {
// encode potential breaking characters and escape double quotes as are used by browser by default
criterias[field][operator] = encodeURI(criterias[field][operator].replace(/["]+/g, '\\"'));
}
// remove empty fields
if (!Object.keys(criterias[field]).length) {
delete criterias[field];
}
}
}
return criterias;
}
/**
* Set criterias according to object passed as argument
* @param {object} criterias - object with criterias to be set
*/
fromObject(criterias) {
this.resetCriteria();
Object.keys(criterias).forEach((field) => {
Object.keys(criterias[field])
.filter((operator) => criterias[field][operator])
.forEach((operator) => this.setCriteria(field, operator, criterias[field][operator]));
});
this.notify();
}
/**
* Check whether at least one text filter is set by the user.
* Only text filters use the since/until and match/exclude fields.
* @returns {boolean} true if at least one text filter has a value
*/
hasActiveTextFilters() {
return Object.values(this.criterias).some((criteria) =>
TEXT_FILTER_OPERATORS.some((operator) => criteria[operator]?.trim()));
}
/**
* Check whether a severity is disabled for the current log level.
* @param {string} severityCode - [D, I, W, E, F]
* @returns {boolean} true if the severity is not allowed at the current level
*/
isSeverityDisabled(severityCode) {
return getDisabledSeverities(this.criterias.level.max).includes(severityCode);
}
/**
* Remove any active severity selections that are disallowed by the current level.
*/
enforceDisabledSeverities() {
const current = this.criterias.severity.$in;
if (!current) {
return;
}
const disabled = getDisabledSeverities(this.criterias.level.max);
if (disabled.length === 0) {
return;
}
const filteredSeverities = current.filter((s) => !disabled.includes(s));
// Only update if there is a change
if (filteredSeverities.length !== current.length) {
this.criterias.severity.$in = filteredSeverities;
this.criterias.severity.in = filteredSeverities.join(' ');
}
}
/**
* Generates a function to filter a log passed as argument to it
* Output of function is boolean.
* @returns {(message: WebSocketMessage) => boolean} - function to filter logs
*/
toStringifyFunction() {
/**
* This function will be stringified then sent to server so it can filter logs
* 'DATA_PLACEHOLDER' will be replaced by the stringified filters too so the function contains de data
* @param {WebSocketMessage} message - message to be filtered
* @returns {boolean} true if message passes criterias
*/
function filterFunction(message) {
const log = message.payload;
const criterias = 'DATA_PLACEHOLDER';
/**
* Transform timestamp of infologger into javascript Date object
* @param {number} timestamp - timestamp from infologger
* @returns {Date} - javascript Date object
*/
function parseInfoLoggerDate(timestamp) {
return new Date(timestamp * 1000);
}
/**
* Method to generate criteria value as Regex
* @param {string} criteria Criteria passed in by user
* @returns {RegExp} - regex criteria value
*/
function generateRegexCriteriaValue(criteria) {
criteria = criteria.replace(new RegExp('%', 'g'), '.*');
criteria = criteria.replace(new RegExp('_', 'g'), '.');
return new RegExp(`^${criteria}$`);
}
/**
* Method to replace all new lines from a log value
* @param {string} logValue - value of the log field that is to be checked (e.g. message, severity, etc.)
* @returns {string} - log value without new lines
*/
function removeNewLinesFrom(logValue) {
if (typeof logValue !== 'string') {
return logValue;
}
return logValue.replace(/\r?\n|\r/g, '');
}
/**
* Whether a log field value is considered empty for emptyFor purposes.
* @param {string|number|undefined|null} logValue - value of the log field
* @returns {boolean} - true if the value is undefined, null, or an empty string
*/
function isEmpty(logValue) {
return logValue === undefined || logValue === null || logValue === '';
}
/**
* Function that applies the criteria of one filter set by the user on each received logValue
* @param {object} logValue - value of the log field that is to be checked (e.g. message, severity, etc.)
* @param {object} criteria - object containing the criteria if applied by the user
* @param {string} [separator = ' '] - (' ', 'n') to be applied when filtering based on an array of values;
* @returns {boolean} - result of the log matching the filter set by user
*/
function isLogMatchingMessageCriteria(logValue, criteria, separator = ' ') {
for (const operator in criteria) {
let criteriaValue = criteria[operator];
// don't apply criterias not set
if (criteriaValue === null) {
continue;
}
switch (operator) {
case '$in': {
if (logValue === undefined || !criteriaValue.includes(logValue)) {
return false;
}
break;
}
case '$match': {
if (isEmpty(logValue)) {
if (criteria.$emptyFor !== 'match') {
return false;
}
break;
}
const criteriaList = criteriaValue.split(separator);
if (criteriaList.length > 1) {
criteriaValue = criteriaValue.replace(new RegExp(separator, 'g'), '|');
}
if (!generateRegexCriteriaValue(criteriaValue).test(removeNewLinesFrom(logValue))) {
return false;
}
break;
}
case '$exclude': {
if (isEmpty(logValue)) {
if (criteria.$emptyFor === 'exclude') {
return false;
}
break;
}
const criteriaList = criteriaValue.split(separator);
if (criteriaList.length > 1) {
criteriaValue = criteriaValue.replace(new RegExp(separator, 'g'), '|');
}
if (logValue !== undefined &&
generateRegexCriteriaValue(criteriaValue).test(removeNewLinesFrom(logValue))) {
return false;
}
break;
}
case '$emptyFor':
if (criteriaValue === 'match' && !criteria.$match && !isEmpty(logValue)) {
return false;
} else if (criteriaValue === 'exclude' && !criteria.$exclude && isEmpty(logValue)) {
return false;
}
break;
case '$since':
if (logValue === undefined || parseInfoLoggerDate(logValue) < parseInfoLoggerDate(criteriaValue)) {
return false;
}
break;
case '$until':
if (logValue === undefined || parseInfoLoggerDate(logValue) > parseInfoLoggerDate(criteriaValue)) {
return false;
}
break;
case '$min':
if (logValue === undefined || parseInt(logValue, 10) < parseInt(criteriaValue, 10)) {
return false;
}
break;
case '$max':
if (logValue === undefined || parseInt(logValue, 10) > parseInt(criteriaValue, 10)) {
return false;
}
break;
default:
continue;
}
}
return true;
}
/*
* Removes the message from the initial filtering as this puts a lot of stress on the server
* Filtering will be done initially on the small contained fields and only later if still needed on the message
*/
const messageCriteria = criterias.message;
delete criterias.message;
for (const field in criterias) {
if (isLogMatchingMessageCriteria(log[field], criterias[field], ' ')) {
continue;
} else {
return false;
}
}
return isLogMatchingMessageCriteria(log['message'], messageCriteria, '\n');
}
const criteriasJSON = JSON.stringify(this.criterias);
const functionAsString = filterFunction.toString();
const functionWithCriterias = functionAsString.replace('\'DATA_PLACEHOLDER\'', criteriasJSON);
return functionWithCriterias;
}
/**
* Reset all filters from the current LogFilter instance to there
* original state: empty or exclusive for other criterias.
*/
resetCriteria() {
const TEXT_FIELDS = [
'hostname',
'rolename',
'pid',
'username',
'system',
'facility',
'detector',
'partition',
'run',
'errcode',
'errline',
'errsource',
];
this.criterias = {
timestamp: {
since: '',
until: '',
$since: null,
$until: null,
},
...Object.fromEntries(TEXT_FIELDS.map((field) => [field, makeDefaultMatchExcludeOperators()])),
message: {
match: '',
$match: null,
exclude: '',
$exclude: null,
},
severity: {
in: 'I W E F',
$in: ['I', 'W', 'E', 'F'],
},
level: {
max: null,
$max: null,
},
};
this.notify();
}
}