forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SLO] fix tags filtering with query searching (elastic#175802)
Fixes elastic#175801
- Loading branch information
Showing
3 changed files
with
139 additions
and
25 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
x-pack/plugins/observability/public/hooks/slo/mix_kql_with_tags.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { mixKqlWithTags } from './mix_kql_with_tags'; | ||
|
||
describe('mixKqlWithTags', () => { | ||
it('mixes kql with selected tags', async () => { | ||
const query = 'something'; | ||
const tags = { | ||
included: ['production'], | ||
excluded: [], | ||
}; | ||
|
||
const kqlQueryMixedValue = mixKqlWithTags(query, tags); | ||
expect(kqlQueryMixedValue).toBe('something and slo.tags: (production)'); | ||
}); | ||
|
||
it('mixes kql with cleared out tags', async () => { | ||
const query = 'something'; | ||
const tags = { | ||
included: [], | ||
excluded: [], | ||
}; | ||
|
||
const kqlQueryMixedValue = mixKqlWithTags(query, tags); | ||
expect(kqlQueryMixedValue).toBe('something'); | ||
}); | ||
|
||
it('mixes kql with included and excluded tags', async () => { | ||
const query = 'something'; | ||
const tags = { | ||
included: ['production'], | ||
excluded: ['dev'], | ||
}; | ||
|
||
const kqlQueryMixedValue = mixKqlWithTags(query, tags); | ||
expect(kqlQueryMixedValue).toBe('something and slo.tags: (production) and not slo.tags: (dev)'); | ||
}); | ||
|
||
it('mixes empty query with tags', async () => { | ||
const query = ''; | ||
const tags = { | ||
included: ['production'], | ||
excluded: ['dev'], | ||
}; | ||
const kqlQueryMixedValue = mixKqlWithTags(query, tags); | ||
expect(kqlQueryMixedValue).toBe('slo.tags: (production) and not slo.tags: (dev)'); | ||
}); | ||
|
||
it('mixes empty query with empty tags', async () => { | ||
const query = ''; | ||
const tags = { | ||
included: [], | ||
excluded: [], | ||
}; | ||
|
||
const kqlQueryMixedValue = mixKqlWithTags(query, tags); | ||
expect(kqlQueryMixedValue).toBe(''); | ||
}); | ||
|
||
it('mixes empty query with undefined tags', async () => { | ||
const query = ''; | ||
const tags = { | ||
included: undefined, | ||
excluded: undefined, | ||
}; | ||
|
||
const kqlQueryMixedValue = mixKqlWithTags(query, tags); | ||
expect(kqlQueryMixedValue).toBe(''); | ||
}); | ||
|
||
it('mixes query with multiple included tags', async () => { | ||
const query = 'something'; | ||
const tags = { | ||
included: ['production', 'staging'], | ||
excluded: [], | ||
}; | ||
|
||
const kqlQueryMixedValue = mixKqlWithTags(query, tags); | ||
expect(kqlQueryMixedValue).toBe('something and slo.tags: (production or staging)'); | ||
}); | ||
|
||
it('mixes query with multiple excluded tags', async () => { | ||
const query = 'something'; | ||
const tags = { | ||
included: [], | ||
excluded: ['dev', 'production'], | ||
}; | ||
|
||
const kqlQueryMixedValue = mixKqlWithTags(query, tags); | ||
expect(kqlQueryMixedValue).toBe('something and not slo.tags: (dev or production)'); | ||
}); | ||
|
||
it('mixes query with multiple included and multiple excluded tags', async () => { | ||
const query = 'something'; | ||
const tags = { | ||
included: ['production', 'staging'], | ||
excluded: ['dev', 'pre', 'qa'], | ||
}; | ||
|
||
const kqlQueryMixedValue = mixKqlWithTags(query, tags); | ||
expect(kqlQueryMixedValue).toBe( | ||
'something and slo.tags: (production or staging) and not slo.tags: (dev or pre or qa)' | ||
); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
x-pack/plugins/observability/public/hooks/slo/mix_kql_with_tags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { SearchState } from '../../pages/slos/hooks/use_url_search_state'; | ||
|
||
export function mixKqlWithTags(kqlQuery: string, tags: SearchState['tags']) { | ||
if (!tags) { | ||
return kqlQuery; | ||
} | ||
const includedKqlTags = tags?.included?.join(' or '); | ||
const excludedKqlTags = tags?.excluded?.join(' or '); | ||
|
||
const queryParts = []; | ||
if (!!kqlQuery) { | ||
queryParts.push(kqlQuery); | ||
} | ||
if (!!includedKqlTags) { | ||
queryParts.push(`slo.tags: (${includedKqlTags})`); | ||
} | ||
if (!!excludedKqlTags) { | ||
queryParts.push(`not slo.tags: (${excludedKqlTags})`); | ||
} | ||
return queryParts.join(' and '); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters