mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-28 02:48:38 +00:00
Use in: - Determining the execution time of an alert - The period throttler to determine the time passed since last execution Original commit: elastic/x-pack-elasticsearch@9197b86b68
50 lines
1.6 KiB
Java
50 lines
1.6 KiB
Java
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
*/
|
|
package org.elasticsearch.alerts.throttle;
|
|
|
|
import org.elasticsearch.alerts.Alert;
|
|
import org.elasticsearch.alerts.ExecutionContext;
|
|
import org.elasticsearch.alerts.support.clock.Clock;
|
|
import org.elasticsearch.common.joda.time.PeriodType;
|
|
import org.elasticsearch.common.unit.TimeValue;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public class PeriodThrottler implements Throttler {
|
|
|
|
private final TimeValue period;
|
|
private final PeriodType periodType;
|
|
private final Clock clock;
|
|
|
|
public PeriodThrottler(Clock clock, TimeValue period) {
|
|
this(clock, period, PeriodType.minutes());
|
|
}
|
|
|
|
public PeriodThrottler(Clock clock, TimeValue period, PeriodType periodType) {
|
|
this.period = period;
|
|
this.periodType = periodType;
|
|
this.clock = clock;
|
|
}
|
|
|
|
public TimeValue interval() {
|
|
return period;
|
|
}
|
|
|
|
@Override
|
|
public Result throttle(ExecutionContext ctx) {
|
|
Alert.Status status = ctx.alert().status();
|
|
if (status.lastExecuted() != null) {
|
|
TimeValue timeElapsed = clock.timeElapsedSince(status.lastExecuted());
|
|
if (timeElapsed.getMillis() <= period.getMillis()) {
|
|
return Result.throttle("throttling interval is set to [" + period.format(periodType) +
|
|
"] but time elapsed since last execution is [" + timeElapsed.format(periodType) + "]");
|
|
}
|
|
}
|
|
return Result.NO;
|
|
}
|
|
}
|