import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { schema } from '@kbn/config-schema' ; import { wrapError } from '../client/error_wrapper' ; import type { RouteInitialization } from '../types' ; import { categorizationFieldExamplesSchema , basicChartSchema , populationChartSchema , datafeedIdsSchema , forceStartDatafeedSchema , jobIdsSchema , optionalJobIdsSchema , jobsWithTimerangeSchema , lookBackProgressSchema , topCategoriesSchema , updateGroupsSchema , revertModelSnapshotSchema , jobsExistSchema , datafeedPreviewSchema , bulkCreateSchema , } from './schemas/job_service_schema' ; import { jobIdSchema } from './schemas/anomaly_detectors_schema' ; import { jobServiceProvider } from '../models/job_service' ; import { categorizationExamplesProvider } from '../models/job_service/new_job' ; import { getAuthorizationHeader } from '../lib/request_authorization' ; import type { Datafeed , Job } from '../../common/types/anomaly_detection_jobs' ; /**
 * Routes for job service
 */ export function jobServiceRoutes ( { router , routeGuard } ) { apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/force_start_datafeeds' , validate : { body : forceStartDatafeedSchema , } , options : { tags : [ 'access:ml:canStartStopDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { forceStartDatafeeds } = jobServiceProvider ( client , mlClient ) ; const { datafeedIds , start , end } = request . body ; const resp = await forceStartDatafeeds ( datafeedIds , start , end ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/stop_datafeeds' , validate : { body : datafeedIdsSchema , } , options : { tags : [ 'access:ml:canStartStopDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { stopDatafeeds } = jobServiceProvider ( client , mlClient ) ; const { datafeedIds } = request . body ; const resp = await stopDatafeeds ( datafeedIds ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/delete_jobs' , validate : { body : jobIdsSchema , } , options : { tags : [ 'access:ml:canDeleteJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { deleteJobs } = jobServiceProvider ( client , mlClient ) ; const { jobIds } = request . body ; const resp = await deleteJobs ( jobIds ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/close_jobs' , validate : { body : jobIdsSchema , } , options : { tags : [ 'access:ml:canCloseJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { closeJobs } = jobServiceProvider ( client , mlClient ) ; const { jobIds } = request . body ; const resp = await closeJobs ( jobIds ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/reset_jobs' , validate : { body : jobIdsSchema , } , options : { tags : [ 'access:ml:canResetJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { resetJobs } = jobServiceProvider ( client , mlClient ) ; const { jobIds } = request . body ; const resp = await resetJobs ( jobIds ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/force_stop_and_close_job' , validate : { body : jobIdSchema , } , options : { tags : [ 'access:ml:canCloseJob' , 'access:ml:canStartStopDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { forceStopAndCloseJob } = jobServiceProvider ( client , mlClient ) ; const { jobId } = request . body ; const resp = await forceStopAndCloseJob ( jobId ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSuccess router . post ( { path : '/api/ml/jobs/jobs_summary' , validate : { body : optionalJobIdsSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response , context } ) => { try { const { jobsSummary } = jobServiceProvider ( client , mlClient , context . alerting ?. getRulesClient ( ) ) ; const { jobIds } = request . body ; const resp = await jobsSummary ( jobIds ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/jobs_with_time_range' , validate : { body : jobsWithTimerangeSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , response } ) => { try { const { jobsWithTimerange } = jobServiceProvider ( client , mlClient ) ; const resp = await jobsWithTimerange ( ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/job_for_cloning' , validate : { body : jobIdSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { getJobForCloning } = jobServiceProvider ( client , mlClient ) ; const { jobId } = request . body ; const resp = await getJobForCloning ( jobId ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/jobs' , validate : { body : optionalJobIdsSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response , context } ) => { try { const { createFullJobsList } = jobServiceProvider ( client , mlClient , context . alerting ?. getRulesClient ( ) ) ; const { jobIds } = request . body ; const resp = await createFullJobsList ( jobIds ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/jobs/groups' , validate : false , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , response } ) => { try { const { getAllGroups } = jobServiceProvider ( client , mlClient ) ; const resp = await getAllGroups ( ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/update_groups' , validate : { body : updateGroupsSchema , } , options : { tags : [ 'access:ml:canUpdateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { updateGroups } = jobServiceProvider ( client , mlClient ) ; const { jobs } = request . body ; const resp = await updateGroups ( jobs ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/jobs/blocking_jobs_tasks' , validate : false , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , response } ) => { try { const { blockingJobTasks } = jobServiceProvider ( client , mlClient ) ; const resp = await blockingJobTasks ( ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/jobs_exist' , validate : { body : jobsExistSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { jobsExist } = jobServiceProvider ( client , mlClient ) ; const { jobIds , allSpaces } = request . body ; const resp = await jobsExist ( jobIds , allSpaces ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/jobs/new_job_caps/{indexPattern}' , validate : { params : schema . object ( { indexPattern : schema . string ( ) } ) , query : schema . maybe ( schema . object ( { rollup : schema . maybe ( schema . string ( ) ) } ) ) , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response , getDataViewsService } ) => { try { const { indexPattern } = request . params ; const isRollup = request . query ?. rollup === 'true' ; const { newJobCaps } = jobServiceProvider ( client , mlClient ) ; const dataViewsService = await getDataViewsService ( ) ; const resp = await newJobCaps ( indexPattern , isRollup , dataViewsService ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/new_job_line_chart' , validate : { body : schema . object ( basicChartSchema ) , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { indexPatternTitle , timeField , start , end , intervalMs , query , aggFieldNamePairs , splitFieldName , splitFieldValue , runtimeMappings , indicesOptions , } = request . body ; const { newJobLineChart } = jobServiceProvider ( client , mlClient ) ; const resp = await newJobLineChart ( indexPatternTitle , timeField , start , end , intervalMs , query , aggFieldNamePairs , splitFieldName , splitFieldValue , runtimeMappings , indicesOptions ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/new_job_population_chart' , validate : { body : schema . object ( populationChartSchema ) , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { indexPatternTitle , timeField , start , end , intervalMs , query , aggFieldNamePairs , splitFieldName , runtimeMappings , indicesOptions , } = request . body ; const { newJobPopulationChart } = jobServiceProvider ( client , mlClient ) ; const resp = await newJobPopulationChart ( indexPatternTitle , timeField , start , end , intervalMs , query , aggFieldNamePairs , splitFieldName , runtimeMappings , indicesOptions ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/jobs/all_jobs_and_group_ids' , validate : false , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , response } ) => { try { const { getAllJobAndGroupIds } = jobServiceProvider ( client , mlClient ) ; const resp = await getAllJobAndGroupIds ( ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/look_back_progress' , validate : { body : schema . object ( lookBackProgressSchema ) , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { getLookBackProgress } = jobServiceProvider ( client , mlClient ) ; const { jobId , start , end } = request . body ; const resp = await getLookBackProgress ( jobId , start , end ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/categorization_field_examples' , validate : { body : schema . object ( categorizationFieldExamplesSchema ) , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { validateCategoryExamples } = categorizationExamplesProvider ( client ) ; const { indexPatternTitle , timeField , query , size , field , start , end , analyzer , runtimeMappings , indicesOptions , } = request . body ; const resp = await validateCategoryExamples ( indexPatternTitle , query , size , field , timeField , start , end , analyzer , runtimeMappings , indicesOptions ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/top_categories' , validate : { body : schema . object ( topCategoriesSchema ) , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { topCategories } = jobServiceProvider ( client , mlClient ) ; const { jobId , count } = request . body ; const resp = await topCategories ( jobId , count ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/datafeed_preview' , validate : { body : datafeedPreviewSchema , } , options : { tags : [ 'access:ml:canPreviewDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { datafeedId , job , datafeed } = request . body ; const payload = datafeedId !== undefined ? { datafeed_id : datafeedId , } : ( { body : { job_config : job , datafeed_config : datafeed , } , } as estypes . MlPreviewDatafeedRequest ) ; const { body } = await mlClient . previewDatafeed ( payload , getAuthorizationHeader ( request ) ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/revert_model_snapshot' , validate : { body : revertModelSnapshotSchema , } , options : { tags : [ 'access:ml:canCreateJob' , 'access:ml:canStartStopDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { revertModelSnapshot } = jobServiceProvider ( client , mlClient ) ; const { jobId , snapshotId , replay , end , deleteInterveningResults , calendarEvents } = request . body ; const resp = await revertModelSnapshot ( jobId , snapshotId , replay , end , deleteInterveningResults , calendarEvents ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/jobs/bulk_create' , validate : { body : bulkCreateSchema , } , options : { tags : [ 'access:ml:canPreviewDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const bulkJobs = request . body ; const { bulkCreate } = jobServiceProvider ( client , mlClient ) ; const jobs = ( Array . isArray ( bulkJobs ) ? bulkJobs : [ bulkJobs ] ) as Array < { job : Job ; datafeed : Datafeed ; } > ; const body = await bulkCreate ( jobs , getAuthorizationHeader ( request ) ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }