import type { TypeOf } from '@kbn/config-schema' ; import type { IScopedClusterClient , KibanaRequest , SavedObjectsClientContract , } from 'kibana/server' ; import type { DataViewsService } from '../../../../../src/plugins/data_views/common' ; import type { DatafeedOverride , JobOverride } from '../../common/types/modules' ; import { wrapError } from '../client/error_wrapper' ; import { dataRecognizerFactory } from '../models/data_recognizer' ; import { moduleIdParamSchema , optionalModuleIdParamSchema , modulesIndexPatternTitleSchema , setupModuleBodySchema , } from './schemas/modules' ; import type { RouteInitialization } from '../types' ; import type { MlClient } from '../lib/ml_client' ; import type { JobSavedObjectService } from '../saved_objects' ; function recognize ( client , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request , indexPatternTitle ) { const dr = dataRecognizerFactory ( client , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request ) ; return dr . findMatches ( indexPatternTitle ) ; } function getModule ( client , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request , moduleId ? ) { const dr = dataRecognizerFactory ( client , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request ) ; if ( moduleId === undefined ) { return dr . listModules ( ) ; } else { return dr . getModule ( moduleId ) ; } } function setup ( client , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request , moduleId , prefix ? , groups ? , indexPatternName ? , query ? , useDedicatedIndex ? , startDatafeed ? , start ? , end ? , jobOverrides ? , datafeedOverrides ? , estimateModelMemory ? , applyToAllSpaces ? ) { const dr = dataRecognizerFactory ( client , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request ) ; return dr . setup ( moduleId , prefix , groups , indexPatternName , query , useDedicatedIndex , startDatafeed , start , end , jobOverrides , datafeedOverrides , estimateModelMemory , applyToAllSpaces ) ; } function dataRecognizerJobsExist ( client , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request , moduleId ) { const dr = dataRecognizerFactory ( client , mlClient , savedObjectsClient , dataViewsService , jobSavedObjectService , request ) ; return dr . dataRecognizerJobsExist ( moduleId ) ; } /**
 * Recognizer routes.
 */ export function dataRecognizer ( { router , routeGuard } ) { apiGroup api apiName apiDescription apiSchema apiSuccess apiSuccessExample router . get ( { path : '/api/ml/modules/recognize/{indexPatternTitle}' , validate : { params : modulesIndexPatternTitleSchema , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response , context , jobSavedObjectService , getDataViewsService , } ) => { try { const { indexPatternTitle } = request . params ; const dataViewService = await getDataViewsService ( ) ; const results = await recognize ( client , mlClient , context . core . savedObjects . client , dataViewService , jobSavedObjectService , request , indexPatternTitle ) ; return response . ok ( { body : results } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSuccess apiSuccess apiSuccessExample router . get ( { path : '/api/ml/modules/get_module/{moduleId?}' , validate : { params : optionalModuleIdParamSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response , context , jobSavedObjectService , getDataViewsService , } ) => { try { let { moduleId } = request . params ; if ( moduleId === '' ) { moduleId = undefined ; } const dataViewService = await getDataViewsService ( ) ; const results = await getModule ( client , mlClient , context . core . savedObjects . client , dataViewService , jobSavedObjectService , request , moduleId ) ; return response . ok ( { body : results } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema apiParamExample apiParamExample apiParamExample apiParamExample apiSuccess apiSuccessExample apiSuccessExample router . post ( { path : '/api/ml/modules/setup/{moduleId}' , validate : { params : moduleIdParamSchema , body : setupModuleBodySchema , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response , context , jobSavedObjectService , getDataViewsService , } ) => { try { const { moduleId } = request . params ; const { prefix , groups , indexPatternName , query , useDedicatedIndex , startDatafeed , start , end , jobOverrides , datafeedOverrides , estimateModelMemory , applyToAllSpaces , } = request . body as TypeOf < typeof setupModuleBodySchema > ; const dataViewService = await getDataViewsService ( ) ; const result = await setup ( client , mlClient , context . core . savedObjects . client , dataViewService , jobSavedObjectService , request , moduleId , prefix , groups , indexPatternName , query , useDedicatedIndex , startDatafeed , start , end , jobOverrides , datafeedOverrides , estimateModelMemory , applyToAllSpaces ) ; return response . ok ( { body : result } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSuccess apiSuccess apiSuccessExample router . get ( { path : '/api/ml/modules/jobs_exist/{moduleId}' , validate : { params : moduleIdParamSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response , context , jobSavedObjectService , getDataViewsService , } ) => { try { const { moduleId } = request . params ; const dataViewService = await getDataViewsService ( ) ; const result = await dataRecognizerJobsExist ( client , mlClient , context . core . savedObjects . client , dataViewService , jobSavedObjectService , request , moduleId ) ; return response . ok ( { body : result } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }