import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import type { IScopedClusterClient } from 'kibana/server' ; import type { DataViewsService , DataView , } from '../../../../../../../src/plugins/data_views/common' ; import type { RollupFields } from '../../../../common/types/fields' ; export interface RollupJob { job_id : string ; rollup_index : string ; index_pattern : string ; fields : RollupFields ; } export async function rollupServiceProvider ( indexPattern , { asCurrentUser } , dataViewsService ) { const rollupIndexPatternObject = await loadRollupIndexPattern ( indexPattern , dataViewsService ) ; let jobIndexPatterns = [ indexPattern ] ; async function getRollupJobs ( ) { if ( rollupIndexPatternObject !== null && rollupIndexPatternObject . typeMeta ?. params !== undefined ) { const rollUpIndex = rollupIndexPatternObject . typeMeta . params . rollup_index ; const { body : rollupCaps } = await asCurrentUser . rollup . getRollupIndexCaps ( { index : rollUpIndex , } ) ; const indexRollupCaps = rollupCaps [ rollUpIndex ] ; if ( indexRollupCaps && indexRollupCaps . rollup_jobs ) { jobIndexPatterns = indexRollupCaps . rollup_jobs . map ( ( j ) => j . index_pattern ) ; return indexRollupCaps . rollup_jobs ; } } return null ; } function getIndexPattern ( ) { return jobIndexPatterns . join ( ',' ) ; } return { getRollupJobs , getIndexPattern , } ; } async function loadRollupIndexPattern ( indexPattern , dataViewsService ) { const resp = await dataViewsService . find ( '*' , 10000 ) ; const obj = resp . find ( ( dv ) => dv . type === 'rollup' && dv . title === indexPattern && dv . typeMeta !== undefined ) ; return obj ?? null ; }