-
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] Cardinality validation API integration tests (#65971)
* [ML] refactor validate_cardinality to TS * [ML] cardinality api integration tests * [ML] resolve PR comments, validateJobObject as TS guard
- Loading branch information
Showing
6 changed files
with
223 additions
and
37 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
13 changes: 0 additions & 13 deletions
13
x-pack/plugins/ml/server/models/job_validation/validate_cardinality.d.ts
This file was deleted.
Oops, something went wrong.
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
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
175 changes: 175 additions & 0 deletions
175
x-pack/test/api_integration/apis/ml/job_validation/cardinality.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,175 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import expect from '@kbn/expect'; | ||
import { FtrProviderContext } from '../../../ftr_provider_context'; | ||
import { USER } from '../../../../functional/services/machine_learning/security_common'; | ||
|
||
const COMMON_HEADERS = { | ||
'kbn-xsrf': 'some-xsrf-token', | ||
}; | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export default ({ getService }: FtrProviderContext) => { | ||
const esArchiver = getService('esArchiver'); | ||
const supertest = getService('supertestWithoutAuth'); | ||
const ml = getService('ml'); | ||
|
||
describe('ValidateCardinality', function() { | ||
before(async () => { | ||
await esArchiver.loadIfNeeded('ml/ecommerce'); | ||
await ml.testResources.setKibanaTimeZoneToUTC(); | ||
}); | ||
|
||
after(async () => { | ||
await ml.api.cleanMlIndices(); | ||
}); | ||
|
||
it(`should recognize a valid cardinality`, async () => { | ||
const requestBody = { | ||
job_id: '', | ||
description: '', | ||
groups: [], | ||
analysis_config: { | ||
bucket_span: '10m', | ||
detectors: [ | ||
{ | ||
function: 'mean', | ||
field_name: 'products.base_price', | ||
partition_field_name: 'geoip.city_name', | ||
}, | ||
], | ||
influencers: ['geoip.city_name'], | ||
}, | ||
data_description: { time_field: 'order_date' }, | ||
analysis_limits: { model_memory_limit: '12MB' }, | ||
model_plot_config: { enabled: true }, | ||
datafeed_config: { | ||
datafeed_id: 'datafeed-', | ||
job_id: '', | ||
indices: ['ft_ecommerce'], | ||
query: { bool: { must: [{ match_all: {} }], filter: [], must_not: [] } }, | ||
}, | ||
}; | ||
|
||
const { body } = await supertest | ||
.post('/api/ml/validate/cardinality') | ||
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER)) | ||
.set(COMMON_HEADERS) | ||
.send(requestBody) | ||
.expect(200); | ||
|
||
expect(body).to.eql([{ id: 'success_cardinality' }]); | ||
}); | ||
|
||
it(`should recognize a high model plot cardinality`, async () => { | ||
const requestBody = { | ||
job_id: '', | ||
description: '', | ||
groups: [], | ||
analysis_config: { | ||
bucket_span: '10m', | ||
detectors: [ | ||
{ | ||
function: 'mean', | ||
field_name: 'products.base_price', | ||
// some high cardinality field | ||
partition_field_name: 'order_id', | ||
}, | ||
], | ||
influencers: ['geoip.city_name'], | ||
}, | ||
data_description: { time_field: 'order_date' }, | ||
analysis_limits: { model_memory_limit: '11MB' }, | ||
model_plot_config: { enabled: true }, | ||
datafeed_config: { | ||
datafeed_id: 'datafeed-', | ||
job_id: '', | ||
indices: ['ft_ecommerce'], | ||
query: { bool: { must: [{ match_all: {} }], filter: [], must_not: [] } }, | ||
}, | ||
}; | ||
const { body } = await supertest | ||
.post('/api/ml/validate/cardinality') | ||
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER)) | ||
.set(COMMON_HEADERS) | ||
.send(requestBody) | ||
.expect(200); | ||
|
||
expect(body).to.eql([ | ||
{ id: 'cardinality_model_plot_high', modelPlotCardinality: 4711 }, | ||
{ id: 'cardinality_partition_field', fieldName: 'order_id' }, | ||
]); | ||
}); | ||
|
||
it('should not validate cardinality in case request payload is invalid', async () => { | ||
const requestBody = { | ||
job_id: '', | ||
description: '', | ||
groups: [], | ||
// missing analysis_config | ||
data_description: { time_field: 'order_date' }, | ||
analysis_limits: { model_memory_limit: '12MB' }, | ||
model_plot_config: { enabled: true }, | ||
datafeed_config: { | ||
datafeed_id: 'datafeed-', | ||
job_id: '', | ||
indices: ['ft_ecommerce'], | ||
query: { bool: { must: [{ match_all: {} }], filter: [], must_not: [] } }, | ||
}, | ||
}; | ||
|
||
const { body } = await supertest | ||
.post('/api/ml/validate/cardinality') | ||
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER)) | ||
.set(COMMON_HEADERS) | ||
.send(requestBody) | ||
.expect(400); | ||
|
||
expect(body.error).to.eql('Bad Request'); | ||
expect(body.message).to.eql( | ||
'[request body.analysis_config.detectors]: expected value of type [array] but got [undefined]' | ||
); | ||
}); | ||
|
||
it('should not validate cardinality if the user does not have required permissions', async () => { | ||
const requestBody = { | ||
job_id: '', | ||
description: '', | ||
groups: [], | ||
analysis_config: { | ||
bucket_span: '10m', | ||
detectors: [ | ||
{ | ||
function: 'mean', | ||
field_name: 'products.base_price', | ||
partition_field_name: 'geoip.city_name', | ||
}, | ||
], | ||
influencers: ['geoip.city_name'], | ||
}, | ||
data_description: { time_field: 'order_date' }, | ||
analysis_limits: { model_memory_limit: '12MB' }, | ||
model_plot_config: { enabled: true }, | ||
datafeed_config: { | ||
datafeed_id: 'datafeed-', | ||
job_id: '', | ||
indices: ['ft_ecommerce'], | ||
query: { bool: { must: [{ match_all: {} }], filter: [], must_not: [] } }, | ||
}, | ||
}; | ||
|
||
const { body } = await supertest | ||
.post('/api/ml/validate/cardinality') | ||
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER)) | ||
.set(COMMON_HEADERS) | ||
.send(requestBody) | ||
.expect(404); | ||
|
||
expect(body.error).to.eql('Not Found'); | ||
expect(body.message).to.eql('Not Found'); | ||
}); | ||
}); | ||
}; |
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