import { PluginInitializerContext , CoreSetup , CoreStart , Plugin , Logger , KibanaRequest , IUiSettingsClient , } from 'src/core/server' ; import { Observable } from 'rxjs' ; import { Server } from '@hapi/hapi' ; import { first , map } from 'rxjs/operators' ; import { VisTypeTimeseriesConfig } from './config' ; import { getVisData } from './lib/get_vis_data' ; import { UsageCollectionSetup } from '../../../usage_collection/server' ; import { HomeServerPluginSetup } from '../../../home/server' ; import { PluginStart } from '../../../data/server' ; import { IndexPatternsService } from '../../../data/common' ; import { visDataRoutes } from './routes/vis' ; import { fieldsRoutes } from './routes/fields' ; import { getUiSettings } from './ui_settings' ; import type { VisTypeTimeseriesRequestHandlerContext , VisTypeTimeseriesVisDataRequest , } from './types' ; import type { FieldFormatsRegistry } from '../../../field_formats/common' ; import { SearchStrategyRegistry , DefaultSearchStrategy , RollupSearchStrategy , } from './lib/search_strategies' ; import type { TimeseriesVisData , VisPayload } from '../common/types' ; import { registerTimeseriesUsageCollector } from './usage_collector' ; export interface LegacySetup { server : Server ; } interface VisTypeTimeseriesPluginSetupDependencies { usageCollection ? : UsageCollectionSetup ; home ? : HomeServerPluginSetup ; } interface VisTypeTimeseriesPluginStartDependencies { data : PluginStart ; } export interface VisTypeTimeseriesSetup { getVisData : ( requestContext : VisTypeTimeseriesRequestHandlerContext , fakeRequest : KibanaRequest , options : any ) => Promise < TimeseriesVisData > ; } export interface Framework { core : CoreSetup < VisTypeTimeseriesPluginStartDependencies > ; plugins : any ; config$ : Observable < VisTypeTimeseriesConfig > ; globalConfig$ : PluginInitializerContext [ 'config' ] [ 'legacy' ] [ 'globalConfig$' ] ; logger : Logger ; searchStrategyRegistry : SearchStrategyRegistry ; getIndexPatternsService : ( requestContext : VisTypeTimeseriesRequestHandlerContext ) => Promise < IndexPatternsService > ; getFieldFormatsService : ( uiSettings : IUiSettingsClient ) => Promise < FieldFormatsRegistry > ; getEsShardTimeout : ( ) => Promise < number > ; } export class VisTypeTimeseriesPlugin implements Plugin < VisTypeTimeseriesSetup > { constructor ( private readonly initializerContext ) { this . initializerContext = initializerContext ; } public setup ( core , plugins ) { const logger = this . initializerContext . logger . get ( 'visTypeTimeseries' ) ; core . uiSettings . register ( getUiSettings ( ) ) ; const config$ = this . initializerContext . config . create < VisTypeTimeseriesConfig > ( ) ; const globalConfig$ = this . initializerContext . config . legacy . globalConfig$ ; const router = core . http . createRouter < VisTypeTimeseriesRequestHandlerContext > ( ) ; const searchStrategyRegistry = new SearchStrategyRegistry ( ) ; const framework = { core , plugins , config$ , globalConfig$ , logger , searchStrategyRegistry , getEsShardTimeout : ( ) => globalConfig$ . pipe ( first ( ) , map ( ( config ) => config . elasticsearch . shardTimeout . asMilliseconds ( ) ) ) . toPromise ( ) , getIndexPatternsService : async ( requestContext ) => { const [ , { data } ] = await core . getStartServices ( ) ; return await data . indexPatterns . indexPatternsServiceFactory ( requestContext . core . savedObjects . client , requestContext . core . elasticsearch . client . asCurrentUser ) ; } , getFieldFormatsService : async ( uiSettings ) => { const [ , { data } ] = await core . getStartServices ( ) ; return data . fieldFormats . fieldFormatServiceFactory ( uiSettings ) ; } , } ; searchStrategyRegistry . addStrategy ( new DefaultSearchStrategy ( ) ) ; searchStrategyRegistry . addStrategy ( new RollupSearchStrategy ( ) ) ; visDataRoutes ( router , framework ) ; fieldsRoutes ( router , framework ) ; if ( plugins . usageCollection ) { registerTimeseriesUsageCollector ( plugins . usageCollection , plugins . home ) ; } return { getVisData : async ( requestContext , fakeRequest , options ) => { return await getVisData ( requestContext , { ... fakeRequest , body : options } as VisTypeTimeseriesVisDataRequest , framework ) ; } , } ; } public start ( core ) { } }