-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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] API Integration tests: adds test for Data Frame Analytics evaluate endpoint #97856
Merged
alvarezmelissa87
merged 6 commits into
elastic:master
from
alvarezmelissa87:ml-dfa-evaluate-api-test
Apr 23, 2021
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
412a6ee
wip: add api test for evaluate endpoint
alvarezmelissa87 ec5009c
Add api test for evaluate endpoint
alvarezmelissa87 2e37d0c
resolve merge conflict
alvarezmelissa87 42e0253
add tests for view only and unauthorized user
alvarezmelissa87 d932fe2
Merge branch 'master' into ml-dfa-evaluate-api-test
kibanamachine 1ff624c
Merge branch 'master' into ml-dfa-evaluate-api-test
alvarezmelissa87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
x-pack/test/api_integration/apis/ml/data_frame_analytics/evaluate.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,188 @@ | ||
/* | ||
* 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 expect from '@kbn/expect'; | ||
import { FtrProviderContext } from '../../../ftr_provider_context'; | ||
import { USER } from '../../../../functional/services/ml/security_common'; | ||
import { DataFrameAnalyticsConfig } from '../../../../../plugins/ml/public/application/data_frame_analytics/common'; | ||
import { DeepPartial } from '../../../../../plugins/ml/common/types/common'; | ||
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api'; | ||
|
||
export default ({ getService }: FtrProviderContext) => { | ||
const esArchiver = getService('esArchiver'); | ||
const supertest = getService('supertestWithoutAuth'); | ||
const ml = getService('ml'); | ||
|
||
const currentTime = `${Date.now()}`; | ||
const generateDestinationIndex = (analyticsId: string) => `user-${analyticsId}`; | ||
const jobEval: any = { | ||
regression: { | ||
index: generateDestinationIndex(`regression_${currentTime}`), | ||
evaluation: { | ||
regression: { | ||
actual_field: 'stab', | ||
predicted_field: 'ml.stab_prediction', | ||
metrics: { | ||
r_squared: {}, | ||
mse: {}, | ||
msle: {}, | ||
huber: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
classification: { | ||
index: generateDestinationIndex(`classification_${currentTime}`), | ||
evaluation: { | ||
classification: { | ||
actual_field: 'y', | ||
predicted_field: 'ml.y_prediction', | ||
metrics: { multiclass_confusion_matrix: {}, accuracy: {}, recall: {} }, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const jobAnalysis: any = { | ||
classification: { | ||
source: { | ||
index: ['ft_bank_marketing'], | ||
query: { | ||
match_all: {}, | ||
}, | ||
}, | ||
analysis: { | ||
classification: { | ||
dependent_variable: 'y', | ||
training_percent: 20, | ||
}, | ||
}, | ||
}, | ||
regression: { | ||
source: { | ||
index: ['ft_egs_regression'], | ||
query: { | ||
match_all: {}, | ||
}, | ||
}, | ||
analysis: { | ||
regression: { | ||
dependent_variable: 'stab', | ||
training_percent: 20, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
interface TestConfig { | ||
jobType: string; | ||
config: DeepPartial<DataFrameAnalyticsConfig>; | ||
eval: any; | ||
} | ||
|
||
const testJobConfigs: TestConfig[] = ['regression', 'classification'].map((jobType, idx) => { | ||
const analyticsId = `${jobType}_${currentTime}`; | ||
return { | ||
jobType, | ||
config: { | ||
id: analyticsId, | ||
description: `Testing ${jobType} evaluation`, | ||
dest: { | ||
index: generateDestinationIndex(analyticsId), | ||
results_field: 'ml', | ||
}, | ||
analyzed_fields: { | ||
includes: [], | ||
excludes: [], | ||
}, | ||
model_memory_limit: '60mb', | ||
...jobAnalysis[jobType], | ||
}, | ||
eval: jobEval[jobType], | ||
}; | ||
}); | ||
|
||
async function createJobs(mockJobConfigs: TestConfig[]) { | ||
for (const jobConfig of mockJobConfigs) { | ||
await ml.api.createAndRunDFAJob(jobConfig.config as DataFrameAnalyticsConfig); | ||
} | ||
} | ||
|
||
describe('POST data_frame/_evaluate', () => { | ||
before(async () => { | ||
await esArchiver.loadIfNeeded('ml/bm_classification'); | ||
await esArchiver.loadIfNeeded('ml/egs_regression'); | ||
await ml.testResources.setKibanaTimeZoneToUTC(); | ||
await createJobs(testJobConfigs); | ||
}); | ||
|
||
after(async () => { | ||
await ml.api.cleanMlIndices(); | ||
}); | ||
|
||
testJobConfigs.forEach((testConfig) => { | ||
describe(`EvaluateDataFrameAnalytics ${testConfig.jobType}`, async () => { | ||
it(`should evaluate ${testConfig.jobType} analytics job`, async () => { | ||
const { body } = await supertest | ||
.post(`/api/ml/data_frame/_evaluate`) | ||
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER)) | ||
pheyos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.set(COMMON_REQUEST_HEADERS) | ||
.send(testConfig.eval) | ||
.expect(200); | ||
|
||
if (testConfig.jobType === 'classification') { | ||
const { classification } = body; | ||
expect(body).to.have.property('classification'); | ||
expect(classification).to.have.property('recall'); | ||
expect(classification).to.have.property('accuracy'); | ||
expect(classification).to.have.property('multiclass_confusion_matrix'); | ||
} else { | ||
const { regression } = body; | ||
expect(body).to.have.property('regression'); | ||
expect(regression).to.have.property('mse'); | ||
expect(regression).to.have.property('msle'); | ||
expect(regression).to.have.property('r_squared'); | ||
} | ||
}); | ||
|
||
it(`should evaluate ${testConfig.jobType} job for the user with only view permission`, async () => { | ||
const { body } = await supertest | ||
.post(`/api/ml/data_frame/_evaluate`) | ||
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER)) | ||
.set(COMMON_REQUEST_HEADERS) | ||
.send(testConfig.eval) | ||
.expect(200); | ||
|
||
if (testConfig.jobType === 'classification') { | ||
const { classification } = body; | ||
expect(body).to.have.property('classification'); | ||
expect(classification).to.have.property('recall'); | ||
expect(classification).to.have.property('accuracy'); | ||
expect(classification).to.have.property('multiclass_confusion_matrix'); | ||
} else { | ||
const { regression } = body; | ||
expect(body).to.have.property('regression'); | ||
expect(regression).to.have.property('mse'); | ||
expect(regression).to.have.property('msle'); | ||
expect(regression).to.have.property('r_squared'); | ||
} | ||
}); | ||
|
||
it(`should not allow unauthorized user to evaluate ${testConfig.jobType} job`, async () => { | ||
const { body } = await supertest | ||
.post(`/api/ml/data_frame/_evaluate`) | ||
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED)) | ||
.set(COMMON_REQUEST_HEADERS) | ||
.send(testConfig.eval) | ||
.expect(403); | ||
|
||
expect(body.error).to.eql('Forbidden'); | ||
expect(body.message).to.eql('Forbidden'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
idx
isn't used, so could be removed here.