Skip to content

Commit

Permalink
[ML] Changing shared module setup function parameters (elastic#70589) (
Browse files Browse the repository at this point in the history
…elastic#70690)

* [ML] Changing shared module setup function parameters

* changes based on review

* adding comments for documentation

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
jgowdyelastic and elasticmachine authored Jul 3, 2020
1 parent d9306a9 commit f7fe5f2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/ml/server/routes/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function getModule(context: RequestHandlerContext, moduleId: string) {
}
}

function saveModuleItems(
function setup(
context: RequestHandlerContext,
moduleId: string,
prefix?: string,
Expand All @@ -57,7 +57,7 @@ function saveModuleItems(
context.ml!.mlClient.callAsCurrentUser,
context.core.savedObjects.client
);
return dr.setupModuleItems(
return dr.setup(
moduleId,
prefix,
groups,
Expand Down Expand Up @@ -438,7 +438,7 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) {
estimateModelMemory,
} = request.body as TypeOf<typeof setupModuleBodySchema>;

const result = await saveModuleItems(
const result = await setup(
context,
moduleId,
prefix,
Expand Down
24 changes: 12 additions & 12 deletions x-pack/plugins/ml/server/routes/schemas/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
/**
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/ml/server/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
24 changes: 21 additions & 3 deletions x-pack/plugins/ml/server/shared_services/providers/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 moduleIdParamSchema> &
TypeOf<typeof setupModuleBodySchema>;

export interface ModulesProvider {
modulesProvider(
Expand All @@ -17,7 +22,7 @@ export interface ModulesProvider {
recognize: DataRecognizer['findMatches'];
getModule: DataRecognizer['getModule'];
listModules: DataRecognizer['listModules'];
setupModuleItems: DataRecognizer['setupModuleItems'];
setup(payload: ModuleSetupPayload): ReturnType<DataRecognizer['setup']>;
};
}

Expand Down Expand Up @@ -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
);
},
};
},
Expand Down

0 comments on commit f7fe5f2

Please sign in to comment.