perf: 覆盖率查询改为按任务分组取最新
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
package com.guo.learningprogresstracker.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务最近一次回忆对比的覆盖率(复习-04)。
|
||||||
|
*
|
||||||
|
* <p>原先按任务取「最近一条回忆记录」是把该任务的全部回忆记录(含体积较大的
|
||||||
|
* {@code compare_result} JSON)拉回内存再逐条取第一条。改为在 SQL 侧按任务分组取最新,
|
||||||
|
* 返回行数等于任务数,不随复习次数增长。</p>
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class TaskRecallRatio {
|
||||||
|
|
||||||
|
private String taskNum;
|
||||||
|
|
||||||
|
/** 最近一次回忆对比的覆盖率(0-1);记录里为空时按 0 处理 */
|
||||||
|
private Double recallRatio;
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.guo.learningprogresstracker.mapper;
|
package com.guo.learningprogresstracker.mapper;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
|
||||||
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
|
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
|
||||||
import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity;
|
import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
@@ -21,4 +22,13 @@ public interface ReviewRecallRecordMapper extends BaseMapper<ReviewRecallRecordE
|
|||||||
* @param taskNums 需要汇总的任务编号;为空时返回空列表
|
* @param taskNums 需要汇总的任务编号;为空时返回空列表
|
||||||
*/
|
*/
|
||||||
List<TaskReviewDuration> sumReviewDurationByTask(@Param("taskNums") Collection<String> taskNums);
|
List<TaskReviewDuration> sumReviewDurationByTask(@Param("taskNums") Collection<String> taskNums);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每个任务最近一条回忆记录的覆盖率(复习-04)。
|
||||||
|
* <p>语句见 {@code resources/mapper/ReviewRecallRecordMapper.xml};返回行数等于任务数,
|
||||||
|
* 不随复习次数增长,且不读取 {@code compare_result} 大字段。</p>
|
||||||
|
*
|
||||||
|
* @param taskNums 需要查询的任务编号;为空时返回空列表
|
||||||
|
*/
|
||||||
|
List<TaskRecallRatio> selectLatestRatioByTask(@Param("taskNums") Collection<String> taskNums);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.guo.learningprogresstracker.dto.IdRange;
|
|||||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||||
import com.guo.learningprogresstracker.dto.StudyReportDetail;
|
import com.guo.learningprogresstracker.dto.StudyReportDetail;
|
||||||
|
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
|
||||||
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
||||||
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
|
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
|
||||||
import com.guo.learningprogresstracker.dto.TaskSessionStats;
|
import com.guo.learningprogresstracker.dto.TaskSessionStats;
|
||||||
@@ -172,14 +173,10 @@ public class ReviewServiceImpl implements ReviewService {
|
|||||||
if (taskNums.isEmpty()) {
|
if (taskNums.isEmpty()) {
|
||||||
return Collections.emptyMap();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
List<ReviewRecallRecordEntity> records = reviewRecallRecordMapper.selectList(
|
// 复习-04:在 SQL 侧按任务分组取最新一条,只带回 任务编号 + 覆盖率
|
||||||
Wrappers.<ReviewRecallRecordEntity>lambdaQuery()
|
|
||||||
.in(ReviewRecallRecordEntity::getTaskNum, taskNums)
|
|
||||||
.orderByDesc(ReviewRecallRecordEntity::getCreatedTime));
|
|
||||||
Map<String, Double> result = new HashMap<>();
|
Map<String, Double> result = new HashMap<>();
|
||||||
for (ReviewRecallRecordEntity record : records) {
|
for (TaskRecallRatio row : reviewRecallRecordMapper.selectLatestRatioByTask(taskNums)) {
|
||||||
result.putIfAbsent(record.getTaskNum(),
|
result.put(row.getTaskNum(), row.getRecallRatio() == null ? 0D : row.getRecallRatio());
|
||||||
record.getRecallRatio() == null ? 0D : record.getRecallRatio());
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,35 @@
|
|||||||
<result property="reviewDurationSeconds" column="review_duration_seconds" jdbcType="BIGINT"/>
|
<result property="reviewDurationSeconds" column="review_duration_seconds" jdbcType="BIGINT"/>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
|
<!-- 显式列映射 -->
|
||||||
|
<resultMap id="TaskRecallRatioMap" type="com.guo.learningprogresstracker.dto.TaskRecallRatio">
|
||||||
|
<result property="taskNum" column="task_num" jdbcType="VARCHAR"/>
|
||||||
|
<result property="recallRatio" column="recall_ratio" jdbcType="DOUBLE"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
每个任务最近一条回忆记录的覆盖率(复习-04)。
|
||||||
|
用「按任务取最大 id」定位最新记录(id 自增,与录入顺序一致),
|
||||||
|
只返回 任务编号 + 覆盖率两列,不再把 compare_result 这类大字段读进内存。
|
||||||
|
租户条件由拦截器追加;自定义 SQL 需显式写 deleted = 0。
|
||||||
|
-->
|
||||||
|
<select id="selectLatestRatioByTask" resultMap="TaskRecallRatioMap">
|
||||||
|
SELECT r.task_num AS task_num,
|
||||||
|
r.recall_ratio AS recall_ratio
|
||||||
|
FROM review_recall_records r
|
||||||
|
JOIN (SELECT task_num, MAX(id) AS latest_id
|
||||||
|
FROM review_recall_records
|
||||||
|
WHERE deleted = 0
|
||||||
|
<if test="taskNums != null and !taskNums.isEmpty()">
|
||||||
|
AND task_num IN
|
||||||
|
<foreach collection="taskNums" item="num" open="(" separator="," close=")">
|
||||||
|
#{num}
|
||||||
|
</foreach>
|
||||||
|
</if>
|
||||||
|
GROUP BY task_num) latest ON r.id = latest.latest_id
|
||||||
|
WHERE r.deleted = 0
|
||||||
|
</select>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
按任务汇总复习耗时(复习-07)。
|
按任务汇总复习耗时(复习-07)。
|
||||||
注意:自定义 SQL 不会被 @TableLogic 自动追加 deleted 条件,必须显式写出;
|
注意:自定义 SQL 不会被 @TableLogic 自动追加 deleted 条件,必须显式写出;
|
||||||
|
|||||||
+125
@@ -0,0 +1,125 @@
|
|||||||
|
package com.guo.learningprogresstracker.service.impl;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
|
||||||
|
import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity;
|
||||||
|
import com.guo.learningprogresstracker.mapper.ReviewRecallRecordMapper;
|
||||||
|
import com.guo.learningprogresstracker.utils.GenerateNumTool;
|
||||||
|
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 java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 覆盖率查询的落库集成测试(复习-04)。
|
||||||
|
*
|
||||||
|
* <p>验收:该查询返回行数等于<b>任务数</b>而非回忆记录数——即不再把某个任务的全部
|
||||||
|
* 回忆记录(含 {@code compare_result} 大字段)拉回内存再逐条取最新。</p>
|
||||||
|
*/
|
||||||
|
@SpringBootTest
|
||||||
|
class RecallRatioQueryIntegrationTest {
|
||||||
|
|
||||||
|
private static final String USER = "覆盖率查询集成测试用户";
|
||||||
|
|
||||||
|
private static final AtomicInteger SEQ = new AtomicInteger();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ReviewRecallRecordMapper recallRecordMapper;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
StpUtil.login(USER, "test_device");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String uniqueTaskNum() {
|
||||||
|
return "TASKRATIO-IT-" + System.nanoTime() + "-" + SEQ.incrementAndGet();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertRecall(String taskNum, double ratio, String compareResult) {
|
||||||
|
ReviewRecallRecordEntity record = new ReviewRecallRecordEntity();
|
||||||
|
record.setTaskNum(taskNum);
|
||||||
|
record.setRecallContent("- 回忆");
|
||||||
|
record.setRecallRatio(ratio);
|
||||||
|
record.setMatchedCount(1);
|
||||||
|
record.setMissedCount(1);
|
||||||
|
record.setExtraCount(0);
|
||||||
|
record.setCompareResult(compareResult);
|
||||||
|
recallRecordMapper.insert(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void returnsOneRowPerTaskRegardlessOfRecallCount() {
|
||||||
|
String taskA = uniqueTaskNum();
|
||||||
|
String taskB = uniqueTaskNum();
|
||||||
|
for (int i = 0; i < 12; i++) {
|
||||||
|
insertRecall(taskA, 0.1 * i, "{\"big\":\"" + "x".repeat(200) + "\"}");
|
||||||
|
}
|
||||||
|
insertRecall(taskB, 0.9, "{}");
|
||||||
|
|
||||||
|
List<TaskRecallRatio> rows = recallRecordMapper.selectLatestRatioByTask(Set.of(taskA, taskB));
|
||||||
|
|
||||||
|
assertEquals(2, rows.size(), "两个任务即使有 13 条回忆记录,也只应返回 2 行");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void returnsLatestRatioPerTask() {
|
||||||
|
String taskNum = uniqueTaskNum();
|
||||||
|
insertRecall(taskNum, 0.20, null);
|
||||||
|
insertRecall(taskNum, 0.55, null);
|
||||||
|
insertRecall(taskNum, 0.80, null);
|
||||||
|
|
||||||
|
List<TaskRecallRatio> rows = recallRecordMapper.selectLatestRatioByTask(Set.of(taskNum));
|
||||||
|
|
||||||
|
assertEquals(1, rows.size());
|
||||||
|
assertEquals(0.80, rows.get(0).getRecallRatio(), 0.0001, "应取最近一次(最后插入)的覆盖率");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void excludesLogicallyDeletedRecordsAndOtherTenants() {
|
||||||
|
String taskNum = uniqueTaskNum();
|
||||||
|
ReviewRecallRecordEntity deleted = new ReviewRecallRecordEntity();
|
||||||
|
deleted.setTaskNum(taskNum);
|
||||||
|
deleted.setRecallContent("- 将被删除");
|
||||||
|
deleted.setRecallRatio(0.99);
|
||||||
|
deleted.setMatchedCount(0);
|
||||||
|
deleted.setMissedCount(1);
|
||||||
|
deleted.setExtraCount(0);
|
||||||
|
recallRecordMapper.insert(deleted);
|
||||||
|
recallRecordMapper.deleteById(deleted.getId());
|
||||||
|
|
||||||
|
insertRecall(taskNum, 0.30, null);
|
||||||
|
|
||||||
|
List<TaskRecallRatio> rows = recallRecordMapper.selectLatestRatioByTask(Set.of(taskNum));
|
||||||
|
assertEquals(1, rows.size());
|
||||||
|
assertEquals(0.30, rows.get(0).getRecallRatio(), 0.0001, "已删除的记录不应参与取最新");
|
||||||
|
|
||||||
|
StpUtil.login("覆盖率查询集成测试另一个用户", "test_device");
|
||||||
|
assertTrue(recallRecordMapper.selectLatestRatioByTask(Set.of(taskNum)).isEmpty(),
|
||||||
|
"覆盖率查询必须受租户隔离约束");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void emptyTaskSet_returnsEmpty() {
|
||||||
|
assertTrue(recallRecordMapper.selectLatestRatioByTask(Set.of()).isEmpty());
|
||||||
|
assertTrue(recallRecordMapper.selectLatestRatioByTask(null).isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void taskWithoutAnyRecall_isAbsentFromResult() {
|
||||||
|
String taskNum = uniqueTaskNum();
|
||||||
|
assertTrue(recallRecordMapper.selectLatestRatioByTask(Set.of(taskNum)).isEmpty(),
|
||||||
|
"从未回忆过的任务不返回行,由调用方按「最需要复习」处理");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generatedTaskNumIsUsable() {
|
||||||
|
assertTrue(GenerateNumTool.generateNum("TASKRATIO").startsWith("TASKRATIO-"));
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-6
@@ -6,6 +6,7 @@ import com.guo.learningprogresstracker.dto.IdRange;
|
|||||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||||
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
||||||
|
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
|
||||||
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
|
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
|
||||||
import com.guo.learningprogresstracker.dto.TaskSessionStats;
|
import com.guo.learningprogresstracker.dto.TaskSessionStats;
|
||||||
import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity;
|
import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity;
|
||||||
@@ -90,16 +91,15 @@ class ReviewServiceImplTest {
|
|||||||
taskHigh.setTaskNum("T_HIGH");
|
taskHigh.setTaskNum("T_HIGH");
|
||||||
taskHigh.setTaskName("高掌握任务");
|
taskHigh.setTaskName("高掌握任务");
|
||||||
|
|
||||||
ReviewRecallRecordEntity highRatioRecord = new ReviewRecallRecordEntity();
|
|
||||||
highRatioRecord.setTaskNum("T_HIGH");
|
|
||||||
highRatioRecord.setRecallRatio(1.0);
|
|
||||||
highRatioRecord.setCreatedTime(now);
|
|
||||||
|
|
||||||
// 复习-03:随机取样改为「主键区间 + 顺序取窗口」,因此需要提供 id 区间
|
// 复习-03:随机取样改为「主键区间 + 顺序取窗口」,因此需要提供 id 区间
|
||||||
when(studyReportFragmentsMapper.selectIdRange()).thenReturn(idRange(1, 2));
|
when(studyReportFragmentsMapper.selectIdRange()).thenReturn(idRange(1, 2));
|
||||||
when(studyReportFragmentsMapper.selectList(any())).thenReturn(List.of(oldLowMastery, freshHighMastery));
|
when(studyReportFragmentsMapper.selectList(any())).thenReturn(List.of(oldLowMastery, freshHighMastery));
|
||||||
when(tasksMapper.selectList(any())).thenReturn(List.of(taskLow, taskHigh));
|
when(tasksMapper.selectList(any())).thenReturn(List.of(taskLow, taskHigh));
|
||||||
when(reviewRecallRecordMapper.selectList(any())).thenReturn(List.of(highRatioRecord));
|
// 复习-04:覆盖率改由 SQL 按任务分组取最新
|
||||||
|
TaskRecallRatio highRatio = new TaskRecallRatio();
|
||||||
|
highRatio.setTaskNum("T_HIGH");
|
||||||
|
highRatio.setRecallRatio(1.0);
|
||||||
|
when(reviewRecallRecordMapper.selectLatestRatioByTask(any())).thenReturn(List.of(highRatio));
|
||||||
|
|
||||||
List<ReviewFeedItem> items = reviewService.getReviewFeed(1, "smart");
|
List<ReviewFeedItem> items = reviewService.getReviewFeed(1, "smart");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user