import { Duration } from 'moment-timezone' ; import { schema } from '@kbn/config-schema' ; import { LogRecord } from '@kbn/logging' ; import { RollingFileContext } from '../../rolling_file_context' ; import { TriggeringPolicy } from '../policy' ; import { getNextRollingTime } from './get_next_rolling_time' ; import { isValidRolloverInterval } from './utils' ; export interface TimeIntervalTriggeringPolicyConfig { type : 'time-interval' ; remarks interval : Duration ; /**
     * Indicates whether the interval should be adjusted to cause the next rollover to occur on the interval boundary.
     *
     * For example, if the interval is `4h` and the current hour is 3 am then
     * the first rollover will occur at 4 am and then next ones will occur at 8 am, noon, 4pm, etc.
     * The default value is true.
     */ modulate : boolean ; } export const timeIntervalTriggeringPolicyConfigSchema = schema . object ( { type : schema . literal ( 'time-interval' ) , interval : schema . duration ( { defaultValue : '24h' , validate : ( interval ) => { if ( ! isValidRolloverInterval ( interval ) ) { return 'Interval value cannot overflow to a higher time unit.' ; } } , } ) , modulate : schema . boolean ( { defaultValue : true } ) , } ) ; /**
 * A triggering policy based on a fixed time interval
 */ export class TimeIntervalTriggeringPolicy implements TriggeringPolicy { /**
     * milliseconds timestamp of when the next rollover should occur.
     */ private nextRolloverTime : number ; constructor ( private readonly config , context ) { this . nextRolloverTime = getNextRollingTime ( context . currentFileTime || Date . now ( ) , config . interval , config . modulate ) ; } isTriggeringEvent ( record ) { const eventTime = record . timestamp . getTime ( ) ; if ( eventTime >= this . nextRolloverTime ) { this . nextRolloverTime = getNextRollingTime ( eventTime , this . config . interval , this . config . modulate ) ; return true ; } return false ; } }