import { first , map } from 'rxjs/operators' ; import { CollectorFetchContext , UsageCollectionSetup } from 'src/plugins/usage_collection/server' ; import { ReportingCore } from '../' ; import { ExportTypesRegistry } from '../lib/export_types_registry' ; import { GetLicense } from './' ; import { getReportingUsage } from './get_reporting_usage' ; import { ReportingUsageType } from './types' ; import { reportingSchema } from './schema' ; export function getReportingUsageCollector ( reporting , usageCollection , getLicense , exportTypesRegistry , isReady ) { return usageCollection . makeUsageCollector < ReportingUsageType > ( { type : 'reporting' , fetch : ( { esClient } ) => { const config = reporting . getConfig ( ) ; return getReportingUsage ( config , getLicense , esClient , exportTypesRegistry ) ; } , isReady , schema : reportingSchema , } ) ; } export function registerReportingUsageCollector ( reporting , usageCollection ? ) { if ( ! usageCollection ) { return ; } const exportTypesRegistry = reporting . getExportTypesRegistry ( ) ; const getLicense = async ( ) => { const { licensing } = await reporting . getPluginStartDeps ( ) ; return await licensing . license$ . pipe ( map ( ( { isAvailable , type } ) => ( { isAvailable : ( ) => isAvailable , license : { getType : ( ) => type , } , } ) ) , first ( ) ) . toPromise ( ) ; } ; const collectionIsReady = reporting . pluginStartsUp . bind ( reporting ) ; const collector = getReportingUsageCollector ( reporting , usageCollection , getLicense , exportTypesRegistry , collectionIsReady ) ; usageCollection . registerCollector ( collector ) ; }