Skip to content

Commit

Permalink
[ML] Adding non-space aware checks for existing jobs (#82814)
Browse files Browse the repository at this point in the history
* [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>
  • Loading branch information
jgowdyelastic and kibanamachine committed Nov 10, 2020
1 parent 8e4643d commit dc287ea
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 11 deletions.
14 changes: 9 additions & 5 deletions x-pack/plugins/ml/server/models/job_service/jobs.ts
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/ml/server/routes/apidoc.json
Expand Up @@ -16,6 +16,7 @@
"GetDataFrameAnalyticsMessages",
"UpdateDataFrameAnalytics",
"DeleteDataFrameAnalytics",
"JobsExist",

"DataVisualizer",
"GetOverallStats",
Expand Down Expand Up @@ -84,6 +85,7 @@
"UpdateGroups",
"DeletingJobTasks",
"DeleteJobs",
"RevertModelSnapshot",

"Calendars",
"PutCalendars",
Expand Down
57 changes: 57 additions & 0 deletions x-pack/plugins/ml/server/routes/data_frame_analytics.ts
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
})
);
}
14 changes: 8 additions & 6 deletions x-pack/plugins/ml/server/routes/job_service.ts
Expand Up @@ -18,6 +18,7 @@ import {
topCategoriesSchema,
updateGroupsSchema,
revertModelSnapshotSchema,
jobsExistSchema,
} from './schemas/job_service_schema';

import { jobIdSchema } from './schemas/anomaly_detectors_schema';
Expand Down Expand Up @@ -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'],
Expand All @@ -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,
Expand Down
Expand Up @@ -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()),
});
5 changes: 5 additions & 0 deletions x-pack/plugins/ml/server/routes/schemas/job_service_schema.ts
Expand Up @@ -83,3 +83,8 @@ export const revertModelSnapshotSchema = schema.object({
)
),
});

export const jobsExistSchema = schema.object({
jobIds: schema.arrayOf(schema.string()),
allSpaces: schema.maybe(schema.boolean()),
});

0 comments on commit dc287ea

Please sign in to comment.