-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] AIOps Log Rate Analysis: Improve query generators (#171008)
Improves the query generation for some parts of log rate analysis and adds jest tests to verify the improvements. Previously, some query generators were not concise enough with their checks and it could happen that for example range queries for the overall time range were added twice or that the range query for the overall time range was added although there were also range filters added for the inner baseline and deviation time range, for example: ```json "query": { "bool": { "filter": [ ... { "range": { "@timestamp": { "gte": 1698796800506, "lte": 1699568984807, "format": "epoch_millis" } } }, { "bool": { "should": [ { "range": { "@timestamp": { "gte": 1699272000000, "lte": 1699344000000, "format": "epoch_millis" } } }, { "range": { "@timestamp": { "gte": 1698969600000, "lte": 1699185600000, "format": "epoch_millis" } } } ] } } ], ... ``` The PR also unifies some mocks for the jest unit tests.
- Loading branch information
Showing
19 changed files
with
552 additions
and
251 deletions.
There are no files selected for viewing
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
19 changes: 19 additions & 0 deletions
19
x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/params_match_all.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,19 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export const paramsMock = { | ||
index: 'the-index', | ||
timeFieldName: 'the-time-field-name', | ||
start: 0, | ||
end: 50, | ||
baselineMin: 10, | ||
baselineMax: 20, | ||
deviationMin: 30, | ||
deviationMax: 40, | ||
includeFrozen: false, | ||
searchQuery: '{ "match_all": {} }', | ||
}; |
16 changes: 16 additions & 0 deletions
16
...ck/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/params_search_query.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,16 @@ | ||
/* | ||
* 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 type { AiopsLogRateAnalysisSchema } from '../../../../../common/api/log_rate_analysis/schema'; | ||
|
||
import { paramsMock } from './params_match_all'; | ||
import { searchQueryMock } from './search_query'; | ||
|
||
export const paramsSearchQueryMock: AiopsLogRateAnalysisSchema = { | ||
...paramsMock, | ||
searchQuery: searchQueryMock, | ||
}; |
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/search_query.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,16 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// This is the format that gets passed on from the Kibana search bar. | ||
export const searchQueryMock = JSON.stringify({ | ||
bool: { | ||
filter: [], | ||
minimum_should_match: 1, | ||
must_not: [], | ||
should: [{ term: { 'the-term': { value: 'the-value' } } }], | ||
}, | ||
}); |
100 changes: 100 additions & 0 deletions
100
x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_categories.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,100 @@ | ||
/* | ||
* 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 { createRandomSamplerWrapper } from '@kbn/ml-random-sampler-utils'; | ||
|
||
import { paramsMock } from './__mocks__/params_match_all'; | ||
import { getBaselineOrDeviationFilter, getCategoryRequest } from './fetch_categories'; | ||
|
||
describe('getBaselineOrDeviationFilter', () => { | ||
it('returns a filter that matches both baseline and deviation time range', () => { | ||
const baselineOrDeviationFilter = getBaselineOrDeviationFilter(paramsMock); | ||
|
||
expect(baselineOrDeviationFilter).toEqual({ | ||
bool: { | ||
should: [ | ||
{ | ||
range: { | ||
'the-time-field-name': { gte: 10, lte: 20, format: 'epoch_millis' }, | ||
}, | ||
}, | ||
{ | ||
range: { | ||
'the-time-field-name': { gte: 30, lte: 40, format: 'epoch_millis' }, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getCategoryRequest', () => { | ||
it('returns the category request', () => { | ||
const randomSamplerWrapper = createRandomSamplerWrapper({ | ||
probability: 0.1, | ||
seed: 1234, | ||
}); | ||
|
||
const query = getCategoryRequest(paramsMock, 'the-field-name', randomSamplerWrapper); | ||
|
||
// Because the time range filter is covered by the should clauses that cover both | ||
// baseline (10,20) and deviation (30,40), we expect that there is no other | ||
// time range filter whatsoever, for example for start/end (0,50). | ||
expect(query).toEqual({ | ||
index: 'the-index', | ||
size: 0, | ||
body: { | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ | ||
bool: { | ||
should: [ | ||
{ | ||
range: { | ||
'the-time-field-name': { | ||
gte: 10, | ||
lte: 20, | ||
format: 'epoch_millis', | ||
}, | ||
}, | ||
}, | ||
{ | ||
range: { | ||
'the-time-field-name': { | ||
gte: 30, | ||
lte: 40, | ||
format: 'epoch_millis', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
aggs: { | ||
sample: { | ||
random_sampler: { probability: 0.1, seed: 1234 }, | ||
aggs: { | ||
categories: { | ||
categorize_text: { field: 'the-field-name', size: 1000 }, | ||
aggs: { | ||
hit: { | ||
top_hits: { size: 1, sort: ['the-time-field-name'], _source: 'the-field-name' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.