import Boom from '@hapi/boom' ; import { IScopedClusterClient } from 'kibana/server' ; import { TypeOf } from '@kbn/config-schema' ; import type { AnalysisConfig , Datafeed } from '../../common/types/anomaly_detection_jobs' ; import { wrapError } from '../client/error_wrapper' ; import type { RouteInitialization } from '../types' ; import { estimateBucketSpanSchema , modelMemoryLimitSchema , validateCardinalitySchema , validateJobSchema , validateDatafeedPreviewSchema , } from './schemas/job_validation_schema' ; import { estimateBucketSpanFactory } from '../models/bucket_span_estimator' ; import { calculateModelMemoryLimitProvider } from '../models/calculate_model_memory_limit' ; import { validateJob , validateCardinality , validateDatafeedPreview , } from '../models/job_validation' ; import { getAuthorizationHeader } from '../lib/request_authorization' ; import type { MlClient } from '../lib/ml_client' ; import { CombinedJob } from '../../common/types/anomaly_detection_jobs' ; type CalculateModelMemoryLimitPayload = TypeOf < typeof modelMemoryLimitSchema > ; /**
 * Routes for job validation
 */ export function jobValidationRoutes ( { router , mlLicense , routeGuard } ) { function calculateModelMemoryLimit ( client , mlClient , payload ) { const { datafeedConfig , analysisConfig , indexPattern , query , timeFieldName , earliestMs , latestMs , } = payload ; return calculateModelMemoryLimitProvider ( client , mlClient ) ( analysisConfig as AnalysisConfig , indexPattern , query , timeFieldName , earliestMs , latestMs , undefined , datafeedConfig as Datafeed ) ; } apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/validate/estimate_bucket_span' , validate : { body : estimateBucketSpanSchema , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , request , response } ) => { try { let errorResp ; const resp = await estimateBucketSpanFactory ( client ) ( request . body ) . catch ( ( error ) => { errorResp = { error : true , message : error , } ; } ) ; return response . ok ( { body : errorResp !== undefined ? errorResp : resp , } ) ; } catch ( e ) { throw Boom . badRequest ( e ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSuccess router . post ( { path : '/api/ml/validate/calculate_model_memory_limit' , validate : { body : modelMemoryLimitSchema , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const resp = await calculateModelMemoryLimit ( client , mlClient , request . body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/validate/cardinality' , validate : { body : validateCardinalitySchema , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , request , response } ) => { try { const resp = await validateCardinality ( client , request . body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/validate/job' , validate : { body : validateJobSchema , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const resp = await validateJob ( client , mlClient , request . body , getAuthorizationHeader ( request ) , mlLicense . isSecurityEnabled ( ) === false ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/validate/datafeed_preview' , validate : { body : validateDatafeedPreviewSchema , } , options : { tags : [ 'access:ml:canCreateJob' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const resp = await validateDatafeedPreview ( mlClient , getAuthorizationHeader ( request ) , request . body . job as CombinedJob ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }