import { IScopedClusterClient } from 'kibana/server' ; import RE2 from 're2' ; import { mlLog } from '../../../lib/log' ; import { Job } from '../../../../common/types/anomaly_detection_jobs' ; const GROUP = 'metrics' ; const MODULE_PREFIX = 'kibana-metrics-ui' ; const SOURCES = [ 'default' , 'internal-stack-monitoring' ] ; const JOB_IDS = [ 'k8s_memory_usage' , 'k8s_network_in' , 'k8s_network_out' , 'hosts_memory_usage' , 'hosts_network_in' , 'hosts_network_out' , ] ; export async function metricsJobsSpaces ( { asInternalUser , } ) { try { const { body } = await asInternalUser . ml . getJobs < { jobs : Job [ ] ; } > ( { job_id : GROUP , } ) ; if ( body . jobs . length === 0 ) { return [ ] ; } const findMetricJobSpace = findMetricsJobSpaceFactory ( ) ; return body . jobs . map ( ( j ) => ( { id : j . job_id , space : findMetricJobSpace ( j . job_id ) } ) ) . filter ( ( j ) => j . space !== null ) as Array < { id : string ; space : string ; } > ; } catch ( { body } ) { if ( body . status !== 404 ) { mlLog . error ( `Error Initializing Metrics job ${ JSON . stringify ( body ) }` ) ; } } return [ ] ; } function findMetricsJobSpaceFactory ( ) { const reg = new RE2 ( `${ MODULE_PREFIX }-(.+)-(${ SOURCES . join ( '|' ) })-(${ JOB_IDS . join ( '|' ) })` ) ; return ( jobId ) => { const result = reg . exec ( jobId ) ; if ( result === null ) { return null ; } return result [ 1 ] ?? null ; } ; }