import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { wrapError } from '../client/error_wrapper' ; import { RouteInitialization } from '../types' ; import { startDatafeedSchema , datafeedConfigSchema , datafeedIdSchema , deleteDatafeedQuerySchema , } from './schemas/datafeeds_schema' ; import { getAuthorizationHeader } from '../lib/request_authorization' ; /**
 * Routes for datafeed service
 */ export function dataFeedRoutes ( { router , routeGuard } ) { apiGroup api apiName apiDescription router . get ( { path : '/api/ml/datafeeds' , validate : false , options : { tags : [ 'access:ml:canGetDatafeeds' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , response } ) => { try { const { body } = await mlClient . getDatafeeds ( ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . get ( { path : '/api/ml/datafeeds/{datafeedId}' , validate : { params : datafeedIdSchema , } , options : { tags : [ 'access:ml:canGetDatafeeds' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const datafeedId = request . params . datafeedId ; const { body } = await mlClient . getDatafeeds ( { datafeed_id : datafeedId } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription router . get ( { path : '/api/ml/datafeeds/_stats' , validate : false , options : { tags : [ 'access:ml:canGetDatafeeds' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , response } ) => { try { const { body } = await mlClient . getDatafeedStats ( ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . get ( { path : '/api/ml/datafeeds/{datafeedId}/_stats' , validate : { params : datafeedIdSchema , } , options : { tags : [ 'access:ml:canGetDatafeeds' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const datafeedId = request . params . datafeedId ; const { body } = await mlClient . getDatafeedStats ( { datafeed_id : datafeedId , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . put ( { path : '/api/ml/datafeeds/{datafeedId}' , validate : { params : datafeedIdSchema , body : datafeedConfigSchema , } , options : { tags : [ 'access:ml:canCreateDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const datafeedId = request . params . datafeedId ; const { body } = await mlClient . putDatafeed ( { datafeed_id : datafeedId , body : request . body , } , getAuthorizationHeader ( request ) ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . post ( { path : '/api/ml/datafeeds/{datafeedId}/_update' , validate : { params : datafeedIdSchema , body : datafeedConfigSchema , } , options : { tags : [ 'access:ml:canUpdateDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const datafeedId = request . params . datafeedId ; const { body } = await mlClient . updateDatafeed ( { datafeed_id : datafeedId , body : request . body , } , getAuthorizationHeader ( request ) ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . delete ( { path : '/api/ml/datafeeds/{datafeedId}' , validate : { params : datafeedIdSchema , query : deleteDatafeedQuerySchema , } , options : { tags : [ 'access:ml:canDeleteDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const options = { datafeed_id : request . params . datafeedId , } ; const force = request . query . force ; if ( force !== undefined ) { options . force = force ; } const { body } = await mlClient . deleteDatafeed ( options ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . post ( { path : '/api/ml/datafeeds/{datafeedId}/_start' , validate : { params : datafeedIdSchema , body : startDatafeedSchema , } , options : { tags : [ 'access:ml:canStartStopDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const datafeedId = request . params . datafeedId ; const { start , end } = request . body ; const { body } = await mlClient . startDatafeed ( { datafeed_id : datafeedId , body : { start : start !== undefined ? String ( start ) : undefined , end : end !== undefined ? String ( end ) : undefined , } , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/datafeeds/{datafeedId}/_stop' , validate : { params : datafeedIdSchema , } , options : { tags : [ 'access:ml:canStartStopDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const datafeedId = request . params . datafeedId ; const { body } = await mlClient . stopDatafeed ( { datafeed_id : datafeedId , } ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . get ( { path : '/api/ml/datafeeds/{datafeedId}/_preview' , validate : { params : datafeedIdSchema , } , options : { tags : [ 'access:ml:canPreviewDatafeed' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const datafeedId = request . params . datafeedId ; const { body } = await mlClient . previewDatafeed ( { datafeed_id : datafeedId , } , getAuthorizationHeader ( request ) ) ; return response . ok ( { body , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }