Skip to content

Commit

Permalink
[ML] Anomaly Explorer - ensure valid syntax after removing 2nd of 3 f…
Browse files Browse the repository at this point in the history
…ilters via icon (#34187)

* Remove middle filter value correctly using icons

* remove middle filter value in OR query correctly with icons

* combine operator pattern check
  • Loading branch information
alvarezmelissa87 authored Apr 2, 2019
1 parent 461289e commit d3dd95d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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

0 comments on commit d3dd95d

Please sign in to comment.