diff --git a/InfoLogger/public/logFilter/LogFilter.js b/InfoLogger/public/logFilter/LogFilter.js index 41d6203de..20599372b 100644 --- a/InfoLogger/public/logFilter/LogFilter.js +++ b/InfoLogger/public/logFilter/LogFilter.js @@ -191,25 +191,27 @@ export default class LogFilter extends Observable { return logValue.replace(/\r?\n|\r/g, ''); } - // eslint-disable-next-line guard-for-in - for (const field in criterias) { - const logValue = log[field]; - - // eslint-disable-next-line guard-for-in - for (const operator in criterias[field]) { - let criteriaValue = criterias[field][operator]; + /** + * 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 = ' '] - separator to be applied when filtering based on an array of values; `\n` has to be passed in case of message field + * @return {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; } - const separator = field === 'message' ? '\n' : ' '; - // logValue is sometime required, undefined means test fails and log is rejected switch (operator) { - case '$in': + case '$in': { if (logValue === undefined || !criteriaValue.includes(logValue)) { return false; } break; + } case '$match': { const criteriaList = criteriaValue.split(separator); if (criteriaList.length > 1) { @@ -259,8 +261,23 @@ export default class LogFilter extends Observable { continue; } } + return true; } - 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);