Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 9 additions & 1 deletion x-pack/legacy/plugins/ml/server/routes/apidoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
"MlCapabilities",
"MlNodeCount",
"MlInfo",
"MlEsSearch"
"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));
}
})
);
}