import type { KibanaRequest , SavedObjectsClientContract } from 'kibana/server' ; import type { TypeOf } from '@kbn/config-schema' ; import type { PluginStart as DataViewsPluginStart } from '../../../../../../src/plugins/data_views/server' ; import type { GetGuards } from '../shared_services' ; import { DataRecognizer , dataRecognizerFactory } from '../../models/data_recognizer' ; import { moduleIdParamSchema , setupModuleBodySchema } from '../../routes/schemas/modules' ; export type ModuleSetupPayload = TypeOf < typeof moduleIdParamSchema > & TypeOf < typeof setupModuleBodySchema > ; export interface ModulesProvider { modulesProvider ( request , savedObjectsClient ) : { recognize : DataRecognizer [ 'findMatches' ] ; getModule : DataRecognizer [ 'getModule' ] ; listModules : DataRecognizer [ 'listModules' ] ; setup ( payload ) : ReturnType < DataRecognizer [ 'setup' ] > ; } ; } export function getModulesProvider ( getGuards , getDataViews ) { return { modulesProvider ( request , savedObjectsClient ) { return { async recognize ( ... args ) { return await getGuards ( request , savedObjectsClient ) . isFullLicense ( ) . hasMlCapabilities ( [ 'canGetJobs' ] ) . ok ( async ( { scopedClient , mlClient , jobSavedObjectService , getDataViewsService } ) => { const dataViewsService = await getDataViewsService ( ) ; const dr = dataRecognizerFactory ( scopedClient , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request ) ; return dr . findMatches ( ... args ) ; } ) ; } , async getModule ( moduleId ) { return await getGuards ( request , savedObjectsClient ) . isFullLicense ( ) . hasMlCapabilities ( [ 'canGetJobs' ] ) . ok ( async ( { scopedClient , mlClient , jobSavedObjectService , getDataViewsService } ) => { const dataViewsService = await getDataViewsService ( ) ; const dr = dataRecognizerFactory ( scopedClient , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request ) ; return dr . getModule ( moduleId ) ; } ) ; } , async listModules ( ) { return await getGuards ( request , savedObjectsClient ) . isFullLicense ( ) . hasMlCapabilities ( [ 'canGetJobs' ] ) . ok ( async ( { scopedClient , mlClient , jobSavedObjectService , getDataViewsService } ) => { const dataViewsService = await getDataViewsService ( ) ; const dr = dataRecognizerFactory ( scopedClient , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request ) ; return dr . listModules ( ) ; } ) ; } , async setup ( payload ) { return await getGuards ( request , savedObjectsClient ) . isFullLicense ( ) . hasMlCapabilities ( [ 'canCreateJob' ] ) . ok ( async ( { scopedClient , mlClient , jobSavedObjectService , getDataViewsService } ) => { const dataViewsService = await getDataViewsService ( ) ; const dr = dataRecognizerFactory ( scopedClient , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request ) ; 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 ) ; } ) ; } , } ; } , } ; }