[ML] Fix spaces job ID check (#84404)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
James Gowdy 2020-11-30 09:34:33 +00:00 committed by GitHub
parent bdf7b88b45
commit 767286e2da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -39,15 +39,19 @@ export function getMlClient(
const jobIds =
jobType === 'anomaly-detector' ? getADJobIdsFromRequest(p) : getDFAJobIdsFromRequest(p);
if (jobIds.length) {
const filteredJobIds = await jobSavedObjectService.filterJobIdsForSpace(jobType, jobIds);
let missingIds = jobIds.filter((j) => filteredJobIds.indexOf(j) === -1);
if (allowWildcards === true && missingIds.join().match('\\*') !== null) {
// filter out wildcard ids from the error
missingIds = missingIds.filter((id) => id.match('\\*') === null);
}
if (missingIds.length) {
throw new MLJobNotFound(`No known job with id '${missingIds.join(',')}'`);
}
await checkIds(jobType, jobIds, allowWildcards);
}
}
async function checkIds(jobType: JobType, jobIds: string[], allowWildcards: boolean = false) {
const filteredJobIds = await jobSavedObjectService.filterJobIdsForSpace(jobType, jobIds);
let missingIds = jobIds.filter((j) => filteredJobIds.indexOf(j) === -1);
if (allowWildcards === true && missingIds.join().match('\\*') !== null) {
// filter out wildcard ids from the error
missingIds = missingIds.filter((id) => id.match('\\*') === null);
}
if (missingIds.length) {
throw new MLJobNotFound(`No known job with id '${missingIds.join(',')}'`);
}
}
@ -59,8 +63,17 @@ export function getMlClient(
if (ids.length) {
// find all groups from unfiltered jobs
const responseGroupIds = [...new Set(allJobs.map((j) => j.groups ?? []).flat())];
// work out which ids requested are actually groups
const requestedGroupIds = ids.filter((id) => responseGroupIds.includes(id));
// work out which ids requested are actually groups and which are jobs
const requestedGroupIds: string[] = [];
const requestedJobIds: string[] = [];
ids.forEach((id) => {
if (responseGroupIds.includes(id)) {
requestedGroupIds.push(id);
} else {
requestedJobIds.push(id);
}
});
// find all groups from filtered jobs
const groupIdsFromFilteredJobs = [
@ -77,10 +90,15 @@ export function getMlClient(
);
if (groupsIdsThatDidNotMatch.length) {
// is there are group ids which were requested but didn't
// if there are group ids which were requested but didn't
// exist in filtered jobs, list them in an error
throw new MLJobNotFound(`No known job with id '${groupsIdsThatDidNotMatch.join(',')}'`);
}
// check the remaining jobs ids
if (requestedJobIds.length) {
await checkIds('anomaly-detector', requestedJobIds, true);
}
}
}