import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { KibanaRequest , SavedObjectsClientContract } from 'kibana/server' ; import { MlLicense } from '../../../common/license' ; import { CloudSetup } from '../../../../cloud/server' ; import { spacesUtilsProvider } from '../../lib/spaces_utils' ; import { SpacesPluginStart } from '../../../../spaces/server' ; import { capabilitiesProvider } from '../../lib/capabilities' ; import { MlInfoResponse } from '../../../common/types/ml_server_info' ; import { MlCapabilitiesResponse , ResolveMlCapabilities } from '../../../common/types/capabilities' ; import { GetGuards } from '../shared_services' ; export interface MlSystemProvider { mlSystemProvider ( request , savedObjectsClient ) : { mlCapabilities ( ) : Promise < MlCapabilitiesResponse > ; mlInfo ( ) : Promise < MlInfoResponse > ; mlAnomalySearch < T > ( searchParams , jobIds ) : Promise < estypes . SearchResponse < T > > ; } ; } export function getMlSystemProvider ( getGuards , mlLicense , getSpaces , cloud , resolveMlCapabilities ) { return { mlSystemProvider ( request , savedObjectsClient ) { return { async mlCapabilities ( ) { return await getGuards ( request , savedObjectsClient ) . isMinimumLicense ( ) . ok ( async ( { mlClient } ) => { const { isMlEnabledInSpace } = spacesUtilsProvider ( getSpaces , request ) ; const mlCapabilities = await resolveMlCapabilities ( request ) ; if ( mlCapabilities === null ) { throw new Error ( 'mlCapabilities is not defined' ) ; } const { getCapabilities } = capabilitiesProvider ( mlClient , mlCapabilities , mlLicense , isMlEnabledInSpace ) ; return getCapabilities ( ) ; } ) ; } , async mlInfo ( ) { return await getGuards ( request , savedObjectsClient ) . isMinimumLicense ( ) . ok ( async ( { mlClient } ) => { const { body : info } = await mlClient . info < MlInfoResponse > ( ) ; const cloudId = cloud && cloud . cloudId ; return { ... info , cloudId , } ; } ) ; } , async mlAnomalySearch < T > ( searchParams , jobIds ) { return await getGuards ( request , savedObjectsClient ) . isFullLicense ( ) . hasMlCapabilities ( [ 'canAccessML' ] ) . ok ( async ( { mlClient } ) => { const { body } = await mlClient . anomalySearch < T > ( searchParams , jobIds ) ; return body ; } ) ; } , } ; } , } ; }