import { TIME_RANGE_DATA_MODES } from '../../common/enums' ; import { findByValueEmbeddables } from '../../../../dashboard/server' ; import type { SavedObjectsClientContract , ISavedObjectsRepository , SavedObjectsFindResult , } from '../../../../../core/server' ; import type { HomeServerPluginSetup } from '../../../../home/server' ; import type { SavedVisState } from '../../../../visualizations/common' ; import type { Panel } from '../../common/types' ; export interface TimeseriesUsage { timeseries_use_last_value_mode_total : number ; timeseries_use_es_indices_total : number ; timeseries_table_use_aggregate_function : number ; timeseries_types : { table : number ; gauge : number ; markdown : number ; top_n : number ; timeseries : number ; metric : number ; } ; } const doTelemetryFoVisualizations = async ( soClient , calculateTelemetry ) => { const finder = await soClient . createPointInTimeFinder ( { type : 'visualization' , perPage : 1000 , namespaces : [ '*' ] , } ) ; for await ( const response of finder . find ( ) ) { ( response . saved_objects || [ ] ) . forEach ( ( { attributes } ) => { if ( attributes ?. visState ) { try { const visState = JSON . parse ( attributes . visState ) ; calculateTelemetry ( visState ) ; } catch { } } } ) ; } await finder . close ( ) ; } ; const doTelemetryForByValueVisualizations = async ( soClient , telemetryUseLastValueMode ) => { const byValueVisualizations = await findByValueEmbeddables ( soClient , 'visualization' ) ; for ( const item of byValueVisualizations ) { telemetryUseLastValueMode ( item . savedVis as unknown as SavedVisState < Panel > ) ; } } ; const getDefaultTSVBVisualizations = ( home ? ) => { const titles = [ ] ; const sampleDataSets = home ?. sampleData . getSampleDatasets ( ) ?? [ ] ; sampleDataSets . forEach ( ( sampleDataSet ) => sampleDataSet . savedObjects . forEach ( ( savedObject ) => { try { if ( savedObject . type === 'visualization' ) { const visState = JSON . parse ( savedObject . attributes ?. visState ) ; if ( visState . type === 'metrics' ) { titles . push ( visState . title ) ; } } } catch ( e ) { } } ) ) ; return titles ; } ; export const getStats = async ( soClient , home ? ) : Promise < TimeseriesUsage | undefined > => { const timeseriesUsage = { timeseries_use_last_value_mode_total : 0 , timeseries_use_es_indices_total : 0 , timeseries_table_use_aggregate_function : 0 , timeseries_types : { gauge : 0 , markdown : 0 , metric : 0 , table : 0 , timeseries : 0 , top_n : 0 , } , } ; const excludedFromStatsVisualizations = getDefaultTSVBVisualizations ( home ) ; function telemetryUseLastValueMode ( visState ) { if ( visState . type === 'metrics' && visState . params . type !== 'timeseries' && ( ! visState . params . time_range_mode || visState . params . time_range_mode === TIME_RANGE_DATA_MODES . LAST_VALUE ) && ! excludedFromStatsVisualizations . includes ( visState . title ) ) { timeseriesUsage . timeseries_use_last_value_mode_total ++ ; } } function telemetryUseESIndices ( visState ) { if ( visState . type === 'metrics' && ! visState . params . use_kibana_indexes && ! excludedFromStatsVisualizations . includes ( visState . title ) ) { timeseriesUsage . timeseries_use_es_indices_total ++ ; } } function telemetryTableAggFunction ( visState ) { if ( visState . type === 'metrics' && visState . params . type === 'table' && visState . params . series && visState . params . series . length > 0 && ! excludedFromStatsVisualizations . includes ( visState . title ) ) { const usesAggregateFunction = visState . params . series . some ( ( s ) => s . aggregate_by && s . aggregate_function ) ; if ( usesAggregateFunction ) { timeseriesUsage . timeseries_table_use_aggregate_function ++ ; } } } function telemetryPanelTypes ( visState ) { if ( visState . type === 'metrics' && ! excludedFromStatsVisualizations . includes ( visState . title ) ) { timeseriesUsage . timeseries_types [ visState . params . type ] ++ ; } } await Promise . all ( [ doTelemetryFoVisualizations ( soClient , telemetryUseLastValueMode ) , doTelemetryForByValueVisualizations ( soClient , telemetryUseLastValueMode ) , doTelemetryFoVisualizations ( soClient , telemetryUseESIndices ) , doTelemetryForByValueVisualizations ( soClient , telemetryUseESIndices ) , doTelemetryFoVisualizations ( soClient , telemetryTableAggFunction ) , doTelemetryForByValueVisualizations ( soClient , telemetryTableAggFunction ) , doTelemetryFoVisualizations ( soClient , telemetryPanelTypes ) , doTelemetryForByValueVisualizations ( soClient , telemetryPanelTypes ) , ] ) ; return timeseriesUsage . timeseries_use_last_value_mode_total || timeseriesUsage . timeseries_use_es_indices_total || timeseriesUsage . timeseries_table_use_aggregate_function || Object . values ( timeseriesUsage . timeseries_types ) . some ( ( visualizationCount ) => visualizationCount ) ? timeseriesUsage : undefined ; } ;