Skip to content

Commit

Permalink
[ML] New Platform server shim: update job audit messages routes (elas…
Browse files Browse the repository at this point in the history
…tic#57925)

* migrate all job audit messages routes to NP

* update types and add missing api names from validation
  • Loading branch information
alvarezmelissa87 committed Feb 19, 2020
1 parent 71bda5f commit 664d6a4
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion x-pack/legacy/plugins/ml/server/new_platform/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
18 changes: 17 additions & 1 deletion x-pack/legacy/plugins/ml/server/routes/apidoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
40 changes: 0 additions & 40 deletions x-pack/legacy/plugins/ml/server/routes/job_audit_messages.js

This file was deleted.

81 changes: 81 additions & 0 deletions x-pack/legacy/plugins/ml/server/routes/job_audit_messages.ts
Original file line number Diff line number Diff line change
@@ -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));
}
})
);
}

0 comments on commit 664d6a4

Please sign in to comment.