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 all 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
3 changes: 3 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,6 +88,9 @@ 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 operator (e.g. tag:thing or or tag:other) remove and replace with the first occurring operator
const invalidOperatorPattern = /\s+(and|or)\s+(and|or)\s+/ig;
newQueryString = newQueryString.replace(invalidOperatorPattern, ' $1 ');
// If string starts/ends with 'and' or 'or' remove that as that is illegal kuery syntax
newQueryString = newQueryString.replace(endPattern, '');
newQueryString = newQueryString.replace(startPattern, '');
Expand Down