From 3223e2e74d9ad270890e2ae15e3dae902b969216 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Fri, 3 Jul 2020 08:30:17 +0100 Subject: [PATCH] [ML] Changing shared module setup function parameters (#70589) * [ML] Changing shared module setup function parameters * changes based on review * adding comments for documentation Co-authored-by: Elastic Machine --- .../models/data_recognizer/data_recognizer.ts | 2 +- x-pack/plugins/ml/server/routes/modules.ts | 6 ++--- .../ml/server/routes/schemas/modules.ts | 24 +++++++++---------- x-pack/plugins/ml/server/shared.ts | 1 + .../shared_services/providers/modules.ts | 24 ++++++++++++++++--- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts index 1ed9df8da65dd..ae9a56f00a5c1 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts +++ b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts @@ -362,7 +362,7 @@ export class DataRecognizer { // takes a module config id, an optional jobPrefix and the request object // creates all of the jobs, datafeeds and savedObjects listed in the module config. // if any of the savedObjects already exist, they will not be overwritten. - async setupModuleItems( + async setup( moduleId: string, jobPrefix?: string, groups?: string[], diff --git a/x-pack/plugins/ml/server/routes/modules.ts b/x-pack/plugins/ml/server/routes/modules.ts index ade3d3eca90ea..88d24a1b86b6d 100644 --- a/x-pack/plugins/ml/server/routes/modules.ts +++ b/x-pack/plugins/ml/server/routes/modules.ts @@ -38,7 +38,7 @@ function getModule(context: RequestHandlerContext, moduleId: string) { } } -function saveModuleItems( +function setup( context: RequestHandlerContext, moduleId: string, prefix?: string, @@ -57,7 +57,7 @@ function saveModuleItems( context.ml!.mlClient.callAsCurrentUser, context.core.savedObjects.client ); - return dr.setupModuleItems( + return dr.setup( moduleId, prefix, groups, @@ -438,7 +438,7 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) { estimateModelMemory, } = request.body as TypeOf; - const result = await saveModuleItems( + const result = await setup( context, moduleId, prefix, diff --git a/x-pack/plugins/ml/server/routes/schemas/modules.ts b/x-pack/plugins/ml/server/routes/schemas/modules.ts index 23148c14c734e..e2b58cf2ce8f2 100644 --- a/x-pack/plugins/ml/server/routes/schemas/modules.ts +++ b/x-pack/plugins/ml/server/routes/schemas/modules.ts @@ -71,19 +71,19 @@ export const setupModuleBodySchema = schema.object({ estimateModelMemory: schema.maybe(schema.boolean()), }); -export const getModuleIdParamSchema = (optional = false) => { - const stringType = schema.string(); - return schema.object({ - /** - * ID of the module. - */ - moduleId: optional ? schema.maybe(stringType) : stringType, - }); -}; - -export const optionalModuleIdParamSchema = getModuleIdParamSchema(true); +export const optionalModuleIdParamSchema = schema.object({ + /** + * ID of the module. + */ + moduleId: schema.maybe(schema.string()), +}); -export const moduleIdParamSchema = getModuleIdParamSchema(false); +export const moduleIdParamSchema = schema.object({ + /** + * ID of the module. + */ + moduleId: schema.string(), +}); export const modulesIndexPatternTitleSchema = schema.object({ /** diff --git a/x-pack/plugins/ml/server/shared.ts b/x-pack/plugins/ml/server/shared.ts index 7b4b2a55c29f1..3fca8ea1ba047 100644 --- a/x-pack/plugins/ml/server/shared.ts +++ b/x-pack/plugins/ml/server/shared.ts @@ -7,3 +7,4 @@ export * from '../common/types/anomalies'; export * from '../common/types/anomaly_detection_jobs'; export * from './lib/capabilities/errors'; +export { ModuleSetupPayload } from './shared_services/providers/modules'; diff --git a/x-pack/plugins/ml/server/shared_services/providers/modules.ts b/x-pack/plugins/ml/server/shared_services/providers/modules.ts index d1666c5c1bf7b..27935fd6fe21d 100644 --- a/x-pack/plugins/ml/server/shared_services/providers/modules.ts +++ b/x-pack/plugins/ml/server/shared_services/providers/modules.ts @@ -5,8 +5,13 @@ */ import { LegacyAPICaller, KibanaRequest, SavedObjectsClientContract } from 'kibana/server'; +import { TypeOf } from '@kbn/config-schema'; import { DataRecognizer } from '../../models/data_recognizer'; import { SharedServicesChecks } from '../shared_services'; +import { moduleIdParamSchema, setupModuleBodySchema } from '../../routes/schemas/modules'; + +export type ModuleSetupPayload = TypeOf & + TypeOf; export interface ModulesProvider { modulesProvider( @@ -17,7 +22,7 @@ export interface ModulesProvider { recognize: DataRecognizer['findMatches']; getModule: DataRecognizer['getModule']; listModules: DataRecognizer['listModules']; - setupModuleItems: DataRecognizer['setupModuleItems']; + setup(payload: ModuleSetupPayload): ReturnType; }; } @@ -52,11 +57,24 @@ export function getModulesProvider({ return dr.listModules(); }, - async setupModuleItems(...args) { + async setup(payload: ModuleSetupPayload) { isFullLicense(); await hasMlCapabilities(['canCreateJob']); - return dr.setupModuleItems(...args); + return dr.setup( + payload.moduleId, + payload.prefix, + payload.groups, + payload.indexPatternName, + payload.query, + payload.useDedicatedIndex, + payload.startDatafeed, + payload.start, + payload.end, + payload.jobOverrides, + payload.datafeedOverrides, + payload.estimateModelMemory + ); }, }; },