From 664d6a4a141b3fee86c38ff673eb930e7f48c40e Mon Sep 17 00:00:00 2001 From: Melissa Alvarez Date: Wed, 19 Feb 2020 13:48:21 -0500 Subject: [PATCH] [ML] New Platform server shim: update job audit messages routes (#57925) * migrate all job audit messages routes to NP * update types and add missing api names from validation --- .../job_audit_messages/{index.js => index.ts} | 0 .../job_audit_messages.d.ts | 14 ++++ .../job_audit_messages/job_audit_messages.js | 8 +- .../plugins/ml/server/new_platform/plugin.ts | 1 - .../plugins/ml/server/routes/apidoc.json | 18 ++++- .../ml/server/routes/job_audit_messages.js | 40 --------- .../ml/server/routes/job_audit_messages.ts | 81 +++++++++++++++++++ 7 files changed, 116 insertions(+), 46 deletions(-) rename x-pack/legacy/plugins/ml/server/models/job_audit_messages/{index.js => index.ts} (100%) create mode 100644 x-pack/legacy/plugins/ml/server/models/job_audit_messages/job_audit_messages.d.ts delete mode 100644 x-pack/legacy/plugins/ml/server/routes/job_audit_messages.js create mode 100644 x-pack/legacy/plugins/ml/server/routes/job_audit_messages.ts diff --git a/x-pack/legacy/plugins/ml/server/models/job_audit_messages/index.js b/x-pack/legacy/plugins/ml/server/models/job_audit_messages/index.ts similarity index 100% rename from x-pack/legacy/plugins/ml/server/models/job_audit_messages/index.js rename to x-pack/legacy/plugins/ml/server/models/job_audit_messages/index.ts diff --git a/x-pack/legacy/plugins/ml/server/models/job_audit_messages/job_audit_messages.d.ts b/x-pack/legacy/plugins/ml/server/models/job_audit_messages/job_audit_messages.d.ts new file mode 100644 index 0000000000000..c4910b2535fb1 --- /dev/null +++ b/x-pack/legacy/plugins/ml/server/models/job_audit_messages/job_audit_messages.d.ts @@ -0,0 +1,14 @@ +/* + * 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 { APICaller } from 'src/core/server'; + +export function jobAuditMessagesProvider( + callAsCurrentUser: APICaller +): { + getJobAuditMessages: (jobId?: string, from?: string) => any; + getAuditMessagesSummary: (jobIds?: string[]) => any; +}; diff --git a/x-pack/legacy/plugins/ml/server/models/job_audit_messages/job_audit_messages.js b/x-pack/legacy/plugins/ml/server/models/job_audit_messages/job_audit_messages.js index 52495b3b732d0..2cdfc0ef4f4c5 100644 --- a/x-pack/legacy/plugins/ml/server/models/job_audit_messages/job_audit_messages.js +++ b/x-pack/legacy/plugins/ml/server/models/job_audit_messages/job_audit_messages.js @@ -34,14 +34,14 @@ const anomalyDetectorTypeFilter = { }, }; -export function jobAuditMessagesProvider(callWithRequest) { +export function jobAuditMessagesProvider(callAsCurrentUser) { // search for audit messages, // jobId is optional. without it, all jobs will be listed. // from is optional and should be a string formatted in ES time units. e.g. 12h, 1d, 7d async function getJobAuditMessages(jobId, from) { let gte = null; if (jobId !== undefined && from === undefined) { - const jobs = await callWithRequest('ml.jobs', { jobId }); + const jobs = await callAsCurrentUser('ml.jobs', { jobId }); if (jobs.count > 0 && jobs.jobs !== undefined) { gte = moment(jobs.jobs[0].create_time).valueOf(); } @@ -100,7 +100,7 @@ export function jobAuditMessagesProvider(callWithRequest) { } try { - const resp = await callWithRequest('search', { + const resp = await callAsCurrentUser('search', { index: ML_NOTIFICATION_INDEX_PATTERN, ignore_unavailable: true, rest_total_hits_as_int: true, @@ -155,7 +155,7 @@ export function jobAuditMessagesProvider(callWithRequest) { levelsPerJobAggSize = jobIds.length; } - const resp = await callWithRequest('search', { + const resp = await callAsCurrentUser('search', { index: ML_NOTIFICATION_INDEX_PATTERN, ignore_unavailable: true, rest_total_hits_as_int: true, diff --git a/x-pack/legacy/plugins/ml/server/new_platform/plugin.ts b/x-pack/legacy/plugins/ml/server/new_platform/plugin.ts index 69b08bfeda13d..60ede82d0de85 100644 --- a/x-pack/legacy/plugins/ml/server/new_platform/plugin.ts +++ b/x-pack/legacy/plugins/ml/server/new_platform/plugin.ts @@ -46,7 +46,6 @@ import { fieldsService } from '../routes/fields_service'; import { filtersRoutes } from '../routes/filters'; import { resultsServiceRoutes } from '../routes/results_service'; import { jobServiceRoutes } from '../routes/job_service'; -// @ts-ignore: could not find declaration file for module import { jobAuditMessagesRoutes } from '../routes/job_audit_messages'; // @ts-ignore: could not find declaration file for module import { fileDataVisualizerRoutes } from '../routes/file_data_visualizer'; diff --git a/x-pack/legacy/plugins/ml/server/routes/apidoc.json b/x-pack/legacy/plugins/ml/server/routes/apidoc.json index be1554bf55f78..4682371c16424 100644 --- a/x-pack/legacy/plugins/ml/server/routes/apidoc.json +++ b/x-pack/legacy/plugins/ml/server/routes/apidoc.json @@ -76,6 +76,22 @@ "CreateFilter", "UpdateFilter", "DeleteFilter", - "GetFiltersStats" + "GetFiltersStats", + "Indices", + "FieldCaps", + "SystemRoutes", + "HasPrivileges", + "MlCapabilities", + "MlNodeCount", + "MlInfo", + "MlEsSearch", + "JobAuditMessages", + "GetJobAuditMessages", + "GetAllJobAuditMessages", + "JobValidation", + "EstimateBucketSpan", + "CalculateModelMemoryLimit", + "ValidateCardinality", + "ValidateJob" ] } diff --git a/x-pack/legacy/plugins/ml/server/routes/job_audit_messages.js b/x-pack/legacy/plugins/ml/server/routes/job_audit_messages.js deleted file mode 100644 index 38e87d97044dd..0000000000000 --- a/x-pack/legacy/plugins/ml/server/routes/job_audit_messages.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 { callWithRequestFactory } from '../client/call_with_request_factory'; -import { wrapError } from '../client/errors'; -import { jobAuditMessagesProvider } from '../models/job_audit_messages'; - -export function jobAuditMessagesRoutes({ commonRouteConfig, elasticsearchPlugin, route }) { - route({ - method: 'GET', - path: '/api/ml/job_audit_messages/messages/{jobId}', - handler(request) { - const callWithRequest = callWithRequestFactory(elasticsearchPlugin, request); - const { getJobAuditMessages } = jobAuditMessagesProvider(callWithRequest); - const { jobId } = request.params; - const from = request.query.from; - return getJobAuditMessages(jobId, from).catch(resp => wrapError(resp)); - }, - config: { - ...commonRouteConfig, - }, - }); - - route({ - method: 'GET', - path: '/api/ml/job_audit_messages/messages', - handler(request) { - const callWithRequest = callWithRequestFactory(elasticsearchPlugin, request); - const { getJobAuditMessages } = jobAuditMessagesProvider(callWithRequest); - const from = request.query.from; - return getJobAuditMessages(undefined, from).catch(resp => wrapError(resp)); - }, - config: { - ...commonRouteConfig, - }, - }); -} diff --git a/x-pack/legacy/plugins/ml/server/routes/job_audit_messages.ts b/x-pack/legacy/plugins/ml/server/routes/job_audit_messages.ts new file mode 100644 index 0000000000000..7298312990005 --- /dev/null +++ b/x-pack/legacy/plugins/ml/server/routes/job_audit_messages.ts @@ -0,0 +1,81 @@ +/* + * 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 { schema } from '@kbn/config-schema'; +import { licensePreRoutingFactory } from '../new_platform/licence_check_pre_routing_factory'; +import { wrapError } from '../client/error_wrapper'; +import { RouteInitialization } from '../new_platform/plugin'; +import { jobAuditMessagesProvider } from '../models/job_audit_messages'; + +/** + * Routes for job audit message routes + */ +export function jobAuditMessagesRoutes({ xpackMainPlugin, router }: RouteInitialization) { + /** + * @apiGroup JobAuditMessages + * + * @api {get} /api/ml/job_audit_messages/messages/:jobId Get audit messages + * @apiName GetJobAuditMessages + * @apiDescription Returns audit messages for specified job ID + */ + router.get( + { + path: '/api/ml/job_audit_messages/messages/{jobId}', + validate: { + params: schema.object({ jobId: schema.maybe(schema.string()) }), + query: schema.maybe(schema.object({ from: schema.maybe(schema.any()) })), + }, + }, + licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => { + try { + const { getJobAuditMessages } = jobAuditMessagesProvider( + context.ml!.mlClient.callAsCurrentUser + ); + const { jobId } = request.params; + const { from } = request.query; + const resp = await getJobAuditMessages(jobId, from); + + return response.ok({ + body: resp, + }); + } catch (e) { + return response.customError(wrapError(e)); + } + }) + ); + + /** + * @apiGroup JobAuditMessages + * + * @api {get} /api/ml/results/anomalies_table_data Get all audit messages + * @apiName GetAllJobAuditMessages + * @apiDescription Returns all audit messages + */ + router.get( + { + path: '/api/ml/job_audit_messages/messages', + validate: { + params: schema.object({ jobId: schema.maybe(schema.string()) }), + query: schema.maybe(schema.object({ from: schema.maybe(schema.any()) })), + }, + }, + licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => { + try { + const { getJobAuditMessages } = jobAuditMessagesProvider( + context.ml!.mlClient.callAsCurrentUser + ); + const { from } = request.query; + const resp = await getJobAuditMessages(undefined, from); + + return response.ok({ + body: resp, + }); + } catch (e) { + return response.customError(wrapError(e)); + } + }) + ); +}