import { MakeSchemaFrom , UsageCollectionSetup } from 'src/plugins/usage_collection/server' ; import { get } from 'lodash' ; import { TaskManagerStartContract } from '../../../task_manager/server' ; import { AlertsUsage } from './types' ; const byTypeSchema = { DYNAMIC_KEY : { type : 'long' } , '__index-threshold' : { type : 'long' } , '__es-query' : { type : 'long' } , transform_health : { type : 'long' } , apm__error_rate : { type : 'long' } , apm__transaction_error_rate : { type : 'long' } , apm__transaction_duration : { type : 'long' } , apm__transaction_duration_anomaly : { type : 'long' } , metrics__alert__threshold : { type : 'long' } , metrics__alert__inventory__threshold : { type : 'long' } , logs__alert__document__count : { type : 'long' } , monitoring_alert_cluster_health : { type : 'long' } , monitoring_alert_cpu_usage : { type : 'long' } , monitoring_alert_disk_usage : { type : 'long' } , monitoring_alert_elasticsearch_version_mismatch : { type : 'long' } , monitoring_alert_kibana_version_mismatch : { type : 'long' } , monitoring_alert_license_expiration : { type : 'long' } , monitoring_alert_logstash_version_mismatch : { type : 'long' } , monitoring_alert_nodes_changed : { type : 'long' } , siem__signals : { type : 'long' } , siem__notifications : { type : 'long' } , xpack__uptime__alerts__monitorStatus : { type : 'long' } , xpack__uptime__alerts__tls : { type : 'long' } , xpack__uptime__alerts__durationAnomaly : { type : 'long' } , '__geo-containment' : { type : 'long' } , xpack__ml__anomaly_detection_alert : { type : 'long' } , xpack__ml__anomaly_detection_jobs_health : { type : 'long' } , } ; const byReasonSchema = { DYNAMIC_KEY : { type : 'long' } , read : { type : 'long' } , decrypt : { type : 'long' } , license : { type : 'long' } , unknown : { type : 'long' } , } ; const byReasonSchemaByType = { DYNAMIC_KEY : byTypeSchema , read : byTypeSchema , decrypt : byTypeSchema , license : byTypeSchema , unknown : byTypeSchema , } ; export function createAlertsUsageCollector ( usageCollection , taskManager ) { return usageCollection . makeUsageCollector < AlertsUsage > ( { type : 'alerts' , isReady : async ( ) => { await taskManager ; return true ; } , fetch : async ( ) => { try { const doc = await getLatestTaskState ( await taskManager ) ; const { runs , ... state } = get ( doc , 'state' ) as AlertsUsage & { runs : number ; } ; return { ... state , } ; } catch ( err ) { return { count_total : 0 , count_active_total : 0 , count_disabled_total : 0 , throttle_time : { min : '0s' , avg : '0s' , max : '0s' , } , schedule_time : { min : '0s' , avg : '0s' , max : '0s' , } , throttle_time_number_s : { min : 0 , avg : 0 , max : 0 , } , schedule_time_number_s : { min : 0 , avg : 0 , max : 0 , } , connectors_per_alert : { min : 0 , avg : 0 , max : 0 , } , count_active_by_type : { } , count_by_type : { } , count_rules_namespaces : 0 , count_rules_executions_per_day : 0 , count_rules_executions_by_type_per_day : { } , count_rules_executions_failured_per_day : 0 , count_rules_executions_failured_by_reason_per_day : { } , count_rules_executions_failured_by_reason_by_type_per_day : { } , avg_execution_time_per_day : 0 , avg_execution_time_by_type_per_day : { } , } ; } } , schema : { count_total : { type : 'long' } , count_active_total : { type : 'long' } , count_disabled_total : { type : 'long' } , throttle_time : { min : { type : 'keyword' } , avg : { type : 'keyword' } , max : { type : 'keyword' } , } , schedule_time : { min : { type : 'keyword' } , avg : { type : 'keyword' } , max : { type : 'keyword' } , } , throttle_time_number_s : { min : { type : 'long' } , avg : { type : 'float' } , max : { type : 'long' } , } , schedule_time_number_s : { min : { type : 'long' } , avg : { type : 'float' } , max : { type : 'long' } , } , connectors_per_alert : { min : { type : 'long' } , avg : { type : 'float' } , max : { type : 'long' } , } , count_active_by_type : byTypeSchema , count_by_type : byTypeSchema , count_rules_namespaces : { type : 'long' } , count_rules_executions_per_day : { type : 'long' } , count_rules_executions_by_type_per_day : byTypeSchema , count_rules_executions_failured_per_day : { type : 'long' } , count_rules_executions_failured_by_reason_per_day : byReasonSchema , count_rules_executions_failured_by_reason_by_type_per_day : byReasonSchemaByType , avg_execution_time_per_day : { type : 'long' } , avg_execution_time_by_type_per_day : byTypeSchema , } , } ) ; } async function getLatestTaskState ( taskManager ) { try { const result = await taskManager . get ( 'Alerting-alerting_telemetry' ) ; return result ; } catch ( err ) { const errMessage = err && err . message ? err . message : err . toString ( ) ; if ( ! errMessage . includes ( 'NotInitialized' ) ) { throw err ; } } return null ; } export function registerAlertsUsageCollector ( usageCollection , taskManager ) { const collector = createAlertsUsageCollector ( usageCollection , taskManager ) ; usageCollection . registerCollector ( collector ) ; }