import { KibanaRequest , SavedObjectsClientContract } from 'kibana/server' ; import { Job , JobStats , Datafeed , DatafeedStats , } from '../../../common/types/anomaly_detection_jobs' ; import { GetGuards } from '../shared_services' ; export interface AnomalyDetectorsProvider { anomalyDetectorsProvider ( request , savedObjectsClient ) : { jobs ( jobId ? ) : Promise < { count : number ; jobs : Job [ ] ; } > ; jobStats ( jobId ? ) : Promise < { count : number ; jobs : JobStats [ ] ; } > ; datafeeds ( datafeedId ? ) : Promise < { count : number ; datafeeds : Datafeed [ ] ; } > ; datafeedStats ( datafeedId ? ) : Promise < { count : number ; datafeeds : DatafeedStats [ ] ; } > ; } ; } export function getAnomalyDetectorsProvider ( getGuards ) { return { anomalyDetectorsProvider ( request , savedObjectsClient ) { return { async jobs ( jobId ? ) { return await getGuards ( request , savedObjectsClient ) . isFullLicense ( ) . hasMlCapabilities ( [ 'canGetJobs' ] ) . ok ( async ( { mlClient } ) => { const { body } = await mlClient . getJobs < { count : number ; jobs : Job [ ] ; } > ( jobId !== undefined ? { job_id : jobId } : undefined ) ; return body ; } ) ; } , async jobStats ( jobId ? ) { return await getGuards ( request , savedObjectsClient ) . isFullLicense ( ) . hasMlCapabilities ( [ 'canGetJobs' ] ) . ok ( async ( { mlClient } ) => { const { body } = await mlClient . getJobStats < { count : number ; jobs : JobStats [ ] ; } > ( jobId !== undefined ? { job_id : jobId } : undefined ) ; return body ; } ) ; } , async datafeeds ( datafeedId ? ) { return await getGuards ( request , savedObjectsClient ) . isFullLicense ( ) . hasMlCapabilities ( [ 'canGetDatafeeds' ] ) . ok ( async ( { mlClient } ) => { const { body } = await mlClient . getDatafeeds < { count : number ; datafeeds : Datafeed [ ] ; } > ( datafeedId !== undefined ? { datafeed_id : datafeedId } : undefined ) ; return body ; } ) ; } , async datafeedStats ( datafeedId ? ) { return await getGuards ( request , savedObjectsClient ) . isFullLicense ( ) . hasMlCapabilities ( [ 'canGetDatafeeds' ] ) . ok ( async ( { mlClient } ) => { const { body } = await mlClient . getDatafeedStats < { count : number ; datafeeds : DatafeedStats [ ] ; } > ( datafeedId !== undefined ? { datafeed_id : datafeedId } : undefined ) ; return body ; } ) ; } , } ; } , } ; }