package cn.iocoder.yudao.module.mes.service.collect;
|
|
import java.time.Duration;
|
import java.time.LocalDateTime;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/** 根据采样间隔推算离线区间,不推断首次采样之前的状态。 */
|
final class CollectOfflinePolicy {
|
record Interval(LocalDateTime start, LocalDateTime end) {}
|
|
private CollectOfflinePolicy() {}
|
|
static List<Interval> calculate(List<LocalDateTime> samples, LocalDateTime start,
|
LocalDateTime end, Duration timeout) {
|
if (start == null || end == null || !start.isBefore(end) || timeout == null || timeout.isNegative() || timeout.isZero()) {
|
throw new IllegalArgumentException("时间范围或离线阈值无效");
|
}
|
List<LocalDateTime> ordered = samples.stream().filter(t -> t != null && !t.isAfter(end)).distinct().sorted().toList();
|
List<Interval> result = new ArrayList<>();
|
for (int i = 0; i < ordered.size(); i++) {
|
LocalDateTime offline = ordered.get(i).plus(timeout);
|
LocalDateTime recovery = i + 1 < ordered.size() ? ordered.get(i + 1) : end;
|
LocalDateTime clippedStart = offline.isBefore(start) ? start : offline;
|
LocalDateTime clippedEnd = recovery.isAfter(end) ? end : recovery;
|
if (clippedStart.isBefore(clippedEnd)) result.add(new Interval(clippedStart, clippedEnd));
|
}
|
return result;
|
}
|
}
|