import { RouteInitialization } from '../types' ; import { wrapError } from '../client/error_wrapper' ; import { getInferenceQuerySchema , modelIdSchema , optionalModelIdSchema , } from './schemas/inference_schema' ; import { modelsProvider } from '../models/data_frame_analytics' ; import { TrainedModelConfigResponse } from '../../common/types/trained_models' ; import { memoryOverviewServiceProvider } from '../models/memory_overview' ; import { mlLog } from '../lib/log' ; import { forceQuerySchema } from './schemas/anomaly_detectors_schema' ; export function trainedModelsRoutes ( { router , routeGuard } ) { apiGroup api apiName apiDescription router . get ( { path : '/api/ml/trained_models/{modelId?}' , validate : { params : optionalModelIdSchema , query : getInferenceQuerySchema , } , options : { tags : [ 'access:ml:canGetDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { modelId } = request . params ; const { with_pipelines : withPipelines , ... query } = request . query ; const { body } = await mlClient . getTrainedModels ( { size : 1000 , ... query , ... ( modelId ? { model_id : modelId } : { } ) , } ) ; const result = body . trained_model_configs as TrainedModelConfigResponse [ ] ; try { if ( withPipelines ) { const modelIdsAndAliases = Array . from ( new Set ( result . map ( ( { model_id : id , metadata } ) => { return [ id , ... ( metadata ?. model_aliases ?? [ ] ) ] ; } ) . flat ( ) ) ) ; const pipelinesResponse = await modelsProvider ( client , mlClient ) . getModelsPipelines ( modelIdsAndAliases ) ; for ( const model of result ) { model . pipelines = { ... ( pipelinesResponse . get ( model . model_id ) ?? { } ) , ... ( model . metadata ?. model_aliases ?? [ ] ) . reduce ( ( acc , alias ) => { return { ... acc , ... ( pipelinesResponse . get ( alias ) ?? { } ) , } ; } , { } ) , } ; } } } catch ( e ) { mlLog . error ( e ) ; } return response . ok ( { body : result , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/trained_models/{modelId}/_stats' , validate : { params : modelIdSchema , } , options : { tags : [ 'access:ml:canGetDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { modelId } = request . params ; const { body } = await mlClient . getTrainedModelsStats ( { ... ( modelId ? { model_id : modelId } : { } ) , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/trained_models/{modelId}/pipelines' , validate : { params : modelIdSchema , } , options : { tags : [ 'access:ml:canGetDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , request , mlClient , response } ) => { try { const { modelId } = request . params ; const result = await modelsProvider ( client , mlClient ) . getModelsPipelines ( modelId . split ( ',' ) ) ; return response . ok ( { body : [ ... result ] . map ( ( [ id , pipelines ] ) => ( { model_id : id , pipelines } ) ) , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . delete ( { path : '/api/ml/trained_models/{modelId}' , validate : { params : modelIdSchema , } , options : { tags : [ 'access:ml:canDeleteDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { modelId } = request . params ; const { body } = await mlClient . deleteTrainedModel ( { model_id : modelId , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/trained_models/nodes_overview' , validate : { } , options : { tags : [ 'access:ml:canViewMlNodes' , 'access:ml:canGetDataFrameAnalytics' , 'access:ml:canGetJobs' , ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const memoryOverviewService = memoryOverviewServiceProvider ( mlClient ) ; const result = await modelsProvider ( client , mlClient , memoryOverviewService ) . getNodesOverview ( ) ; return response . ok ( { body : result , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . post ( { path : '/api/ml/trained_models/{modelId}/deployment/_start' , validate : { params : modelIdSchema , } , options : { tags : [ 'access:ml:canGetDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { modelId } = request . params ; const { body } = await mlClient . startTrainedModelDeployment ( { model_id : modelId , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . post ( { path : '/api/ml/trained_models/{modelId}/deployment/_stop' , validate : { params : modelIdSchema , query : forceQuerySchema , } , options : { tags : [ 'access:ml:canGetDataFrameAnalytics' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { modelId } = request . params ; const { body } = await mlClient . stopTrainedModelDeployment ( { model_id : modelId , force : request . query . force ?? false , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }