import { Logger , CoreSetup } from 'kibana/server' ; import moment from 'moment' ; import { RunContext , TaskManagerSetupContract , TaskManagerStartContract , } from '../../../task_manager/server' ; import { getTotalCountAggregations , getTotalCountInUse , getExecutionsPerDayCount , } from './alerts_telemetry' ; export const TELEMETRY_TASK_TYPE = 'alerting_telemetry' ; export const TASK_ID = `Alerting-${ TELEMETRY_TASK_TYPE }` ; export function initializeAlertingTelemetry ( logger , core , taskManager , kibanaIndex , eventLogIndex ) { registerAlertingTelemetryTask ( logger , core , taskManager , kibanaIndex , eventLogIndex ) ; } export function scheduleAlertingTelemetry ( logger , taskManager ? ) { if ( taskManager ) { scheduleTasks ( logger , taskManager ) ; } } function registerAlertingTelemetryTask ( logger , core , taskManager , kibanaIndex , eventLogIndex ) { taskManager . registerTaskDefinitions ( { [ TELEMETRY_TASK_TYPE ] : { title : 'Alerting usage fetch task' , timeout : '5m' , createTaskRunner : telemetryTaskRunner ( logger , core , kibanaIndex , eventLogIndex ) , } , } ) ; } async function scheduleTasks ( logger , taskManager ) { try { await taskManager . ensureScheduled ( { id : TASK_ID , taskType : TELEMETRY_TASK_TYPE , state : { } , params : { } , } ) ; } catch ( e ) { logger . debug ( `Error scheduling task, received ${ e . message }` ) ; } } export function telemetryTaskRunner ( logger , core , kibanaIndex , eventLogIndex ) { return ( { taskInstance } ) => { const { state } = taskInstance ; const getEsClient = ( ) => core . getStartServices ( ) . then ( ( [ { elasticsearch : { client } , } , ] ) => client . asInternalUser ) ; return { async run ( ) { const esClient = await getEsClient ( ) ; return Promise . all ( [ getTotalCountAggregations ( esClient , kibanaIndex ) , getTotalCountInUse ( esClient , kibanaIndex ) , getExecutionsPerDayCount ( esClient , eventLogIndex ) , ] ) . then ( ( [ totalCountAggregations , totalInUse , totalExecutions ] ) => { return { state : { runs : ( state . runs || 0 ) + 1 , ... totalCountAggregations , count_active_by_type : totalInUse . countByType , count_active_total : totalInUse . countTotal , count_disabled_total : totalCountAggregations . count_total - totalInUse . countTotal , count_rules_namespaces : totalInUse . countNamespaces , count_rules_executions_per_day : totalExecutions . countTotal , count_rules_executions_by_type_per_day : totalExecutions . countByType , count_rules_executions_failured_per_day : totalExecutions . countTotalFailures , count_rules_executions_failured_by_reason_per_day : totalExecutions . countFailuresByReason , count_rules_executions_failured_by_reason_by_type_per_day : totalExecutions . countFailuresByReasonByType , avg_execution_time_per_day : totalExecutions . avgExecutionTime , avg_execution_time_by_type_per_day : totalExecutions . avgExecutionTimeByType , } , runAt : getNextMidnight ( ) , } ; } ) . catch ( ( errMsg ) => { logger . warn ( `Error executing alerting telemetry task: ${ errMsg }` ) ; return { state : { } , runAt : getNextMidnight ( ) , } ; } ) ; } , } ; } ; } function getNextMidnight ( ) { return moment ( ) . add ( 1 , 'd' ) . startOf ( 'd' ) . toDate ( ) ; }