import { isString } from 'lodash' ; import { JsonValue } from '@kbn/utility-types' ; import { HealthStatus , RawMonitoringStats } from '../monitoring' ; import { TaskManagerConfig } from '../config' ; import { Logger } from '../../../../../src/core/server' ; export function calculateHealthStatus ( summarizedStats , config , logger ) { const now = Date . now ( ) ; const requiredHotStatsFreshness = config . monitored_stats_required_freshness * 3 ; const requiredColdStatsFreshness = config . monitored_aggregated_stats_refresh_rate * 1.5 ; if ( hasStatus ( summarizedStats . stats , HealthStatus . Error ) ) { return HealthStatus . Error ; } if ( hasExpiredHotTimestamps ( summarizedStats , now , requiredHotStatsFreshness ) ) { logger . debug ( 'setting HealthStatus.Error because of expired hot timestamps' ) ; return HealthStatus . Error ; } if ( hasExpiredColdTimestamps ( summarizedStats , now , requiredColdStatsFreshness ) ) { logger . debug ( 'setting HealthStatus.Error because of expired cold timestamps' ) ; return HealthStatus . Error ; } if ( hasStatus ( summarizedStats . stats , HealthStatus . Warning ) ) { return HealthStatus . Warning ; } return HealthStatus . OK ; } function hasStatus ( stats , status ) { return Object . values ( stats ) . map ( ( stat ) => stat ?. status === status ) . includes ( true ) ; } param monitoringStats param now param requiredFreshness function hasExpiredHotTimestamps ( monitoringStats , now , requiredFreshness ) { const diff = now - getOldestTimestamp ( monitoringStats . last_update , monitoringStats . stats . runtime ?. value . polling . last_successful_poll ) ; return diff > requiredFreshness ; } function hasExpiredColdTimestamps ( monitoringStats , now , requiredFreshness ) { return now - getOldestTimestamp ( monitoringStats . stats . workload ?. timestamp ) > requiredFreshness ; } function getOldestTimestamp ( ... timestamps ) { const validTimestamps = timestamps . map ( ( timestamp ) => ( isString ( timestamp ) ? Date . parse ( timestamp ) : NaN ) ) . filter ( ( timestamp ) => ! isNaN ( timestamp ) ) ; return validTimestamps . length ? Math . min ( ... validTimestamps ) : 0 ; }