import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { schema } from '@kbn/config-schema' ; import { wrapError } from '../client/error_wrapper' ; import { RouteInitialization } from '../types' ; import { anomalyDetectionJobSchema , anomalyDetectionUpdateJobSchema , jobIdSchema , getRecordsSchema , getBucketsSchema , getOverallBucketsSchema , getCategoriesSchema , forecastAnomalyDetector , getBucketParamsSchema , getModelSnapshotsSchema , updateModelSnapshotsSchema , updateModelSnapshotBodySchema , forceQuerySchema , jobResetQuerySchema , } from './schemas/anomaly_detectors_schema' ; /**
 * Routes for the anomaly detectors
 */ export function jobRoutes ( { router , routeGuard } ) { apiGroup api apiName apiDescription apiSuccess apiSuccess router . get ( { path : '/api/ml/anomaly_detectors' , validate : false , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , response } ) => { try { const { body } = await mlClient . getJobs ( ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . get ( { path : '/api/ml/anomaly_detectors/{jobId}' , validate : { params : jobIdSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { jobId } = request . params ; const { body } = await mlClient . getJobs ( { job_id : jobId } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSuccess apiSuccess router . get ( { path : '/api/ml/anomaly_detectors/_stats' , validate : false , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , response } ) => { try { const { body } = await mlClient . getJobStats ( ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . get ( { path : '/api/ml/anomaly_detectors/{jobId}/_stats' , validate : { params : jobIdSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { jobId } = request . params ; const { body } = await mlClient . getJobStats ( { job_id : jobId } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema apiSuccess router . put ( { path : '/api/ml/anomaly_detectors/{jobId}' , validate : { params : jobIdSchema , body : schema . object ( anomalyDetectionJobSchema ) , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { jobId } = request . params ; const { body } = await mlClient . putJob ( { job_id : jobId , body : request . body , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . post ( { path : '/api/ml/anomaly_detectors/{jobId}/_update' , validate : { params : jobIdSchema , body : anomalyDetectionUpdateJobSchema , } , options : { tags : [ 'access:ml:canUpdateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { jobId } = request . params ; const { body } = await mlClient . updateJob ( { job_id : jobId , body : request . body , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/anomaly_detectors/{jobId}/_open' , validate : { params : jobIdSchema , } , options : { tags : [ 'access:ml:canOpenJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { jobId } = request . params ; const { body } = await mlClient . openJob ( { job_id : jobId } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . post ( { path : '/api/ml/anomaly_detectors/{jobId}/_close' , validate : { params : jobIdSchema , query : forceQuerySchema , } , options : { tags : [ 'access:ml:canCloseJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const options = { job_id : request . params . jobId , } ; const force = request . query . force ; if ( force !== undefined ) { options . force = force ; } const { body } = await mlClient . closeJob ( options ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . post ( { path : '/api/ml/anomaly_detectors/{jobId}/_reset' , validate : { params : jobIdSchema , query : jobResetQuerySchema , } , options : { tags : [ 'access:ml:canCloseJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const options = { job_id : request . params . jobId , ... ( request . query . wait_for_completion !== undefined ? { wait_for_completion : request . query . wait_for_completion } : { } ) , } ; const { body } = await mlClient . resetJob ( options ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . delete ( { path : '/api/ml/anomaly_detectors/{jobId}' , validate : { params : jobIdSchema , query : forceQuerySchema , } , options : { tags : [ 'access:ml:canDeleteJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const options = { job_id : request . params . jobId , wait_for_completion : false , } ; const force = request . query . force ; if ( force !== undefined ) { options . force = force ; } const { body } = await mlClient . deleteJob ( options ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . post ( { path : '/api/ml/anomaly_detectors/_validate/detector' , validate : { body : schema . any ( ) , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { body } = await mlClient . validateDetector ( { body : request . body } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . post ( { path : '/api/ml/anomaly_detectors/{jobId}/_forecast' , validate : { params : jobIdSchema , body : forecastAnomalyDetector , } , options : { tags : [ 'access:ml:canForecastJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const jobId = request . params . jobId ; const duration = request . body . duration ; const { body } = await mlClient . forecast ( { job_id : jobId , body : { duration , } , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema apiSuccess apiSuccess router . post ( { path : '/api/ml/anomaly_detectors/{jobId}/results/records' , validate : { params : jobIdSchema , body : getRecordsSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { body } = await mlClient . getRecords ( { job_id : request . params . jobId , body : request . body , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema apiSuccess apiSuccess router . post ( { path : '/api/ml/anomaly_detectors/{jobId}/results/buckets/{timestamp?}' , validate : { params : getBucketParamsSchema , body : getBucketsSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { body } = await mlClient . getBuckets ( { job_id : request . params . jobId , timestamp : request . params . timestamp , body : request . body , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema apiSuccess apiSuccess router . post ( { path : '/api/ml/anomaly_detectors/{jobId}/results/overall_buckets' , validate : { params : jobIdSchema , body : getOverallBucketsSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { body } = await mlClient . getOverallBuckets ( { job_id : request . params . jobId , top_n : request . body . topN , bucket_span : request . body . bucketSpan , start : request . body . start !== undefined ? String ( request . body . start ) : undefined , end : request . body . end !== undefined ? String ( request . body . end ) : undefined , overall_score : request . body . overall_score ?? 0 , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . get ( { path : '/api/ml/anomaly_detectors/{jobId}/results/categories/{categoryId}' , validate : { params : getCategoriesSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { body } = await mlClient . getCategories ( { job_id : request . params . jobId , category_id : request . params . categoryId , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . get ( { path : '/api/ml/anomaly_detectors/{jobId}/model_snapshots' , validate : { params : getModelSnapshotsSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { body } = await mlClient . getModelSnapshots ( { job_id : request . params . jobId , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . get ( { path : '/api/ml/anomaly_detectors/{jobId}/model_snapshots/{snapshotId}' , validate : { params : getModelSnapshotsSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { body } = await mlClient . getModelSnapshots ( { job_id : request . params . jobId , snapshot_id : request . params . snapshotId , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . post ( { path : '/api/ml/anomaly_detectors/{jobId}/model_snapshots/{snapshotId}/_update' , validate : { params : updateModelSnapshotsSchema , body : updateModelSnapshotBodySchema , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { body } = await mlClient . updateModelSnapshot ( { job_id : request . params . jobId , snapshot_id : request . params . snapshotId , body : request . body , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . delete ( { path : '/api/ml/anomaly_detectors/{jobId}/model_snapshots/{snapshotId}' , validate : { params : updateModelSnapshotsSchema , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { body } = await mlClient . deleteModelSnapshot ( { job_id : request . params . jobId , snapshot_id : request . params . snapshotId , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }