import { getCapabilitiesForRollupIndices , IndexPatternsService , } from '../../../../../../data/server' ; import { AbstractSearchStrategy } from './abstract_search_strategy' ; import { RollupSearchCapabilities } from '../capabilities/rollup_search_capabilities' ; import type { FetchedIndexPattern } from '../../../../common/types' ; import type { CachedIndexPatternFetcher } from '../lib/cached_index_pattern_fetcher' ; import type { VisTypeTimeseriesRequest , VisTypeTimeseriesRequestHandlerContext , VisTypeTimeseriesVisDataRequest , } from '../../../types' ; import { UI_SETTINGS } from '../../../../common/constants' ; const getRollupIndices = ( rollupData ) => Object . keys ( rollupData ) ; const isIndexPatternContainsWildcard = ( indexPattern ) => indexPattern . includes ( '*' ) ; export class RollupSearchStrategy extends AbstractSearchStrategy { async search ( requestContext , req , bodies ) { return super . search ( requestContext , req , bodies , 'rollup' ) ; } async getRollupData ( requestContext , indexPattern ) { try { const { body } = await requestContext . core . elasticsearch . client . asCurrentUser . rollup . getRollupIndexCaps ( { index : indexPattern , } ) ; return body ; } catch ( e ) { return { } ; } } async checkForViability ( requestContext , req , { indexPatternString , indexPattern } ) { let isViable = false ; let capabilities = null ; if ( indexPatternString && ( ( ! indexPattern && ! isIndexPatternContainsWildcard ( indexPatternString ) ) || indexPattern ?. type === 'rollup' ) ) { const rollupData = await this . getRollupData ( requestContext , indexPatternString ) ; const rollupIndices = getRollupIndices ( rollupData ) ; const uiSettings = requestContext . core . uiSettings . client ; isViable = rollupIndices . length === 1 ; if ( isViable ) { const [ rollupIndex ] = rollupIndices ; const fieldsCapabilities = getCapabilitiesForRollupIndices ( rollupData ) ; capabilities = new RollupSearchCapabilities ( { maxBucketsLimit : await uiSettings . get ( UI_SETTINGS . MAX_BUCKETS_SETTING ) , panel : req . body . panels ? req . body . panels [ 0 ] : null , } , fieldsCapabilities , rollupIndex ) ; } } return { isViable , capabilities , } ; } async getFieldsForWildcard ( fetchedIndexPattern , indexPatternsService , getCachedIndexPatternFetcher , capabilities ? ) { return super . getFieldsForWildcard ( fetchedIndexPattern , indexPatternsService , capabilities , { type : 'rollup' , rollupIndex : fetchedIndexPattern . indexPatternString , } ) ; } }