Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Anomaly Explorer - ensure valid syntax after removing 2nd of 3 filters via icon #34187

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ describe('ML - KqlFilterBar utils', () => {
expect(result).to.be(expectedOutput);
});

it('removes selected fieldName/fieldValue correctly from AND query string when it is the middle value', () => {
const currentQueryString = `http.response.status_code : "400" and http.response.status_code : "200"
and http.response.status_code : "300"`;
const expectedOutput = 'http.response.status_code : "400" and http.response.status_code : "300"';
const result = removeFilterFromQueryString(currentQueryString, fieldName, fieldValue);
expect(result).to.be(expectedOutput);
});

it('removes selected fieldName/fieldValue correctly from OR query string when it is the middle value', () => {
const currentQueryString = `http.response.status_code : "400" or http.response.status_code : "200"
or http.response.status_code : "300"`;
const expectedOutput = 'http.response.status_code : "400" or http.response.status_code : "300"';
const result = removeFilterFromQueryString(currentQueryString, fieldName, fieldValue);
expect(result).to.be(expectedOutput);
});



});

describe('getQueryPattern', () => {
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/ml/public/components/kql_filter_bar/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ export function removeFilterFromQueryString(currentQueryString, fieldName, field
// match 'and' or 'or' at the start/end of the string
const endPattern = /\s(and|or)\s*$/ig;
const startPattern = /^\s*(and|or)\s/ig;
// If string has a double 'and' operator (e.g. tag:thing and and tag:other) remove as it is illegal kuery syntax
const invalidAndPattern = /\s+(and)\s+(and)\s+/ig;
// If string has a double 'or' operator (e.g. tag:thing or or tag:other) remove as it is illegal kuery syntax
const invalidOrPattern = /\s+(or)\s+(or)\s+/ig;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't catch OR AND or AND OR.
the regex could be changed to something like /\s+(and|or)\s+(and|or)\s+/ig to catch all combinations of duplicate operators.

// If string starts/ends with 'and' or 'or' remove that as that is illegal kuery syntax
newQueryString = newQueryString.replace(endPattern, '');
newQueryString = newQueryString.replace(startPattern, '');
newQueryString = newQueryString.replace(invalidAndPattern, ' and ');
newQueryString = newQueryString.replace(invalidOrPattern, ' or ');

return newQueryString;
}