feat: 复习 Feed 随机取样改用主键区间并固定 actuator 清单
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.guo.learningprogresstracker.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 主键区间的聚合结果(复习-03)。
|
||||
* <p>随机取样需要知道 id 的跨度,才能用「随机起点 + 顺序取窗口」替代 {@code ORDER BY RAND()}。
|
||||
* 单列聚合可用 {@code selectObjs},这里需要 MIN 与 MAX 两列,因此用显式结果类 + 自定义 SQL。</p>
|
||||
*/
|
||||
@Data
|
||||
public class IdRange {
|
||||
|
||||
private Integer minId;
|
||||
|
||||
private Integer maxId;
|
||||
|
||||
/** 区间内是否存在记录 */
|
||||
public boolean isEmpty() {
|
||||
return minId == null || maxId == null;
|
||||
}
|
||||
|
||||
/** 区间宽度(含两端);空区间返回 0 */
|
||||
public long span() {
|
||||
return isEmpty() ? 0L : (long) maxId - minId + 1L;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.guo.learningprogresstracker.mapper;
|
||||
|
||||
import com.guo.learningprogresstracker.dto.IdRange;
|
||||
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
||||
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
@@ -25,6 +26,12 @@ public interface StudyReportFragmentsMapper extends BaseMapper<StudyReportFragme
|
||||
* @param taskNums 需要统计的任务编号;为空时返回空列表
|
||||
*/
|
||||
List<TaskContentCount> countFragmentsByTask(@Param("taskNums") Collection<String> taskNums);
|
||||
|
||||
/**
|
||||
* 取知识碎片的主键区间(复习-03):供随机取样计算随机起点。
|
||||
* <p>语句见 {@code resources/mapper/StudyReportFragmentsMapper.xml};无数据时两列为 null。</p>
|
||||
*/
|
||||
IdRange selectIdRange();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -36,6 +36,11 @@ public class AiServiceClient {
|
||||
private String url;
|
||||
/** 超时秒数(等待 AI 任务完成的最长时间) */
|
||||
private int timeoutSeconds = 600;
|
||||
/**
|
||||
* 入站令牌(安全-03):lpt-ai 配置了 LPT_AI_TOKEN 时,本客户端必须携带同一令牌。
|
||||
* 为空表示不携带,兼容未启用令牌的部署。
|
||||
*/
|
||||
private String token;
|
||||
|
||||
private HttpClient httpClient;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
@@ -56,6 +61,11 @@ public class AiServiceClient {
|
||||
return url != null && !url.isBlank();
|
||||
}
|
||||
|
||||
/** 头里必须带值,未配置令牌时给空串(lpt-ai 未启用令牌时会忽略它) */
|
||||
private String authToken() {
|
||||
return token == null ? "" : token.trim();
|
||||
}
|
||||
|
||||
// ============ 公开 API(签名不变,内部改为 submit + poll)============
|
||||
|
||||
/**
|
||||
@@ -144,6 +154,7 @@ public class AiServiceClient {
|
||||
.uri(URI.create(url + "/ai/tasks"))
|
||||
.timeout(Duration.ofSeconds(30)) // 提交本身不应超时
|
||||
.header("Content-Type", "application/json")
|
||||
.header("x-api-token", authToken())
|
||||
.POST(HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(body)))
|
||||
.build();
|
||||
|
||||
@@ -182,6 +193,7 @@ public class AiServiceClient {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url + "/ai/tasks/" + taskId))
|
||||
.timeout(Duration.ofSeconds(10))
|
||||
.header("x-api-token", authToken())
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.guo.learningprogresstracker.dto.IdRange;
|
||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
||||
@@ -27,6 +28,7 @@ import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -59,8 +61,7 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
boolean random = RANDOM_MODE.equalsIgnoreCase(mode);
|
||||
|
||||
List<StudyReportFragmentsEntity> fragments = random
|
||||
? studyReportFragmentsMapper.selectList(Wrappers.<StudyReportFragmentsEntity>lambdaQuery()
|
||||
.last("ORDER BY RAND() LIMIT " + safeLimit))
|
||||
? randomFragments(safeLimit)
|
||||
: studyReportFragmentsMapper.selectList(Wrappers.<StudyReportFragmentsEntity>lambdaQuery()
|
||||
.orderByDesc(StudyReportFragmentsEntity::getCreatedTime)
|
||||
.last("LIMIT " + safeLimit));
|
||||
@@ -72,6 +73,47 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
return items.stream().limit(safeLimit).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机取样知识碎片(复习-03)。
|
||||
*
|
||||
* <p>不使用 {@code ORDER BY RAND()}:那在 MySQL 上是全表随机排序,一次 Feed 请求会触发两次。
|
||||
* 改为「随机起点 + 顺序取窗口」:先取主键区间,随机选一个起点,再按 id 顺序取够数量;
|
||||
* 不足时从头回绕补齐(id 与创建时间同向,回绕等价于取更早的内容)。删除造成的 id 空洞会让
|
||||
* 靠后的内容被略微偏重,对「偶遇式」的 Feed 来说可以接受。</p>
|
||||
*/
|
||||
private List<StudyReportFragmentsEntity> randomFragments(int limit) {
|
||||
IdRange range = studyReportFragmentsMapper.selectIdRange();
|
||||
if (range == null || range.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
long start = range.getMinId()
|
||||
+ (long) (ThreadLocalRandom.current().nextDouble() * range.span());
|
||||
|
||||
// 用 LinkedHashMap 去重:回绕补齐时可能取到与首轮相同的行
|
||||
Map<Integer, StudyReportFragmentsEntity> picked = new LinkedHashMap<>();
|
||||
collectFrom(start, limit, picked);
|
||||
if (picked.size() < limit) {
|
||||
collectFrom(range.getMinId(), limit - picked.size(), picked);
|
||||
}
|
||||
return new ArrayList<>(picked.values());
|
||||
}
|
||||
|
||||
/** 从 startId 起按 id 顺序取至多 count 条,按主键去重后并入 picked */
|
||||
private void collectFrom(long startId, int count, Map<Integer, StudyReportFragmentsEntity> picked) {
|
||||
if (count <= 0) {
|
||||
return;
|
||||
}
|
||||
List<StudyReportFragmentsEntity> window = studyReportFragmentsMapper.selectList(
|
||||
Wrappers.<StudyReportFragmentsEntity>lambdaQuery()
|
||||
.ge(StudyReportFragmentsEntity::getId, startId)
|
||||
.orderByAsc(StudyReportFragmentsEntity::getId)
|
||||
.last("LIMIT " + count));
|
||||
for (StudyReportFragmentsEntity fragment : window) {
|
||||
picked.putIfAbsent(fragment.getId(), fragment);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 智能模式:加权随机采样,让更需要复习的内容有更高概率出现。
|
||||
* 权重 = 时间衰减(越久未见权重越高) × 任务回忆掌握度(覆盖率越低权重越高)。
|
||||
@@ -80,9 +122,7 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
private List<ReviewFeedItem> getSmartFeed(int safeLimit) {
|
||||
int candidateLimit = Math.min(safeLimit * SMART_CANDIDATE_MULTIPLIER, 500);
|
||||
|
||||
List<StudyReportFragmentsEntity> fragments = studyReportFragmentsMapper.selectList(
|
||||
Wrappers.<StudyReportFragmentsEntity>lambdaQuery()
|
||||
.last("ORDER BY RAND() LIMIT " + candidateLimit));
|
||||
List<StudyReportFragmentsEntity> fragments = randomFragments(candidateLimit);
|
||||
|
||||
List<ReviewFeedItem> candidates = mergeAndConvert(List.of(), fragments);
|
||||
if (candidates.size() <= safeLimit) {
|
||||
|
||||
@@ -48,17 +48,24 @@ sa-token:
|
||||
is-log: true
|
||||
|
||||
# 健康检查配置
|
||||
# 安全-02:可访问的端点清单必须显式固定。
|
||||
# /actuator/** 不受 SaInterceptor 约束(未登录也返回 200),所以这里是唯一的口子:
|
||||
# 只暴露探针需要的 health 与 info;放开 metrics/prometheus/env 会连带把内部信息(含配置项)公开。
|
||||
# 需要临时排查时在本机用 SPRING_PROFILES_ACTIVE=local 覆盖,不要改这里。
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: health,info # 或 "*"
|
||||
include: health,info
|
||||
|
||||
# lpt-ai 独立 AI 服务(可选,未配置时降级为内置规则引擎)
|
||||
lpt:
|
||||
ai-service:
|
||||
url: http://localhost:5199
|
||||
timeout-seconds: 600
|
||||
# 安全-03:与 lpt-ai 共用的入站令牌;留空表示 lpt-ai 未启用令牌校验。
|
||||
# 部署清单用 LPT_AI-SERVICE_TOKEN(secret lpt-secrets/lpt-ai-token,optional)注入
|
||||
token: ""
|
||||
# 学习会话的时间口径(会话-04):三类阈值集中在这里,改口径不需要改代码
|
||||
study-time:
|
||||
# 番茄钟长度(分钟)
|
||||
|
||||
@@ -26,6 +26,24 @@
|
||||
<result property="contentCount" column="content_count" jdbcType="BIGINT"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 显式列映射 -->
|
||||
<resultMap id="IdRangeMap" type="com.guo.learningprogresstracker.dto.IdRange">
|
||||
<result property="minId" column="min_id" jdbcType="INTEGER"/>
|
||||
<result property="maxId" column="max_id" jdbcType="INTEGER"/>
|
||||
</resultMap>
|
||||
|
||||
<!--
|
||||
取主键区间(复习-03):随机取样用它算「随机起点 + 顺序取窗口」。
|
||||
租户条件由拦截器追加;自定义 SQL 不会被 @TableLogic 追加条件,故显式写 deleted = 0。
|
||||
聚合无 GROUP BY,恒返回一行(无数据时两列为 NULL)。
|
||||
-->
|
||||
<select id="selectIdRange" resultMap="IdRangeMap">
|
||||
SELECT MIN(id) AS min_id,
|
||||
MAX(id) AS max_id
|
||||
FROM study_report_fragments
|
||||
WHERE deleted = 0
|
||||
</select>
|
||||
|
||||
<!--
|
||||
按内容归属统计任务下的碎片条数(数据-02)。
|
||||
两表都含 created_by,租户条件由拦截器分别追加;自定义 SQL 需显式写 deleted = 0。
|
||||
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.guo.learningprogresstracker.dto.IdRange;
|
||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
||||
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
|
||||
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
|
||||
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
|
||||
import com.guo.learningprogresstracker.service.ReviewService;
|
||||
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.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 复习 Feed 随机取样的落库集成测试(复习-03)。
|
||||
*
|
||||
* <p>原实现用 {@code ORDER BY RAND()}(MySQL 全表随机排序,一次请求两次)。
|
||||
* 现在改为「主键区间 + 顺序取窗口 + 回绕补齐」,这里守住它的三个行为:
|
||||
* 取满数量、页内不重复、以及真的随机。</p>
|
||||
*/
|
||||
@SpringBootTest
|
||||
class ReviewFeedSamplingIntegrationTest {
|
||||
|
||||
private static final String USER = "复习取样集成测试用户";
|
||||
|
||||
private static final AtomicInteger SEQ = new AtomicInteger();
|
||||
|
||||
@Autowired
|
||||
private ReviewService reviewService;
|
||||
|
||||
@Autowired
|
||||
private StudyReportFragmentsMapper fragmentsMapper;
|
||||
|
||||
@Autowired
|
||||
private StudySessionsMapper studySessionsMapper;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StpUtil.login(USER, "test_device");
|
||||
}
|
||||
|
||||
/** 造 sessionNum → 碎片,返回本次使用的会话编号 */
|
||||
private String seedFragments(int count) {
|
||||
String taskNum = "TASKSAMPLE-IT-" + System.nanoTime() + "-" + SEQ.incrementAndGet();
|
||||
StudySessionsEntity session = new StudySessionsEntity();
|
||||
session.setSessionNum(GenerateNumTool.generateNum("SESSIONSAMPLE"));
|
||||
session.setTaskNum(taskNum);
|
||||
session.setStartTime(java.time.LocalDateTime.now());
|
||||
session.setLastStartTime(java.time.LocalDateTime.now());
|
||||
session.setSessionState("ENDED");
|
||||
studySessionsMapper.insert(session);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
StudyReportFragmentsEntity fragment = new StudyReportFragmentsEntity();
|
||||
fragment.setSessionNum(session.getSessionNum());
|
||||
fragment.setContent("碎片-" + i);
|
||||
fragmentsMapper.insert(fragment);
|
||||
}
|
||||
return session.getSessionNum();
|
||||
}
|
||||
|
||||
@Test
|
||||
void idRange_reflectsCurrentTenantOnly() {
|
||||
seedFragments(3);
|
||||
|
||||
IdRange range = fragmentsMapper.selectIdRange();
|
||||
|
||||
assertFalse(range.isEmpty(), "有碎片时区间不应为空");
|
||||
assertTrue(range.getMaxId() >= range.getMinId());
|
||||
assertTrue(range.span() >= 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void randomMode_returnsRequestedCountWithoutDuplicates() {
|
||||
seedFragments(20);
|
||||
|
||||
for (int round = 0; round < 20; round++) {
|
||||
List<ReviewFeedItem> items = reviewService.getReviewFeed(5, "random");
|
||||
|
||||
assertEquals(5, items.size(), "第 " + round + " 轮应取满 5 条");
|
||||
Set<Integer> ids = new HashSet<>();
|
||||
for (ReviewFeedItem item : items) {
|
||||
assertTrue(ids.add(item.getId()), "同一页内不应出现重复条目");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void randomMode_isActuallyRandom() {
|
||||
seedFragments(60);
|
||||
|
||||
Set<Integer> seen = new HashSet<>();
|
||||
for (int round = 0; round < 40; round++) {
|
||||
reviewService.getReviewFeed(3, "random").forEach(item -> seen.add(item.getId()));
|
||||
}
|
||||
|
||||
// 样本池 60 条、每次取 3 条并取 40 轮:固定取样只会看到 3 个 id
|
||||
assertTrue(seen.size() > 3, "随机取样不应每次都取到同一批内容,实际去重后 " + seen.size() + " 条");
|
||||
}
|
||||
|
||||
@Test
|
||||
void randomMode_wrapsAroundWhenWindowRunsOut() {
|
||||
// 取满全部:起点落在末尾时剩余窗口不足,必须从头回绕补齐
|
||||
seedFragments(6);
|
||||
|
||||
for (int round = 0; round < 10; round++) {
|
||||
List<ReviewFeedItem> items = reviewService.getReviewFeed(6, "random");
|
||||
assertEquals(6, items.size(), "回绕补齐后应取满,第 " + round + " 轮");
|
||||
Set<Integer> ids = new HashSet<>();
|
||||
items.forEach(item -> ids.add(item.getId()));
|
||||
assertEquals(6, ids.size(), "回绕补齐也不应产生重复");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void smartMode_stillWorksOnTopOfRandomWindow() {
|
||||
seedFragments(30);
|
||||
|
||||
List<ReviewFeedItem> items = reviewService.getReviewFeed(4, "smart");
|
||||
|
||||
assertEquals(4, items.size());
|
||||
Set<Integer> ids = new HashSet<>();
|
||||
items.forEach(item -> ids.add(item.getId()));
|
||||
assertEquals(4, ids.size());
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,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.IdRange;
|
||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
||||
@@ -97,6 +98,8 @@ class ReviewServiceImplTest {
|
||||
highRatioRecord.setRecallRatio(1.0);
|
||||
highRatioRecord.setCreatedTime(now);
|
||||
|
||||
// 复习-03:随机取样改为「主键区间 + 顺序取窗口」,因此需要提供 id 区间
|
||||
when(studyReportFragmentsMapper.selectIdRange()).thenReturn(idRange(1, 2));
|
||||
when(studyReportFragmentsMapper.selectList(any())).thenReturn(List.of(oldLowMastery, freshHighMastery));
|
||||
when(studySessionsMapper.selectList(any())).thenReturn(List.of(sessionLow, sessionHigh));
|
||||
when(tasksMapper.selectList(any())).thenReturn(List.of(taskLow, taskHigh));
|
||||
@@ -181,6 +184,13 @@ class ReviewServiceImplTest {
|
||||
assertEquals(420L, stats.get(0).getReviewDurationSeconds());
|
||||
}
|
||||
|
||||
private static IdRange idRange(int minId, int maxId) {
|
||||
IdRange range = new IdRange();
|
||||
range.setMinId(minId);
|
||||
range.setMaxId(maxId);
|
||||
return range;
|
||||
}
|
||||
|
||||
private static TaskContentCount count(String taskNum, Long value) {
|
||||
TaskContentCount row = new TaskContentCount();
|
||||
row.setTaskNum(taskNum);
|
||||
|
||||
Reference in New Issue
Block a user