import { Reporter , METRIC_TYPE } from '@osd/analytics' ; import { Subject , merge } from 'rxjs' ; import { Storage } from '../../opensearch_dashboards_utils/public' ; import { createReporter } from './services' ; import { PluginInitializerContext , Plugin , CoreSetup , CoreStart , HttpSetup , } from '../../../core/public' ; import { reportApplicationUsage } from './services/application_usage' ; interface PublicConfigType { uiMetric : { enabled : boolean ; debug : boolean ; } ; } export interface UsageCollectionSetup { allowTrackUserAgent : ( allow : boolean ) => void ; reportUiStats : Reporter [ 'reportUiStats' ] ; METRIC_TYPE : typeof METRIC_TYPE ; __LEGACY : { deprecated appChanged : ( appId : string ) => void ; } ; } export interface UsageCollectionStart { reportUiStats : Reporter [ 'reportUiStats' ] ; METRIC_TYPE : typeof METRIC_TYPE ; } export function isUnauthenticated ( http ) { const { anonymousPaths } = http ; return anonymousPaths . isAnonymous ( window . location . pathname ) ; } export class UsageCollectionPlugin implements Plugin < UsageCollectionSetup , UsageCollectionStart > { private readonly legacyAppId$ = new Subject < string > ( ) ; private trackUserAgent : boolean = true ; private reporter ? : Reporter ; private config : PublicConfigType ; constructor ( initializerContext ) { this . config = initializerContext . config . get < PublicConfigType > ( ) ; } public setup ( { http } ) { const localStorage = new Storage ( window . localStorage ) ; const debug = this . config . uiMetric . debug ; this . reporter = createReporter ( { localStorage , debug , fetch : http , } ) ; return { allowTrackUserAgent : ( allow ) => { this . trackUserAgent = allow ; } , reportUiStats : this . reporter . reportUiStats , METRIC_TYPE , __LEGACY : { appChanged : ( appId ) => this . legacyAppId$ . next ( appId ) , } , } ; } public start ( { http , application } ) { if ( ! this . reporter ) { throw new Error ( 'Usage collection reporter not set up correctly' ) ; } if ( this . config . uiMetric . enabled && ! isUnauthenticated ( http ) ) { this . reporter . start ( ) ; } if ( this . trackUserAgent ) { this . reporter . reportUserAgent ( 'opensearchDashboards' ) ; } reportApplicationUsage ( merge ( application . currentAppId$ , this . legacyAppId$ ) , this . reporter ) ; return { reportUiStats : this . reporter . reportUiStats , METRIC_TYPE , } ; } public stop ( ) { } }