Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] New Platform server shim: update job service routes to use new platform router #57403

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const jobs = {

jobsWithTimerange(dateFormatTz) {
return http({
url: `${basePath()}/jobs/jobs_with_timerange`,
url: `${basePath()}/jobs/jobs_with_time_range`,
method: 'POST',
data: {
dateFormatTz,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { difference } from 'lodash';
import Boom from 'boom';
import { IScopedClusterClient } from 'src/core/server';
import { EventManager, CalendarEvent } from './event_manager';

interface BasicCalendar {
Expand All @@ -23,13 +24,12 @@ export interface FormCalendar extends BasicCalendar {
}

export class CalendarManager {
private _client: any;
private _client: IScopedClusterClient['callAsCurrentUser'];
private _eventManager: any;

constructor(isLegacy: boolean, client: any) {
const actualClient = isLegacy === true ? client : client.ml!.mlClient.callAsCurrentUser;
this._client = actualClient;
this._eventManager = new EventManager(actualClient);
constructor(client: any) {
this._client = client;
this._eventManager = new EventManager(client);
}

async getCalendar(calendarId: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { CalendarManager } from '../calendar';

export function groupsProvider(callWithRequest) {
const calMngr = new CalendarManager(true, callWithRequest);
const calMngr = new CalendarManager(callWithRequest);

async function getAllGroups() {
const groups = {};
Expand Down
16 changes: 8 additions & 8 deletions x-pack/legacy/plugins/ml/server/models/job_service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import {
topCategoriesProvider,
} from './new_job';

export function jobServiceProvider(callWithRequest, request) {
export function jobServiceProvider(callAsCurrentUser) {
return {
...datafeedsProvider(callWithRequest),
...jobsProvider(callWithRequest),
...groupsProvider(callWithRequest),
...newJobCapsProvider(callWithRequest, request),
...newJobChartsProvider(callWithRequest, request),
...categorizationExamplesProvider(callWithRequest, request),
...topCategoriesProvider(callWithRequest, request),
...datafeedsProvider(callAsCurrentUser),
...jobsProvider(callAsCurrentUser),
...groupsProvider(callAsCurrentUser),
...newJobCapsProvider(callAsCurrentUser),
...newJobChartsProvider(callAsCurrentUser),
...categorizationExamplesProvider(callAsCurrentUser),
...topCategoriesProvider(callAsCurrentUser),
};
}
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/ml/server/models/job_service/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function jobsProvider(callWithRequest) {
const { forceDeleteDatafeed, getDatafeedIdsByJobId } = datafeedsProvider(callWithRequest);
const { getAuditMessagesSummary } = jobAuditMessagesProvider(callWithRequest);
const { getLatestBucketTimestampByJob } = resultsServiceProvider(callWithRequest);
const calMngr = new CalendarManager(true, callWithRequest);
const calMngr = new CalendarManager(callWithRequest);

async function forceDeleteJob(jobId) {
return callWithRequest('ml.deleteJob', { jobId, force: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { cloneDeep } from 'lodash';
import { Request } from 'src/legacy/server/kbn_server';
import { SavedObjectsClientContract } from 'kibana/server';
import {
Field,
Aggregation,
Expand Down Expand Up @@ -40,22 +40,27 @@ export function fieldServiceProvider(
indexPattern: string,
isRollup: boolean,
callWithRequest: any,
request: Request
savedObjectsClient: SavedObjectsClientContract
) {
return new FieldsService(indexPattern, isRollup, callWithRequest, request);
return new FieldsService(indexPattern, isRollup, callWithRequest, savedObjectsClient);
}

class FieldsService {
private _indexPattern: string;
private _isRollup: boolean;
private _callWithRequest: any;
private _request: Request;
private _savedObjectsClient: SavedObjectsClientContract;

constructor(indexPattern: string, isRollup: boolean, callWithRequest: any, request: Request) {
constructor(
indexPattern: string,
isRollup: boolean,
callWithRequest: any,
savedObjectsClient: any
) {
this._indexPattern = indexPattern;
this._isRollup = isRollup;
this._callWithRequest = callWithRequest;
this._request = request;
this._savedObjectsClient = savedObjectsClient;
}

private async loadFieldCaps(): Promise<any> {
Expand Down Expand Up @@ -104,7 +109,7 @@ class FieldsService {
const rollupService = await rollupServiceProvider(
this._indexPattern,
this._callWithRequest,
this._request
this._savedObjectsClient
);
const rollupConfigs: RollupJob[] | null = await rollupService.getRollupJobs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import cloudwatchJobCaps from './__mocks__/results/cloudwatch_rollup_job_caps.js
describe('job_service - job_caps', () => {
let callWithRequestNonRollupMock: jest.Mock;
let callWithRequestRollupMock: jest.Mock;
let requestMock: any;
let savedObjectsClientMock: any;

beforeEach(() => {
callWithRequestNonRollupMock = jest.fn((action: string) => {
Expand All @@ -37,32 +37,28 @@ describe('job_service - job_caps', () => {
}
});

requestMock = {
getSavedObjectsClient: jest.fn(() => {
return {
async find() {
return Promise.resolve(kibanaSavedObjects);
},
};
}),
savedObjectsClientMock = {
async find() {
return Promise.resolve(kibanaSavedObjects);
},
};
});

describe('farequote newJobCaps()', () => {
it('can get job caps for index pattern', async done => {
const indexPattern = 'farequote-*';
const isRollup = false;
const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock, requestMock);
const response = await newJobCaps(indexPattern, isRollup);
const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock);
const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock);
expect(response).toEqual(farequoteJobCaps);
done();
});

it('can get rollup job caps for non rollup index pattern', async done => {
const indexPattern = 'farequote-*';
const isRollup = true;
const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock, requestMock);
const response = await newJobCaps(indexPattern, isRollup);
const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock);
const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock);
expect(response).toEqual(farequoteJobCapsEmpty);
done();
});
Expand All @@ -72,17 +68,17 @@ describe('job_service - job_caps', () => {
it('can get rollup job caps for rollup index pattern', async done => {
const indexPattern = 'cloud_roll_index';
const isRollup = true;
const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock, requestMock);
const response = await newJobCaps(indexPattern, isRollup);
const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock);
const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock);
expect(response).toEqual(cloudwatchJobCaps);
done();
});

it('can get non rollup job caps for rollup index pattern', async done => {
const indexPattern = 'cloud_roll_index';
const isRollup = false;
const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock, requestMock);
const response = await newJobCaps(indexPattern, isRollup);
const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock);
const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock);
expect(response).not.toEqual(cloudwatchJobCaps);
done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@
* you may not use this file except in compliance with the Elastic License.
*/

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

interface NewJobCapsResponse {
[indexPattern: string]: NewJobCaps;
}

export function newJobCapsProvider(callWithRequest: any, request: Request) {
export function newJobCapsProvider(callWithRequest: any) {
async function newJobCaps(
indexPattern: string,
isRollup: boolean = false
isRollup: boolean = false,
savedObjectsClient: SavedObjectsClientContract
): Promise<NewJobCapsResponse> {
const fieldService = fieldServiceProvider(indexPattern, isRollup, callWithRequest, request);
const fieldService = fieldServiceProvider(
indexPattern,
isRollup,
callWithRequest,
savedObjectsClient
);
const { aggs, fields } = await fieldService.getData();
convertForStringify(aggs, fields);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Request } from 'src/legacy/server/kbn_server';
import { SavedObject } from 'src/core/server';
import { SavedObjectsClientContract } from 'kibana/server';
import { FieldId } from '../../../../common/types/fields';
import { ES_AGGREGATION } from '../../../../common/constants/aggregation_types';

Expand All @@ -21,9 +21,9 @@ export interface RollupJob {
export async function rollupServiceProvider(
indexPattern: string,
callWithRequest: any,
request: Request
savedObjectsClient: SavedObjectsClientContract
) {
const rollupIndexPatternObject = await loadRollupIndexPattern(indexPattern, request);
const rollupIndexPatternObject = await loadRollupIndexPattern(indexPattern, savedObjectsClient);
let jobIndexPatterns: string[] = [indexPattern];

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

async function loadRollupIndexPattern(
indexPattern: string,
request: Request
savedObjectsClient: SavedObjectsClientContract
): Promise<SavedObject | null> {
const savedObjectsClient = request.getSavedObjectsClient();
const resp = await savedObjectsClient.find({
type: 'index-pattern',
fields: ['title', 'type', 'typeMeta'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

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

const customRulesSchema = schema.maybe(
schema.arrayOf(
schema.maybe(
schema.object({
actions: schema.arrayOf(schema.string()),
conditions: schema.arrayOf(schema.any()),
scope: schema.maybe(schema.any()),
})
)
)
);

const detectorSchema = schema.object({
identifier: schema.maybe(schema.string()),
function: schema.string(),
Expand All @@ -14,6 +26,7 @@ const detectorSchema = schema.object({
over_field_name: schema.maybe(schema.string()),
partition_field_name: schema.maybe(schema.string()),
detector_description: schema.maybe(schema.string()),
custom_rules: customRulesSchema,
});

const customUrlSchema = {
Expand All @@ -34,15 +47,8 @@ export const anomalyDetectionUpdateJobSchema = {
schema.maybe(
schema.object({
detector_index: schema.number(),
custom_rules: schema.arrayOf(
schema.maybe(
schema.object({
actions: schema.arrayOf(schema.string()),
conditions: schema.arrayOf(schema.any()),
scope: schema.maybe(schema.any()),
})
)
),
description: schema.maybe(schema.string()),
custom_rules: customRulesSchema,
})
)
)
Expand Down
75 changes: 75 additions & 0 deletions x-pack/legacy/plugins/ml/server/new_platform/job_service_schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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';

const analyzerSchema = {
tokenizer: schema.string(),
filter: schema.arrayOf(
schema.object({
type: schema.string(),
stopwords: schema.arrayOf(schema.maybe(schema.string())),
})
),
};

export const categorizationFieldExamplesSchema = {
indexPatternTitle: schema.string(),
query: schema.any(),
size: schema.number(),
field: schema.string(),
timeField: schema.maybe(schema.string()),
start: schema.number(),
end: schema.number(),
analyzer: schema.object(analyzerSchema),
};

export const chartSchema = {
indexPatternTitle: schema.string(),
timeField: schema.maybe(schema.string()),
start: schema.maybe(schema.number()),
end: schema.maybe(schema.number()),
intervalMs: schema.number(),
query: schema.any(),
aggFieldNamePairs: schema.arrayOf(schema.any()),
splitFieldName: schema.maybe(schema.nullable(schema.string())),
splitFieldValue: schema.maybe(schema.nullable(schema.string())),
};

export const datafeedIdsSchema = { datafeedIds: schema.arrayOf(schema.maybe(schema.string())) };

export const forceStartDatafeedSchema = {
datafeedIds: schema.arrayOf(schema.maybe(schema.string())),
start: schema.maybe(schema.number()),
end: schema.maybe(schema.number()),
};

export const jobIdsSchema = {
jobIds: schema.maybe(
schema.oneOf([schema.string(), schema.arrayOf(schema.maybe(schema.string()))])
),
};

export const jobsWithTimerangeSchema = { dateFormatTz: schema.maybe(schema.string()) };

export const lookBackProgressSchema = {
jobId: schema.string(),
start: schema.maybe(schema.number()),
end: schema.maybe(schema.number()),
};

export const topCategoriesSchema = { jobId: schema.string(), count: schema.number() };

export const updateGroupsSchema = {
jobs: schema.maybe(
schema.arrayOf(
schema.object({
job_id: schema.maybe(schema.string()),
groups: schema.arrayOf(schema.maybe(schema.string())),
})
)
),
};
Loading