import type { CoreSetup , CoreStart , Plugin , PluginInitializerContext } from 'src/core/server' ; import { PLUGIN_ID } from '../common/constants' ; import { ReportingCore } from './' ; import { buildConfig , registerUiSettings , ReportingConfigType } from './config' ; import { registerDeprecations } from './deprecations' ; import { LevelLogger , ReportingStore } from './lib' ; import { registerEventLogProviderActions } from './lib/event_logger' ; import { registerRoutes } from './routes' ; import { setFieldFormats } from './services' ; import type { ReportingRequestHandlerContext , ReportingSetup , ReportingSetupDeps , ReportingStart , ReportingStartDeps , } from './types' ; import { registerReportingUsageCollector } from './usage' ; export class ReportingPlugin implements Plugin < ReportingSetup , ReportingStart , ReportingSetupDeps , ReportingStartDeps > { private logger : LevelLogger ; private reportingCore ? : ReportingCore ; constructor ( private initContext ) { this . logger = new LevelLogger ( initContext . logger . get ( ) ) ; } public setup ( core , plugins ) { const { http , status } = core ; const { features , eventLog , security , spaces , taskManager } = plugins ; const reportingCore = new ReportingCore ( this . logger , this . initContext ) ; http . registerRouteHandlerContext ( PLUGIN_ID , ( ) => { if ( reportingCore . pluginIsStarted ( ) ) { return reportingCore . getContract ( ) ; } else { this . logger . error ( `Reporting features are not yet ready` ) ; return null ; } } ) ; const basePath = http . basePath ; const router = http . createRouter < ReportingRequestHandlerContext > ( ) ; reportingCore . pluginSetup ( { status , features , eventLog , security , spaces , taskManager , basePath , router , logger : this . logger , } ) ; registerEventLogProviderActions ( eventLog ) ; registerUiSettings ( core ) ; registerDeprecations ( { core , reportingCore } ) ; registerReportingUsageCollector ( reportingCore , plugins . usageCollection ) ; registerRoutes ( reportingCore , this . logger ) ; ( async ( ) => { const config = await buildConfig ( this . initContext , core , this . logger ) ; reportingCore . setConfig ( config ) ; reportingCore . registerFeature ( ) ; this . logger . debug ( 'Setup complete' ) ; } ) ( ) . catch ( ( e ) => { this . logger . error ( `Error in Reporting setup, reporting may not function properly` ) ; this . logger . error ( e ) ; } ) ; this . reportingCore = reportingCore ; return reportingCore . getContract ( ) ; } public start ( core , plugins ) { const { elasticsearch , savedObjects , uiSettings } = core ; const { data , licensing , screenshotting , taskManager } = plugins ; setFieldFormats ( data . fieldFormats ) ; const reportingCore = this . reportingCore ! ; ( async ( ) => { await reportingCore . pluginSetsUp ( ) ; const store = new ReportingStore ( reportingCore , this . logger ) ; await reportingCore . pluginStart ( { logger : this . logger , esClient : elasticsearch . client , savedObjects , uiSettings , store , data , licensing , screenshotting , taskManager , } ) ; await store . start ( ) ; this . logger . debug ( 'Start complete' ) ; } ) ( ) . catch ( ( e ) => { this . logger . error ( `Error in Reporting start, reporting may not function properly` ) ; this . logger . error ( e ) ; } ) ; return reportingCore . getContract ( ) ; } }