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。
|
||||
|
||||
Reference in New Issue
Block a user