52 lines
2.3 KiB
Java
52 lines
2.3 KiB
Java
package com.guo.learningprogresstracker.utils;
|
|
|
|
import java.time.Duration;
|
|
import java.time.LocalDateTime;
|
|
|
|
/**
|
|
* 复习耗时结算工具(复习-07)。
|
|
* <p>口径见 lpt-docs/docs/concepts/time-and-efficiency.md:复习耗时从复习行为开始结算到结束,
|
|
* 超过 {@link #IDLE_WINDOW_SECONDS} 没有交互则截断到上一次交互时间;复习没有暂停动作,
|
|
* 也不套用学习会话的 10 分钟最小阈值。</p>
|
|
*/
|
|
public final class ReviewDurationTool {
|
|
|
|
/** 空闲截断窗口:超过该时长无交互,之后的时间不计入本次复习耗时 */
|
|
public static final long IDLE_WINDOW_SECONDS = 5 * 60L;
|
|
|
|
private ReviewDurationTool() {
|
|
}
|
|
|
|
/**
|
|
* 结算一次复习行为的净耗时(秒)。
|
|
*
|
|
* @param startedAt 行为开始时刻,为空表示前端未计时,返回 0
|
|
* @param lastInteractionAt 最近一次交互时刻,为空表示无法判断是否挂机
|
|
* @param reportedSeconds 前端按空闲截断后上报的耗时,为空时用服务端按墙钟推算的值
|
|
* @param now 服务端当前时刻
|
|
* @return 净耗时秒数,恒 >= 0,且不超过「开始时刻到 now」的墙钟跨度
|
|
*/
|
|
public static int resolveSeconds(LocalDateTime startedAt, LocalDateTime lastInteractionAt,
|
|
Integer reportedSeconds, LocalDateTime now) {
|
|
if (startedAt == null || now == null) {
|
|
return 0;
|
|
}
|
|
long elapsedSeconds = Duration.between(startedAt, now).getSeconds();
|
|
if (elapsedSeconds <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
long maxSeconds = elapsedSeconds;
|
|
if (lastInteractionAt != null && lastInteractionAt.isAfter(startedAt) && !lastInteractionAt.isAfter(now)) {
|
|
long idleSeconds = Duration.between(lastInteractionAt, now).getSeconds();
|
|
if (idleSeconds > IDLE_WINDOW_SECONDS) {
|
|
// 挂机超过窗口:耗时截断到上一次交互时间
|
|
maxSeconds = Math.min(maxSeconds, Duration.between(startedAt, lastInteractionAt).getSeconds());
|
|
}
|
|
}
|
|
|
|
long reported = reportedSeconds == null ? maxSeconds : Math.max(0L, reportedSeconds);
|
|
return (int) Math.min(Math.min(reported, maxSeconds), Integer.MAX_VALUE);
|
|
}
|
|
}
|