import { get } from 'lodash' ; import { SavedObjectsServiceStart , Logger } from 'src/core/server' ; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server' ; import { getSavedObjectAttributesFromRepo } from '../lib/telemetry' ; interface Telemetry { ui_viewed : { setup_guide : number ; engines_overview : number ; } ; ui_error : { cannot_connect : number ; not_found : number ; } ; ui_clicked : { create_first_engine_button : number ; engine_table_link : number ; } ; } export const AS_TELEMETRY_NAME = 'app_search_telemetry' ; /**
 * Register the telemetry collector
 */ export const registerTelemetryUsageCollector = ( usageCollection , savedObjects , log ) => { const telemetryUsageCollector = usageCollection . makeUsageCollector < Telemetry > ( { type : 'app_search' , fetch : async ( ) => fetchTelemetryMetrics ( savedObjects , log ) , isReady : ( ) => true , schema : { ui_viewed : { setup_guide : { type : 'long' } , engines_overview : { type : 'long' } , } , ui_error : { cannot_connect : { type : 'long' } , not_found : { type : 'long' } , } , ui_clicked : { create_first_engine_button : { type : 'long' } , engine_table_link : { type : 'long' } , } , } , } ) ; usageCollection . registerCollector ( telemetryUsageCollector ) ; } ; /**
 * Fetch the aggregated telemetry metrics from our saved objects
 */ const fetchTelemetryMetrics = async ( savedObjects , log ) => { const savedObjectsRepository = savedObjects . createInternalRepository ( ) ; const savedObjectAttributes = await getSavedObjectAttributesFromRepo ( AS_TELEMETRY_NAME , savedObjectsRepository , log ) ; const defaultTelemetrySavedObject = { ui_viewed : { setup_guide : 0 , engines_overview : 0 , } , ui_error : { cannot_connect : 0 , not_found : 0 , } , ui_clicked : { create_first_engine_button : 0 , engine_table_link : 0 , } , } ; if ( ! savedObjectAttributes ) { return defaultTelemetrySavedObject ; } return { ui_viewed : { setup_guide : get ( savedObjectAttributes , 'ui_viewed.setup_guide' , 0 ) , engines_overview : get ( savedObjectAttributes , 'ui_viewed.engines_overview' , 0 ) , } , ui_error : { cannot_connect : get ( savedObjectAttributes , 'ui_error.cannot_connect' , 0 ) , not_found : get ( savedObjectAttributes , 'ui_error.not_found' , 0 ) , } , ui_clicked : { create_first_engine_button : get ( savedObjectAttributes , 'ui_clicked.create_first_engine_button' , 0 ) , engine_table_link : get ( savedObjectAttributes , 'ui_clicked.engine_table_link' , 0 ) , } , } as Telemetry ; } ;