[ML] Adding non-space aware checks for existing jobs (#82814)

* [ML] Adding non-space aware checks for existing jobs

* merge with existing jobs exist endpoint

* updating comments

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
James Gowdy 2020-11-10 20:54:32 +00:00 committed by GitHub
parent 8e4643dcdd
commit dc287eaadc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 11 deletions

View file

@ -414,17 +414,21 @@ export function jobsProvider(client: IScopedClusterClient, mlClient: MlClient) {
// Checks if each of the jobs in the specified list of IDs exist.
// Job IDs in supplied array may contain wildcard '*' characters
// e.g. *_low_request_rate_ecs
async function jobsExist(jobIds: string[] = []) {
async function jobsExist(jobIds: string[] = [], allSpaces: boolean = false) {
const results: { [id: string]: boolean } = {};
for (const jobId of jobIds) {
try {
const { body } = await mlClient.getJobs<MlJobsResponse>({
job_id: jobId,
});
const { body } = allSpaces
? await client.asInternalUser.ml.getJobs<MlJobsResponse>({
job_id: jobId,
})
: await mlClient.getJobs<MlJobsResponse>({
job_id: jobId,
});
results[jobId] = body.count > 0;
} catch (e) {
// if a non-wildcarded job id is supplied, the get jobs endpoint will 404
if (e.body?.status !== 404) {
if (e.statusCode !== 404) {
throw e;
}
results[jobId] = false;

View file

@ -16,6 +16,7 @@
"GetDataFrameAnalyticsMessages",
"UpdateDataFrameAnalytics",
"DeleteDataFrameAnalytics",
"JobsExist",
"DataVisualizer",
"GetOverallStats",
@ -84,6 +85,7 @@
"UpdateGroups",
"DeletingJobTasks",
"DeleteJobs",
"RevertModelSnapshot",
"Calendars",
"PutCalendars",

View file

@ -16,10 +16,12 @@ import {
analyticsIdSchema,
stopsDataFrameAnalyticsJobQuerySchema,
deleteDataFrameAnalyticsJobSchema,
jobsExistSchema,
} from './schemas/data_analytics_schema';
import { IndexPatternHandler } from '../models/data_frame_analytics/index_patterns';
import { DeleteDataFrameAnalyticsWithIndexStatus } from '../../common/types/data_frame_analytics';
import { getAuthorizationHeader } from '../lib/request_authorization';
import { DataFrameAnalyticsConfig } from '../../common/types/data_frame_analytics';
function getIndexPatternId(context: RequestHandlerContext, patternName: string) {
const iph = new IndexPatternHandler(context.core.savedObjects.client);
@ -541,4 +543,59 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense, routeGuard }: Rout
}
})
);
/**
* @apiGroup DataFrameAnalytics
*
* @api {post} /api/ml/data_frame/analytics/job_exists Check whether jobs exists in current or any space
* @apiName JobExists
* @apiDescription Checks if each of the jobs in the specified list of IDs exist.
* If allSpaces is true, the check will look across all spaces.
*
* @apiSchema (params) analyticsIdSchema
*/
router.post(
{
path: '/api/ml/data_frame/analytics/jobs_exist',
validate: {
body: jobsExistSchema,
},
options: {
tags: ['access:ml:canGetDataFrameAnalytics'],
},
},
routeGuard.fullLicenseAPIGuard(async ({ client, mlClient, request, response }) => {
try {
const { analyticsIds, allSpaces } = request.body;
const results: { [id: string]: boolean } = {};
for (const id of analyticsIds) {
try {
const { body } = allSpaces
? await client.asInternalUser.ml.getDataFrameAnalytics<{
data_frame_analytics: DataFrameAnalyticsConfig[];
}>({
id,
})
: await mlClient.getDataFrameAnalytics<{
data_frame_analytics: DataFrameAnalyticsConfig[];
}>({
id,
});
results[id] = body.data_frame_analytics.length > 0;
} catch (error) {
if (error.statusCode !== 404) {
throw error;
}
results[id] = false;
}
}
return response.ok({
body: { results },
});
} catch (e) {
return response.customError(wrapError(e));
}
})
);
}

View file

@ -18,6 +18,7 @@ import {
topCategoriesSchema,
updateGroupsSchema,
revertModelSnapshotSchema,
jobsExistSchema,
} from './schemas/job_service_schema';
import { jobIdSchema } from './schemas/anomaly_detectors_schema';
@ -400,17 +401,18 @@ export function jobServiceRoutes({ router, routeGuard }: RouteInitialization) {
/**
* @apiGroup JobService
*
* @api {post} /api/ml/jobs/jobs_exist Check if jobs exist
* @api {post} /api/ml/jobs/jobs_exist Check whether jobs exists in current or any space
* @apiName JobsExist
* @apiDescription Checks if each of the jobs in the specified list of IDs exist
* @apiDescription Checks if each of the jobs in the specified list of IDs exist.
* If allSpaces is true, the check will look across all spaces.
*
* @apiSchema (body) jobIdsSchema
* @apiSchema (body) jobsExistSchema
*/
router.post(
{
path: '/api/ml/jobs/jobs_exist',
validate: {
body: jobIdsSchema,
body: jobsExistSchema,
},
options: {
tags: ['access:ml:canGetJobs'],
@ -419,8 +421,8 @@ export function jobServiceRoutes({ router, routeGuard }: RouteInitialization) {
routeGuard.fullLicenseAPIGuard(async ({ client, mlClient, request, response }) => {
try {
const { jobsExist } = jobServiceProvider(client, mlClient);
const { jobIds } = request.body;
const resp = await jobsExist(jobIds);
const { jobIds, allSpaces } = request.body;
const resp = await jobsExist(jobIds, allSpaces);
return response.ok({
body: resp,

View file

@ -81,3 +81,8 @@ export const dataAnalyticsJobUpdateSchema = schema.object({
export const stopsDataFrameAnalyticsJobQuerySchema = schema.object({
force: schema.maybe(schema.boolean()),
});
export const jobsExistSchema = schema.object({
analyticsIds: schema.arrayOf(schema.string()),
allSpaces: schema.maybe(schema.boolean()),
});

View file

@ -83,3 +83,8 @@ export const revertModelSnapshotSchema = schema.object({
)
),
});
export const jobsExistSchema = schema.object({
jobIds: schema.arrayOf(schema.string()),
allSpaces: schema.maybe(schema.boolean()),
});