Skip to content

Commit cc21b64

Browse files
[ML] New Platform server shim: update job service routes to use new platform router (#57403)
* wip: convert jobService route file to TS and use NP router * add schema definitions for route params * add api docs description for routes * update schema and rename client * update calendarManager * fix typo in schema * use NP context savedObjectsClient for rollup true * request no longer passed to JobServiceProvider * update anomalyDetectors schema for job update * add missing key to anomalydetectors schema
1 parent 204767e commit cc21b64

15 files changed

Lines changed: 781 additions & 384 deletions

File tree

x-pack/legacy/plugins/ml/public/application/services/ml_api_service/jobs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const jobs = {
2121

2222
jobsWithTimerange(dateFormatTz) {
2323
return http({
24-
url: `${basePath()}/jobs/jobs_with_timerange`,
24+
url: `${basePath()}/jobs/jobs_with_time_range`,
2525
method: 'POST',
2626
data: {
2727
dateFormatTz,

x-pack/legacy/plugins/ml/server/models/calendar/calendar_manager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { difference } from 'lodash';
88
import Boom from 'boom';
9+
import { IScopedClusterClient } from 'src/core/server';
910
import { EventManager, CalendarEvent } from './event_manager';
1011

1112
interface BasicCalendar {
@@ -23,13 +24,12 @@ export interface FormCalendar extends BasicCalendar {
2324
}
2425

2526
export class CalendarManager {
26-
private _client: any;
27+
private _client: IScopedClusterClient['callAsCurrentUser'];
2728
private _eventManager: any;
2829

29-
constructor(isLegacy: boolean, client: any) {
30-
const actualClient = isLegacy === true ? client : client.ml!.mlClient.callAsCurrentUser;
31-
this._client = actualClient;
32-
this._eventManager = new EventManager(actualClient);
30+
constructor(client: any) {
31+
this._client = client;
32+
this._eventManager = new EventManager(client);
3333
}
3434

3535
async getCalendar(calendarId: string) {

x-pack/legacy/plugins/ml/server/models/job_service/groups.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { CalendarManager } from '../calendar';
88

99
export function groupsProvider(callWithRequest) {
10-
const calMngr = new CalendarManager(true, callWithRequest);
10+
const calMngr = new CalendarManager(callWithRequest);
1111

1212
async function getAllGroups() {
1313
const groups = {};

x-pack/legacy/plugins/ml/server/models/job_service/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import {
1414
topCategoriesProvider,
1515
} from './new_job';
1616

17-
export function jobServiceProvider(callWithRequest, request) {
17+
export function jobServiceProvider(callAsCurrentUser) {
1818
return {
19-
...datafeedsProvider(callWithRequest),
20-
...jobsProvider(callWithRequest),
21-
...groupsProvider(callWithRequest),
22-
...newJobCapsProvider(callWithRequest, request),
23-
...newJobChartsProvider(callWithRequest, request),
24-
...categorizationExamplesProvider(callWithRequest, request),
25-
...topCategoriesProvider(callWithRequest, request),
19+
...datafeedsProvider(callAsCurrentUser),
20+
...jobsProvider(callAsCurrentUser),
21+
...groupsProvider(callAsCurrentUser),
22+
...newJobCapsProvider(callAsCurrentUser),
23+
...newJobChartsProvider(callAsCurrentUser),
24+
...categorizationExamplesProvider(callAsCurrentUser),
25+
...topCategoriesProvider(callAsCurrentUser),
2626
};
2727
}

x-pack/legacy/plugins/ml/server/models/job_service/jobs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function jobsProvider(callWithRequest) {
2222
const { forceDeleteDatafeed, getDatafeedIdsByJobId } = datafeedsProvider(callWithRequest);
2323
const { getAuditMessagesSummary } = jobAuditMessagesProvider(callWithRequest);
2424
const { getLatestBucketTimestampByJob } = resultsServiceProvider(callWithRequest);
25-
const calMngr = new CalendarManager(true, callWithRequest);
25+
const calMngr = new CalendarManager(callWithRequest);
2626

2727
async function forceDeleteJob(jobId) {
2828
return callWithRequest('ml.deleteJob', { jobId, force: true });

x-pack/legacy/plugins/ml/server/models/job_service/new_job_caps/field_service.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { cloneDeep } from 'lodash';
8-
import { Request } from 'src/legacy/server/kbn_server';
8+
import { SavedObjectsClientContract } from 'kibana/server';
99
import {
1010
Field,
1111
Aggregation,
@@ -40,22 +40,27 @@ export function fieldServiceProvider(
4040
indexPattern: string,
4141
isRollup: boolean,
4242
callWithRequest: any,
43-
request: Request
43+
savedObjectsClient: SavedObjectsClientContract
4444
) {
45-
return new FieldsService(indexPattern, isRollup, callWithRequest, request);
45+
return new FieldsService(indexPattern, isRollup, callWithRequest, savedObjectsClient);
4646
}
4747

4848
class FieldsService {
4949
private _indexPattern: string;
5050
private _isRollup: boolean;
5151
private _callWithRequest: any;
52-
private _request: Request;
52+
private _savedObjectsClient: SavedObjectsClientContract;
5353

54-
constructor(indexPattern: string, isRollup: boolean, callWithRequest: any, request: Request) {
54+
constructor(
55+
indexPattern: string,
56+
isRollup: boolean,
57+
callWithRequest: any,
58+
savedObjectsClient: any
59+
) {
5560
this._indexPattern = indexPattern;
5661
this._isRollup = isRollup;
5762
this._callWithRequest = callWithRequest;
58-
this._request = request;
63+
this._savedObjectsClient = savedObjectsClient;
5964
}
6065

6166
private async loadFieldCaps(): Promise<any> {
@@ -104,7 +109,7 @@ class FieldsService {
104109
const rollupService = await rollupServiceProvider(
105110
this._indexPattern,
106111
this._callWithRequest,
107-
this._request
112+
this._savedObjectsClient
108113
);
109114
const rollupConfigs: RollupJob[] | null = await rollupService.getRollupJobs();
110115

x-pack/legacy/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.test.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import cloudwatchJobCaps from './__mocks__/results/cloudwatch_rollup_job_caps.js
1818
describe('job_service - job_caps', () => {
1919
let callWithRequestNonRollupMock: jest.Mock;
2020
let callWithRequestRollupMock: jest.Mock;
21-
let requestMock: any;
21+
let savedObjectsClientMock: any;
2222

2323
beforeEach(() => {
2424
callWithRequestNonRollupMock = jest.fn((action: string) => {
@@ -37,32 +37,28 @@ describe('job_service - job_caps', () => {
3737
}
3838
});
3939

40-
requestMock = {
41-
getSavedObjectsClient: jest.fn(() => {
42-
return {
43-
async find() {
44-
return Promise.resolve(kibanaSavedObjects);
45-
},
46-
};
47-
}),
40+
savedObjectsClientMock = {
41+
async find() {
42+
return Promise.resolve(kibanaSavedObjects);
43+
},
4844
};
4945
});
5046

5147
describe('farequote newJobCaps()', () => {
5248
it('can get job caps for index pattern', async done => {
5349
const indexPattern = 'farequote-*';
5450
const isRollup = false;
55-
const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock, requestMock);
56-
const response = await newJobCaps(indexPattern, isRollup);
51+
const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock);
52+
const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock);
5753
expect(response).toEqual(farequoteJobCaps);
5854
done();
5955
});
6056

6157
it('can get rollup job caps for non rollup index pattern', async done => {
6258
const indexPattern = 'farequote-*';
6359
const isRollup = true;
64-
const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock, requestMock);
65-
const response = await newJobCaps(indexPattern, isRollup);
60+
const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock);
61+
const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock);
6662
expect(response).toEqual(farequoteJobCapsEmpty);
6763
done();
6864
});
@@ -72,17 +68,17 @@ describe('job_service - job_caps', () => {
7268
it('can get rollup job caps for rollup index pattern', async done => {
7369
const indexPattern = 'cloud_roll_index';
7470
const isRollup = true;
75-
const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock, requestMock);
76-
const response = await newJobCaps(indexPattern, isRollup);
71+
const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock);
72+
const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock);
7773
expect(response).toEqual(cloudwatchJobCaps);
7874
done();
7975
});
8076

8177
it('can get non rollup job caps for rollup index pattern', async done => {
8278
const indexPattern = 'cloud_roll_index';
8379
const isRollup = false;
84-
const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock, requestMock);
85-
const response = await newJobCaps(indexPattern, isRollup);
80+
const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock);
81+
const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock);
8682
expect(response).not.toEqual(cloudwatchJobCaps);
8783
done();
8884
});

x-pack/legacy/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { Request } from 'src/legacy/server/kbn_server';
7+
import { SavedObjectsClientContract } from 'kibana/server';
88
import { Aggregation, Field, NewJobCaps } from '../../../../common/types/fields';
99
import { fieldServiceProvider } from './field_service';
1010

1111
interface NewJobCapsResponse {
1212
[indexPattern: string]: NewJobCaps;
1313
}
1414

15-
export function newJobCapsProvider(callWithRequest: any, request: Request) {
15+
export function newJobCapsProvider(callWithRequest: any) {
1616
async function newJobCaps(
1717
indexPattern: string,
18-
isRollup: boolean = false
18+
isRollup: boolean = false,
19+
savedObjectsClient: SavedObjectsClientContract
1920
): Promise<NewJobCapsResponse> {
20-
const fieldService = fieldServiceProvider(indexPattern, isRollup, callWithRequest, request);
21+
const fieldService = fieldServiceProvider(
22+
indexPattern,
23+
isRollup,
24+
callWithRequest,
25+
savedObjectsClient
26+
);
2127
const { aggs, fields } = await fieldService.getData();
2228
convertForStringify(aggs, fields);
2329

x-pack/legacy/plugins/ml/server/models/job_service/new_job_caps/rollup.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { Request } from 'src/legacy/server/kbn_server';
87
import { SavedObject } from 'src/core/server';
8+
import { SavedObjectsClientContract } from 'kibana/server';
99
import { FieldId } from '../../../../common/types/fields';
1010
import { ES_AGGREGATION } from '../../../../common/constants/aggregation_types';
1111

@@ -21,9 +21,9 @@ export interface RollupJob {
2121
export async function rollupServiceProvider(
2222
indexPattern: string,
2323
callWithRequest: any,
24-
request: Request
24+
savedObjectsClient: SavedObjectsClientContract
2525
) {
26-
const rollupIndexPatternObject = await loadRollupIndexPattern(indexPattern, request);
26+
const rollupIndexPatternObject = await loadRollupIndexPattern(indexPattern, savedObjectsClient);
2727
let jobIndexPatterns: string[] = [indexPattern];
2828

2929
async function getRollupJobs(): Promise<RollupJob[] | null> {
@@ -57,9 +57,8 @@ export async function rollupServiceProvider(
5757

5858
async function loadRollupIndexPattern(
5959
indexPattern: string,
60-
request: Request
60+
savedObjectsClient: SavedObjectsClientContract
6161
): Promise<SavedObject | null> {
62-
const savedObjectsClient = request.getSavedObjectsClient();
6362
const resp = await savedObjectsClient.find({
6463
type: 'index-pattern',
6564
fields: ['title', 'type', 'typeMeta'],

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66

77
import { schema } from '@kbn/config-schema';
88

9+
const customRulesSchema = schema.maybe(
10+
schema.arrayOf(
11+
schema.maybe(
12+
schema.object({
13+
actions: schema.arrayOf(schema.string()),
14+
conditions: schema.arrayOf(schema.any()),
15+
scope: schema.maybe(schema.any()),
16+
})
17+
)
18+
)
19+
);
20+
921
const detectorSchema = schema.object({
1022
identifier: schema.maybe(schema.string()),
1123
function: schema.string(),
@@ -14,6 +26,7 @@ const detectorSchema = schema.object({
1426
over_field_name: schema.maybe(schema.string()),
1527
partition_field_name: schema.maybe(schema.string()),
1628
detector_description: schema.maybe(schema.string()),
29+
custom_rules: customRulesSchema,
1730
});
1831

1932
const customUrlSchema = {
@@ -34,15 +47,8 @@ export const anomalyDetectionUpdateJobSchema = {
3447
schema.maybe(
3548
schema.object({
3649
detector_index: schema.number(),
37-
custom_rules: schema.arrayOf(
38-
schema.maybe(
39-
schema.object({
40-
actions: schema.arrayOf(schema.string()),
41-
conditions: schema.arrayOf(schema.any()),
42-
scope: schema.maybe(schema.any()),
43-
})
44-
)
45-
),
50+
description: schema.maybe(schema.string()),
51+
custom_rules: customRulesSchema,
4652
})
4753
)
4854
)

0 commit comments

Comments
 (0)