Skip to content

Commit 664d6a4

Browse files
[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
1 parent 71bda5f commit 664d6a4

File tree

7 files changed

+116
-46
lines changed

7 files changed

+116
-46
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { APICaller } from 'src/core/server';
8+
9+
export function jobAuditMessagesProvider(
10+
callAsCurrentUser: APICaller
11+
): {
12+
getJobAuditMessages: (jobId?: string, from?: string) => any;
13+
getAuditMessagesSummary: (jobIds?: string[]) => any;
14+
};

x-pack/legacy/plugins/ml/server/models/job_audit_messages/job_audit_messages.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ const anomalyDetectorTypeFilter = {
3434
},
3535
};
3636

37-
export function jobAuditMessagesProvider(callWithRequest) {
37+
export function jobAuditMessagesProvider(callAsCurrentUser) {
3838
// search for audit messages,
3939
// jobId is optional. without it, all jobs will be listed.
4040
// from is optional and should be a string formatted in ES time units. e.g. 12h, 1d, 7d
4141
async function getJobAuditMessages(jobId, from) {
4242
let gte = null;
4343
if (jobId !== undefined && from === undefined) {
44-
const jobs = await callWithRequest('ml.jobs', { jobId });
44+
const jobs = await callAsCurrentUser('ml.jobs', { jobId });
4545
if (jobs.count > 0 && jobs.jobs !== undefined) {
4646
gte = moment(jobs.jobs[0].create_time).valueOf();
4747
}
@@ -100,7 +100,7 @@ export function jobAuditMessagesProvider(callWithRequest) {
100100
}
101101

102102
try {
103-
const resp = await callWithRequest('search', {
103+
const resp = await callAsCurrentUser('search', {
104104
index: ML_NOTIFICATION_INDEX_PATTERN,
105105
ignore_unavailable: true,
106106
rest_total_hits_as_int: true,
@@ -155,7 +155,7 @@ export function jobAuditMessagesProvider(callWithRequest) {
155155
levelsPerJobAggSize = jobIds.length;
156156
}
157157

158-
const resp = await callWithRequest('search', {
158+
const resp = await callAsCurrentUser('search', {
159159
index: ML_NOTIFICATION_INDEX_PATTERN,
160160
ignore_unavailable: true,
161161
rest_total_hits_as_int: true,

x-pack/legacy/plugins/ml/server/new_platform/plugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import { fieldsService } from '../routes/fields_service';
4646
import { filtersRoutes } from '../routes/filters';
4747
import { resultsServiceRoutes } from '../routes/results_service';
4848
import { jobServiceRoutes } from '../routes/job_service';
49-
// @ts-ignore: could not find declaration file for module
5049
import { jobAuditMessagesRoutes } from '../routes/job_audit_messages';
5150
// @ts-ignore: could not find declaration file for module
5251
import { fileDataVisualizerRoutes } from '../routes/file_data_visualizer';

x-pack/legacy/plugins/ml/server/routes/apidoc.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@
7676
"CreateFilter",
7777
"UpdateFilter",
7878
"DeleteFilter",
79-
"GetFiltersStats"
79+
"GetFiltersStats",
80+
"Indices",
81+
"FieldCaps",
82+
"SystemRoutes",
83+
"HasPrivileges",
84+
"MlCapabilities",
85+
"MlNodeCount",
86+
"MlInfo",
87+
"MlEsSearch",
88+
"JobAuditMessages",
89+
"GetJobAuditMessages",
90+
"GetAllJobAuditMessages",
91+
"JobValidation",
92+
"EstimateBucketSpan",
93+
"CalculateModelMemoryLimit",
94+
"ValidateCardinality",
95+
"ValidateJob"
8096
]
8197
}

x-pack/legacy/plugins/ml/server/routes/job_audit_messages.js

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { schema } from '@kbn/config-schema';
8+
import { licensePreRoutingFactory } from '../new_platform/licence_check_pre_routing_factory';
9+
import { wrapError } from '../client/error_wrapper';
10+
import { RouteInitialization } from '../new_platform/plugin';
11+
import { jobAuditMessagesProvider } from '../models/job_audit_messages';
12+
13+
/**
14+
* Routes for job audit message routes
15+
*/
16+
export function jobAuditMessagesRoutes({ xpackMainPlugin, router }: RouteInitialization) {
17+
/**
18+
* @apiGroup JobAuditMessages
19+
*
20+
* @api {get} /api/ml/job_audit_messages/messages/:jobId Get audit messages
21+
* @apiName GetJobAuditMessages
22+
* @apiDescription Returns audit messages for specified job ID
23+
*/
24+
router.get(
25+
{
26+
path: '/api/ml/job_audit_messages/messages/{jobId}',
27+
validate: {
28+
params: schema.object({ jobId: schema.maybe(schema.string()) }),
29+
query: schema.maybe(schema.object({ from: schema.maybe(schema.any()) })),
30+
},
31+
},
32+
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
33+
try {
34+
const { getJobAuditMessages } = jobAuditMessagesProvider(
35+
context.ml!.mlClient.callAsCurrentUser
36+
);
37+
const { jobId } = request.params;
38+
const { from } = request.query;
39+
const resp = await getJobAuditMessages(jobId, from);
40+
41+
return response.ok({
42+
body: resp,
43+
});
44+
} catch (e) {
45+
return response.customError(wrapError(e));
46+
}
47+
})
48+
);
49+
50+
/**
51+
* @apiGroup JobAuditMessages
52+
*
53+
* @api {get} /api/ml/results/anomalies_table_data Get all audit messages
54+
* @apiName GetAllJobAuditMessages
55+
* @apiDescription Returns all audit messages
56+
*/
57+
router.get(
58+
{
59+
path: '/api/ml/job_audit_messages/messages',
60+
validate: {
61+
params: schema.object({ jobId: schema.maybe(schema.string()) }),
62+
query: schema.maybe(schema.object({ from: schema.maybe(schema.any()) })),
63+
},
64+
},
65+
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
66+
try {
67+
const { getJobAuditMessages } = jobAuditMessagesProvider(
68+
context.ml!.mlClient.callAsCurrentUser
69+
);
70+
const { from } = request.query;
71+
const resp = await getJobAuditMessages(undefined, from);
72+
73+
return response.ok({
74+
body: resp,
75+
});
76+
} catch (e) {
77+
return response.customError(wrapError(e));
78+
}
79+
})
80+
);
81+
}

0 commit comments

Comments
 (0)