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 @@ -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, request) {
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, request),
...newJobChartsProvider(callAsCurrentUser, request),
...categorizationExamplesProvider(callAsCurrentUser, request),
...topCategoriesProvider(callAsCurrentUser, request),
};
}
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 @@ -5,6 +5,7 @@
*/

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';

Expand All @@ -15,9 +16,15 @@ interface NewJobCapsResponse {
export function newJobCapsProvider(callWithRequest: any, request: Request) {
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
69 changes: 69 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,69 @@
/*
* 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 catFieldExamplesSchema = {
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
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 = {
job_id: schema.string(),
groups: schema.arrayOf(schema.maybe(schema.string())),
};
21 changes: 20 additions & 1 deletion x-pack/legacy/plugins/ml/server/routes/apidoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@
"Annotations",
"GetAnnotations",
"IndexAnnotations",
"DeleteAnnotation"
"DeleteAnnotation",
"JobService",
"ForceStartDatafeeds",
"StopDatafeeds",
"DeleteJobs",
"CloseJobs",
"JobsSummary",
"JobsWithTimerange",
"CreateFullJobsList",
"GetAllGroups",
"UpdateGroups",
"DeletingJobTasks",
"JobsExist",
"NewJobCaps",
"NewJobLineChart",
"NewJobPopulationChart",
"GetAllJobAndGroupIds",
"GetLookBackProgress",
"ValidateCategoryExamples",
"TopCategories"
]
}
12 changes: 6 additions & 6 deletions x-pack/legacy/plugins/ml/server/routes/calendars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ import { calendarSchema } from '../new_platform/calendars_schema';
import { CalendarManager, Calendar, FormCalendar } from '../models/calendar';

function getAllCalendars(context: RequestHandlerContext) {
const cal = new CalendarManager(false, context);
const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser);
return cal.getAllCalendars();
}

function getCalendar(context: RequestHandlerContext, calendarId: string) {
const cal = new CalendarManager(false, context);
const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser);
return cal.getCalendar(calendarId);
}

function newCalendar(context: RequestHandlerContext, calendar: FormCalendar) {
const cal = new CalendarManager(false, context);
const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser);
return cal.newCalendar(calendar);
}

function updateCalendar(context: RequestHandlerContext, calendarId: string, calendar: Calendar) {
const cal = new CalendarManager(false, context);
const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser);
return cal.updateCalendar(calendarId, calendar);
}

function deleteCalendar(context: RequestHandlerContext, calendarId: string) {
const cal = new CalendarManager(false, context);
const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser);
return cal.deleteCalendar(calendarId);
}

function getCalendarsByIds(context: RequestHandlerContext, calendarIds: string) {
const cal = new CalendarManager(false, context);
const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser);
return cal.getCalendarsByIds(calendarIds);
}

Expand Down
Loading