From 3b33cd83967cd8bf9157ee9b6dee9a6aec427a66 Mon Sep 17 00:00:00 2001 From: cat-shark <1716967236@qq.com> Date: Fri, 9 Oct 2026 11:12:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=A4=8D=E4=B9=A0?= =?UTF-8?q?=E8=80=97=E6=97=B6=E7=BB=9F=E8=AE=A1=E4=B8=8E=E6=8C=89=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=B1=87=E6=80=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ReviewController.java | 6 +- .../dto/ReviewTaskStats.java | 2 + .../dto/ReviewTiming.java | 15 ++++ .../dto/request/RecallCompareRequest.java | 14 ++++ .../entity/ReviewRecallRecordEntity.java | 6 ++ .../service/StandardMindMapService.java | 5 +- .../service/impl/ReviewServiceImpl.java | 39 ++++++++++ .../impl/StandardMindMapServiceImpl.java | 14 +++- .../utils/ReviewDurationTool.java | 51 ++++++++++++ ..._add_review_duration_to_recall_records.sql | 10 +++ .../controller/ReviewControllerUnitTest.java | 34 +++++++- .../impl/ReviewDurationIntegrationTest.java | 78 +++++++++++++++++++ .../service/impl/ReviewServiceImplTest.java | 30 +++++++ .../utils/ReviewDurationToolTest.java | 75 ++++++++++++++++++ 14 files changed, 373 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/guo/learningprogresstracker/dto/ReviewTiming.java create mode 100644 src/main/java/com/guo/learningprogresstracker/utils/ReviewDurationTool.java create mode 100644 src/main/resources/db/migration/V20261009_2__add_review_duration_to_recall_records.sql create mode 100644 src/test/java/com/guo/learningprogresstracker/service/impl/ReviewDurationIntegrationTest.java create mode 100644 src/test/java/com/guo/learningprogresstracker/utils/ReviewDurationToolTest.java diff --git a/src/main/java/com/guo/learningprogresstracker/controller/ReviewController.java b/src/main/java/com/guo/learningprogresstracker/controller/ReviewController.java index 189eb02..994a0c3 100644 --- a/src/main/java/com/guo/learningprogresstracker/controller/ReviewController.java +++ b/src/main/java/com/guo/learningprogresstracker/controller/ReviewController.java @@ -2,6 +2,7 @@ package com.guo.learningprogresstracker.controller; import com.guo.learningprogresstracker.dto.ReviewFeedItem; import com.guo.learningprogresstracker.dto.ReviewTaskStats; +import com.guo.learningprogresstracker.dto.ReviewTiming; import com.guo.learningprogresstracker.dto.request.RecallCompareRequest; import com.guo.learningprogresstracker.dto.request.UpdateStandardMindMapRequest; import com.guo.learningprogresstracker.entity.CommonResult; @@ -126,7 +127,10 @@ public class ReviewController { public CommonResult recallCompare( @PathVariable String taskNum, @Valid @RequestBody RecallCompareRequest request) throws NotFindEntitiesException, OperationFailedException { - return CommonResult.success(standardMindMapService.recallCompare(taskNum, request.getRecallOutline(), request.getFocusPath())); + return CommonResult.success(standardMindMapService.recallCompare( + taskNum, request.getRecallOutline(), request.getFocusPath(), + new ReviewTiming(request.getReviewStartedAt(), request.getReviewLastInteractionAt(), + request.getReviewDurationSeconds()))); } /** diff --git a/src/main/java/com/guo/learningprogresstracker/dto/ReviewTaskStats.java b/src/main/java/com/guo/learningprogresstracker/dto/ReviewTaskStats.java index 21a9c23..c17cd15 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/ReviewTaskStats.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/ReviewTaskStats.java @@ -17,4 +17,6 @@ public class ReviewTaskStats { private double weekEffectiveTime; private double avgEffectiveTime; private double avgEffectivenessRatio; + /** 该任务累计复习耗时(秒):来自回忆对比等复习行为,与学习时长独立统计 */ + private long reviewDurationSeconds; } diff --git a/src/main/java/com/guo/learningprogresstracker/dto/ReviewTiming.java b/src/main/java/com/guo/learningprogresstracker/dto/ReviewTiming.java new file mode 100644 index 0000000..e2af0d2 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/dto/ReviewTiming.java @@ -0,0 +1,15 @@ +package com.guo.learningprogresstracker.dto; + +import java.time.LocalDateTime; + +/** + * 一次复习行为的计时信息(复习-07)。 + *

由前端上报:复习行为开始时刻、最近一次交互时刻、以及按 5 分钟空闲截断后的净耗时。 + * 三个字段都可空;服务端会用 {@code utils.ReviewDurationTool} 统一结算并落库。

+ * + * @param startedAt 行为开始时刻 + * @param lastInteractionAt 最近一次交互时刻 + * @param durationSeconds 前端结算出的净耗时(秒) + */ +public record ReviewTiming(LocalDateTime startedAt, LocalDateTime lastInteractionAt, Integer durationSeconds) { +} diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/RecallCompareRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/RecallCompareRequest.java index 960b43e..5986323 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/RecallCompareRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/RecallCompareRequest.java @@ -1,8 +1,11 @@ package com.guo.learningprogresstracker.dto.request; +import com.fasterxml.jackson.annotation.JsonFormat; import jakarta.validation.constraints.NotBlank; import lombok.Data; +import java.time.LocalDateTime; + /** * 用户回忆对比请求 */ @@ -14,4 +17,15 @@ public class RecallCompareRequest { /** 复习起点节点路径(以 / 分隔),null 为任务维度 */ private String focusPath; + + /** 本次回忆复习开始时刻(本地时间),复习-07 */ + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") + private LocalDateTime reviewStartedAt; + + /** 最近一次交互时刻(本地时间),供服务端按 5 分钟空闲截断,复习-07 */ + @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") + private LocalDateTime reviewLastInteractionAt; + + /** 前端按 5 分钟空闲截断后的净耗时(秒),复习-07 */ + private Integer reviewDurationSeconds; } diff --git a/src/main/java/com/guo/learningprogresstracker/entity/ReviewRecallRecordEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/ReviewRecallRecordEntity.java index 82aa56b..f39d521 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/ReviewRecallRecordEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/ReviewRecallRecordEntity.java @@ -57,6 +57,12 @@ public class ReviewRecallRecordEntity extends BaseEntity implements Serializable @TableField(value = "extra_count") private Integer extraCount; + /** + * 本次复习耗时(秒),已按「超过 5 分钟无交互则截断」规则结算 + */ + @TableField(value = "review_duration_seconds") + private Integer reviewDurationSeconds; + @TableField(exist = false) private static final long serialVersionUID = 1L; } diff --git a/src/main/java/com/guo/learningprogresstracker/service/StandardMindMapService.java b/src/main/java/com/guo/learningprogresstracker/service/StandardMindMapService.java index 779a140..082cbaf 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/StandardMindMapService.java +++ b/src/main/java/com/guo/learningprogresstracker/service/StandardMindMapService.java @@ -1,5 +1,6 @@ package com.guo.learningprogresstracker.service; +import com.guo.learningprogresstracker.dto.ReviewTiming; import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity; import com.guo.learningprogresstracker.entity.ReviewStandardMindMapEntity; import com.guo.learningprogresstracker.exception.NotFindEntitiesException; @@ -39,8 +40,10 @@ public interface StandardMindMapService { * * @param recallOutline 用户回忆的缩进大纲文本 * @param focusPath 可选:标准导图中的节点路径(/ 分隔),指定后仅对比该子树 + * @param timing 可选:本次复习行为的计时信息(复习-07),为空时不计复习耗时 */ - ReviewStandardMindMapEntity recallCompare(String taskNum, String recallOutline, String focusPath) throws NotFindEntitiesException, OperationFailedException; + ReviewStandardMindMapEntity recallCompare(String taskNum, String recallOutline, String focusPath, + ReviewTiming timing) throws NotFindEntitiesException, OperationFailedException; /** * 根据内容文本在标准导图中查找最匹配的节点,返回节点路径。 diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImpl.java index 4240174..cccfad4 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImpl.java @@ -378,6 +378,45 @@ public class ReviewServiceImpl implements ReviewService { }); fillReportAndFragmentCount(sessionToTaskNum, statsByTaskNum); + fillReviewDuration(statsByTaskNum); + } + + /** + * 按任务汇总复习行为累计耗时(复习-07)。在 SQL 侧聚合,只把任务数量的行拉回内存, + * 不随回忆记录条数增长。 + */ + private void fillReviewDuration(Map statsByTaskNum) { + List> rows = reviewRecallRecordMapper.selectMaps( + Wrappers.query() + .select("task_num AS taskNum", + "COALESCE(SUM(review_duration_seconds), 0) AS reviewDurationSeconds") + .in("task_num", statsByTaskNum.keySet()) + .groupBy("task_num")); + + for (Map row : rows) { + Object taskNum = columnValue(row, "taskNum"); + Object duration = columnValue(row, "reviewDurationSeconds"); + ReviewTaskStats stats = taskNum == null ? null : statsByTaskNum.get(taskNum.toString()); + if (stats != null && duration instanceof Number number) { + stats.setReviewDurationSeconds(number.longValue()); + } + } + } + + /** + * 取聚合结果的列:别名大小写随数据库实现不同(MySQL 保留、H2 会转为小写),这里统一不区分大小写。 + */ + private static Object columnValue(Map row, String column) { + Object direct = row.get(column); + if (direct != null) { + return direct; + } + for (Map.Entry entry : row.entrySet()) { + if (entry.getKey() != null && entry.getKey().equalsIgnoreCase(column)) { + return entry.getValue(); + } + } + return null; } private void fillReportAndFragmentCount(Map sessionToTaskNum, diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/StandardMindMapServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/StandardMindMapServiceImpl.java index acf8157..02a06ea 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/StandardMindMapServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/StandardMindMapServiceImpl.java @@ -3,6 +3,7 @@ package com.guo.learningprogresstracker.service.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.guo.learningprogresstracker.dto.ReviewTiming; import com.guo.learningprogresstracker.entity.*; import com.guo.learningprogresstracker.exception.NotFindEntitiesException; import com.guo.learningprogresstracker.exception.OperationFailedException; @@ -12,6 +13,7 @@ import com.guo.learningprogresstracker.service.StandardMindMapService; import com.guo.learningprogresstracker.utils.CompareResult; import com.guo.learningprogresstracker.utils.MindMapNode; import com.guo.learningprogresstracker.utils.MindMapTreeTool; +import com.guo.learningprogresstracker.utils.ReviewDurationTool; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -152,7 +154,8 @@ public class StandardMindMapServiceImpl implements StandardMindMapService { @Override @Transactional - public ReviewStandardMindMapEntity recallCompare(String taskNum, String recallOutline, String focusPath) + public ReviewStandardMindMapEntity recallCompare(String taskNum, String recallOutline, String focusPath, + ReviewTiming timing) throws NotFindEntitiesException, OperationFailedException { ensureTaskExists(taskNum); @@ -216,11 +219,16 @@ public class StandardMindMapServiceImpl implements StandardMindMapService { record.setMatchedCount(result.getMatchedCount()); record.setMissedCount(result.getMissedCount()); record.setExtraCount(result.getExtraCount()); + record.setReviewDurationSeconds(ReviewDurationTool.resolveSeconds( + timing == null ? null : timing.startedAt(), + timing == null ? null : timing.lastInteractionAt(), + timing == null ? null : timing.durationSeconds(), + LocalDateTime.now())); recallRecordMapper.insert(record); - log.info("回忆对比: taskNum={}, recallRatio={}, matched={}, missed={}, extra={}", + log.info("回忆对比: taskNum={}, recallRatio={}, matched={}, missed={}, extra={}, 复习耗时={}秒", taskNum, result.getRecallRatio(), result.getMatchedCount(), - result.getMissedCount(), result.getExtraCount()); + result.getMissedCount(), result.getExtraCount(), record.getReviewDurationSeconds()); return standard; } diff --git a/src/main/java/com/guo/learningprogresstracker/utils/ReviewDurationTool.java b/src/main/java/com/guo/learningprogresstracker/utils/ReviewDurationTool.java new file mode 100644 index 0000000..45c732f --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/utils/ReviewDurationTool.java @@ -0,0 +1,51 @@ +package com.guo.learningprogresstracker.utils; + +import java.time.Duration; +import java.time.LocalDateTime; + +/** + * 复习耗时结算工具(复习-07)。 + *

口径见 lpt-docs/docs/concepts/time-and-efficiency.md:复习耗时从复习行为开始结算到结束, + * 超过 {@link #IDLE_WINDOW_SECONDS} 没有交互则截断到上一次交互时间;复习没有暂停动作, + * 也不套用学习会话的 10 分钟最小阈值。

+ */ +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); + } +} diff --git a/src/main/resources/db/migration/V20261009_2__add_review_duration_to_recall_records.sql b/src/main/resources/db/migration/V20261009_2__add_review_duration_to_recall_records.sql new file mode 100644 index 0000000..0ff22da --- /dev/null +++ b/src/main/resources/db/migration/V20261009_2__add_review_duration_to_recall_records.sql @@ -0,0 +1,10 @@ +-- 复习耗时(复习-07):回忆对比行为记录本次净耗时,单位秒。 +-- 口径见 lpt-docs/docs/concepts/time-and-efficiency.md:耗时只覆盖复习行为(回忆对比、学习引导), +-- 超过 5 分钟无交互则截断到上一次交互时间;学习引导复用同一字段。 +-- 对应清单条目:复习-07。 + +ALTER TABLE review_recall_records + ADD COLUMN review_duration_seconds INT DEFAULT 0 NOT NULL COMMENT '本次复习耗时(秒),已按 5 分钟空闲截断' AFTER extra_count; + +-- 复习总览按任务聚合复习耗时:WHERE created_by = ? AND task_num IN (...) GROUP BY task_num +CREATE INDEX idx_recall_records_created_by_task ON review_recall_records (created_by, task_num); diff --git a/src/test/java/com/guo/learningprogresstracker/controller/ReviewControllerUnitTest.java b/src/test/java/com/guo/learningprogresstracker/controller/ReviewControllerUnitTest.java index 92d3cf0..33dc926 100644 --- a/src/test/java/com/guo/learningprogresstracker/controller/ReviewControllerUnitTest.java +++ b/src/test/java/com/guo/learningprogresstracker/controller/ReviewControllerUnitTest.java @@ -2,6 +2,7 @@ package com.guo.learningprogresstracker.controller; import com.guo.learningprogresstracker.dto.ReviewFeedItem; import com.guo.learningprogresstracker.dto.ReviewTaskStats; +import com.guo.learningprogresstracker.dto.ReviewTiming; import com.guo.learningprogresstracker.dto.request.RecallCompareRequest; import com.guo.learningprogresstracker.dto.request.UpdateStandardMindMapRequest; import com.guo.learningprogresstracker.entity.CommonResult; @@ -13,15 +14,21 @@ import com.guo.learningprogresstracker.service.ReviewService; import com.guo.learningprogresstracker.service.StandardMindMapService; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.time.LocalDateTime; import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -159,13 +166,38 @@ class ReviewControllerUnitTest { RecallCompareRequest request = new RecallCompareRequest(); request.setRecallOutline("- 回忆内容"); request.setFocusPath("根/子"); + request.setReviewStartedAt(LocalDateTime.of(2026, 10, 9, 10, 0, 0)); + request.setReviewLastInteractionAt(LocalDateTime.of(2026, 10, 9, 10, 3, 0)); + request.setReviewDurationSeconds(180); ReviewStandardMindMapEntity mindMap = new ReviewStandardMindMapEntity(); - when(standardMindMapService.recallCompare("T007", "- 回忆内容", "根/子")).thenReturn(mindMap); + ArgumentCaptor timingCaptor = ArgumentCaptor.forClass(ReviewTiming.class); + when(standardMindMapService.recallCompare(eq("T007"), eq("- 回忆内容"), eq("根/子"), any())) + .thenReturn(mindMap); CommonResult result = controller.recallCompare("T007", request); assertEquals(200, result.getCode()); assertSame(mindMap, result.getData()); + verify(standardMindMapService).recallCompare(eq("T007"), eq("- 回忆内容"), eq("根/子"), timingCaptor.capture()); + assertEquals(LocalDateTime.of(2026, 10, 9, 10, 0, 0), timingCaptor.getValue().startedAt()); + assertEquals(180, timingCaptor.getValue().durationSeconds()); + } + + @Test + void recallCompare_withoutTiming_passesEmptyTiming() throws Exception { + RecallCompareRequest request = new RecallCompareRequest(); + request.setRecallOutline("- 回忆内容"); + ReviewStandardMindMapEntity mindMap = new ReviewStandardMindMapEntity(); + ArgumentCaptor timingCaptor = ArgumentCaptor.forClass(ReviewTiming.class); + when(standardMindMapService.recallCompare(eq("T012"), eq("- 回忆内容"), isNull(), any())) + .thenReturn(mindMap); + + CommonResult result = controller.recallCompare("T012", request); + + assertEquals(200, result.getCode()); + verify(standardMindMapService).recallCompare(eq("T012"), eq("- 回忆内容"), isNull(), timingCaptor.capture()); + assertNull(timingCaptor.getValue().startedAt()); + assertNull(timingCaptor.getValue().durationSeconds()); } @Test diff --git a/src/test/java/com/guo/learningprogresstracker/service/impl/ReviewDurationIntegrationTest.java b/src/test/java/com/guo/learningprogresstracker/service/impl/ReviewDurationIntegrationTest.java new file mode 100644 index 0000000..59ce661 --- /dev/null +++ b/src/test/java/com/guo/learningprogresstracker/service/impl/ReviewDurationIntegrationTest.java @@ -0,0 +1,78 @@ +package com.guo.learningprogresstracker.service.impl; + +import cn.dev33.satoken.stp.StpUtil; +import com.guo.learningprogresstracker.dto.ReviewTaskStats; +import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity; +import com.guo.learningprogresstracker.mapper.ReviewRecallRecordMapper; +import com.guo.learningprogresstracker.service.ReviewService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * 复习耗时的落库与聚合集成测试(复习-07):在真实 SQL(含租户条件、逻辑删除与 GROUP BY 聚合)下验证。 + */ +@SpringBootTest +class ReviewDurationIntegrationTest { + + @Autowired + private ReviewService reviewService; + + @Autowired + private ReviewRecallRecordMapper recallRecordMapper; + + /** GenerateNumTool 在同毫秒内会生成相同编号(数据-03),集成测试自己保证任务编号唯一。 */ + private static final java.util.concurrent.atomic.AtomicInteger SEQ = new java.util.concurrent.atomic.AtomicInteger(); + + private static String uniqueTaskNum() { + return "TASKRD-IT-" + System.nanoTime() + "-" + SEQ.incrementAndGet(); + } + + @BeforeEach + void setUp() { + StpUtil.login("复习耗时集成测试用户", "test_device"); + } + + @Test + void reviewDuration_isPersistedAndAggregatedPerTask() { + String taskNum = uniqueTaskNum(); + String otherTaskNum = uniqueTaskNum(); + + insertRecall(taskNum, 300); + insertRecall(taskNum, 120); + insertRecall(otherTaskNum, 60); + + ReviewTaskStats stats = reviewService.getReviewTaskStats(taskNum); + assertEquals(420L, stats.getReviewDurationSeconds(), "同一任务应累计 300+120 秒"); + + ReviewTaskStats otherStats = reviewService.getReviewTaskStats(otherTaskNum); + assertEquals(60L, otherStats.getReviewDurationSeconds(), "不同任务之间不应串数据"); + } + + @Test + void reviewDuration_databaseDefaultIsZero() { + ReviewRecallRecordEntity record = new ReviewRecallRecordEntity(); + record.setTaskNum(uniqueTaskNum()); + record.setRecallContent("- 未计时回忆"); + record.setRecallRatio(0.0); + recallRecordMapper.insert(record); + + ReviewRecallRecordEntity reloaded = recallRecordMapper.selectById(record.getId()); + assertEquals(0, reloaded.getReviewDurationSeconds(), "未上报耗时时应落库为 0,而不是 null"); + } + + private void insertRecall(String taskNum, int durationSeconds) { + ReviewRecallRecordEntity record = new ReviewRecallRecordEntity(); + record.setTaskNum(taskNum); + record.setRecallContent("- 集成测试回忆"); + record.setRecallRatio(0.5); + record.setMatchedCount(1); + record.setMissedCount(1); + record.setExtraCount(0); + record.setReviewDurationSeconds(durationSeconds); + recallRecordMapper.insert(record); + } +} diff --git a/src/test/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImplTest.java b/src/test/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImplTest.java index bf5573d..141b7bd 100644 --- a/src/test/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImplTest.java +++ b/src/test/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImplTest.java @@ -3,6 +3,7 @@ package com.guo.learningprogresstracker.service.impl; import com.baomidou.mybatisplus.core.MybatisConfiguration; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.guo.learningprogresstracker.dto.ReviewFeedItem; +import com.guo.learningprogresstracker.dto.ReviewTaskStats; import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity; import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity; import com.guo.learningprogresstracker.entity.StudyReportsEntity; @@ -23,6 +24,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.time.LocalDateTime; import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; @@ -141,4 +143,32 @@ class ReviewServiceImplTest { assertEquals("Task", items.get(0).getTaskName()); } + @Test + void getReviewTaskStats_fillsReviewDurationFromAggregate() { + TaskEntity task = new TaskEntity(); + task.setTaskNum("T1"); + task.setTaskName("任务一"); + + StudySessionsEntity session = new StudySessionsEntity(); + session.setSessionNum("S1"); + session.setTaskNum("T1"); + session.setEffectiveTime(600); + session.setEffectivenessRatio(0.8); + session.setStartTime(LocalDateTime.now()); + + when(tasksMapper.selectList(any())).thenReturn(List.of(task)); + when(studySessionsMapper.selectList(any())).thenReturn(List.of(session)); + when(studyReportsMapper.selectList(any())).thenReturn(List.of()); + when(studyReportFragmentsMapper.selectList(any())).thenReturn(List.of()); + // 聚合结果里出现了不在任务列表中的 taskNum,应被忽略 + when(reviewRecallRecordMapper.selectMaps(any())).thenReturn(List.of( + Map.of("taskNum", "T1", "reviewDurationSeconds", 420L), + Map.of("taskNum", "T_UNKNOWN", "reviewDurationSeconds", 100L))); + + List stats = reviewService.getReviewTaskStats(); + + assertEquals(1, stats.size()); + assertEquals(600, stats.get(0).getEffectiveTime()); + assertEquals(420L, stats.get(0).getReviewDurationSeconds()); + } } diff --git a/src/test/java/com/guo/learningprogresstracker/utils/ReviewDurationToolTest.java b/src/test/java/com/guo/learningprogresstracker/utils/ReviewDurationToolTest.java new file mode 100644 index 0000000..5db6ae1 --- /dev/null +++ b/src/test/java/com/guo/learningprogresstracker/utils/ReviewDurationToolTest.java @@ -0,0 +1,75 @@ +package com.guo.learningprogresstracker.utils; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * 复习耗时结算口径测试(复习-07)。 + *

口径:从行为开始结算到结束;超过 5 分钟没有交互则截断到上一次交互时间。

+ */ +class ReviewDurationToolTest { + + private static final LocalDateTime START = LocalDateTime.of(2026, 10, 9, 10, 0, 0); + + @Test + void idleWindow_isFiveMinutes() { + assertEquals(300, ReviewDurationTool.IDLE_WINDOW_SECONDS); + } + + @Test + void resolveSeconds_withoutStartedAt_recordsNothing() { + assertEquals(0, ReviewDurationTool.resolveSeconds(null, START, 600, START.plusMinutes(10))); + } + + @Test + void resolveSeconds_withoutNow_recordsNothing() { + assertEquals(0, ReviewDurationTool.resolveSeconds(START, START.plusMinutes(1), 60, null)); + } + + @Test + void resolveSeconds_futureStartedAt_recordsNothing() { + assertEquals(0, ReviewDurationTool.resolveSeconds(START.plusMinutes(5), null, 60, START)); + } + + @Test + void resolveSeconds_keepsReportedSecondsWhenIdleIsWithinWindow() { + // 结束前 1 分钟仍在交互:空闲 <= 5 分钟,按前端上报值记账 + int seconds = ReviewDurationTool.resolveSeconds( + START, START.plusMinutes(19), 1140, START.plusMinutes(20)); + assertEquals(1140, seconds); + } + + @Test + void resolveSeconds_truncatesToLastInteractionWhenIdleBeyondWindow() { + // 10:03 之后挂机到 13:00:耗时截断到上一次交互,3 小时只剩 3 分钟 + int seconds = ReviewDurationTool.resolveSeconds( + START, START.plusMinutes(3), 3 * 3600, START.plusHours(3)); + assertEquals(180, seconds); + } + + @Test + void resolveSeconds_negativeReportedSecondsBecomesZero() { + assertEquals(0, ReviewDurationTool.resolveSeconds( + START, START.plusMinutes(1), -30, START.plusMinutes(2))); + } + + @Test + void resolveSeconds_reportedBeyondWallClockIsClamped() { + // 上报值不可信时以「开始到 now」的墙钟跨度为上限 + assertEquals(120, ReviewDurationTool.resolveSeconds(START, null, 99999, START.plusMinutes(2))); + } + + @Test + void resolveSeconds_missingReportedSecondsFallsBackToWallClock() { + assertEquals(450, ReviewDurationTool.resolveSeconds(START, null, null, START.plusSeconds(450))); + } + + @Test + void resolveSeconds_lastInteractionBeforeStartIsIgnored() { + assertEquals(600, ReviewDurationTool.resolveSeconds( + START, START.minusHours(1), 600, START.plusMinutes(10))); + } +}