feat: 复习 Feed 随机取样改用主键区间并固定 actuator 清单
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user