perf: Feed 只返回摘要与原文长度

This commit is contained in:
2026-10-09 19:35:15 +08:00
parent 4da3b78fa4
commit dbeafba23c
3 changed files with 166 additions and 2 deletions
@@ -27,8 +27,18 @@ public class ReviewFeedItem {
private String taskName;
/**
* 内容摘要(复习-05):Feed 只回摘要,全文由详情接口按需拉取。
* <p>列表只用来触发回忆,全文既拖慢滚动也会让用户跳过「先尝试回忆」这一步。</p>
*/
private String content;
/** 原文总长度(字符数),供界面标注「全文 N 字」 */
private int contentLength;
/** 是否为截断后的摘要 */
private boolean contentTruncated;
private LocalDateTime createdTime;
/** 仅碎片有意义:内容归属为空,等待归类 */
@@ -59,6 +59,8 @@ public class ReviewServiceImpl implements ReviewService {
private static final String SOURCE_FRAGMENT = "FRAGMENT";
/** 待归类分组在内部的键名(不会作为任务编号出现在响应里,响应里 taskNum 为 null) */
private static final String PENDING_OWNERSHIP_KEY = "__PENDING__";
/** Feed 条目只返回这么长的摘要;全文由详情接口按需拉取(复习-05) */
private static final int FEED_SUMMARY_MAX_LENGTH = 200;
/** 时间效应封顶天数 */
private static final int AGE_CEILING_DAYS = 90;
/** 「稍后再看」的权重倍数(提高出现概率) */
@@ -517,7 +519,7 @@ public class ReviewServiceImpl implements ReviewService {
ReviewFeedItem item = new ReviewFeedItem();
item.setId(r.getId());
item.setSourceType(SOURCE_REPORT);
item.setContent(reportDisplayText(r));
applySummary(item, reportDisplayText(r));
item.setCreatedTime(r.getCreatedTime());
item.setContextType(FragmentContextTypeEnum.SESSION.name());
item.setContextNum(r.getSessionNum());
@@ -531,7 +533,7 @@ public class ReviewServiceImpl implements ReviewService {
ReviewFeedItem item = new ReviewFeedItem();
item.setId(f.getId());
item.setSourceType(SOURCE_FRAGMENT);
item.setContent(f.getContent());
applySummary(item, f.getContent());
item.setCreatedTime(f.getCreatedTime());
item.setContextType(f.getContextType());
item.setContextNum(f.getContextNum());
@@ -553,6 +555,25 @@ public class ReviewServiceImpl implements ReviewService {
.collect(Collectors.toList());
}
/**
* 把原文放进 Feed 条目:只保留摘要与总长度(复习-05)。
* <p>摘要把换行压成空格,避免多行内容在列表里撑高卡片。</p>
*/
private static void applySummary(ReviewFeedItem item, String rawContent) {
String full = rawContent == null ? "" : rawContent;
item.setContentLength(full.length());
if (full.length() <= FEED_SUMMARY_MAX_LENGTH) {
item.setContent(full);
item.setContentTruncated(false);
return;
}
String summary = full.substring(0, FEED_SUMMARY_MAX_LENGTH)
.replaceAll("\\s+", " ")
.trim();
item.setContent(summary);
item.setContentTruncated(true);
}
/**
* 报告在列表/Feed 中的展示文本:结构化报告由三段组成,这里按段拼接。
*/
@@ -0,0 +1,133 @@
package com.guo.learningprogresstracker.service.impl;
import cn.dev33.satoken.stp.StpUtil;
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.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.*;
/**
* Feed 摘要的落库集成测试(复习-05)。
*
* <p>验收:Feed 响应体积显著下降且展示行为不变——列表只回摘要与总长度,
* 全文由详情接口按需拉取(前端本来就只展示前 120 字)。</p>
*/
@SpringBootTest
class ReviewFeedSummaryIntegrationTest {
private static final String USER = "Feed摘要集成测试用户";
private static final AtomicInteger SEQ = new AtomicInteger();
/** 与 ReviewServiceImpl.FEED_SUMMARY_MAX_LENGTH 一致 */
private static final int SUMMARY_MAX = 200;
@Autowired
private ReviewService reviewService;
@Autowired
private StudyReportFragmentsMapper fragmentsMapper;
@Autowired
private StudySessionsMapper studySessionsMapper;
@BeforeEach
void setUp() {
StpUtil.login(USER, "test_device");
}
/** 造一条碎片并把“独占的”内容标记放在开头,便于在结果里定位 */
private StudyReportFragmentsEntity saveFragment(String uniquePrefix, int totalLength) {
String taskNum = "TASKSUM-IT-" + System.nanoTime() + "-" + SEQ.incrementAndGet();
StudySessionsEntity session = new StudySessionsEntity();
session.setSessionNum(GenerateNumTool.generateNum("SESSIONSUM"));
session.setTaskNum(taskNum);
session.setStartTime(LocalDateTime.now());
session.setLastStartTime(LocalDateTime.now());
session.setSessionState("ENDED");
studySessionsMapper.insert(session);
StudyReportFragmentsEntity fragment = new StudyReportFragmentsEntity();
fragment.setContextType("SESSION");
fragment.setContextNum(session.getSessionNum());
fragment.setOwnerTaskNum(taskNum);
fragment.setContent(uniquePrefix + "x".repeat(Math.max(0, totalLength - uniquePrefix.length())));
fragmentsMapper.insert(fragment);
return fragment;
}
private ReviewFeedItem findItem(String prefix) {
List<ReviewFeedItem> feed = reviewService.getReviewFeed(100, "recent");
return feed.stream()
.filter(item -> item.getContent() != null && item.getContent().startsWith(prefix))
.findFirst()
.orElse(null);
}
@Test
void longContent_isSummarizedButLengthIsReported() {
String prefix = "LONGSUMMARY-" + System.nanoTime() + "-";
int total = 5000;
saveFragment(prefix, total);
ReviewFeedItem item = findItem(prefix);
assertNotNull(item, "新建的碎片应出现在 Feed 里");
assertEquals(total, item.getContentLength(), "应报告原文总长度");
assertTrue(item.isContentTruncated(), "超长内容应被标记为截断");
assertTrue(item.getContent().length() <= SUMMARY_MAX,
"Feed 条目内容不应超过摘要上限,实际 " + item.getContent().length());
}
@Test
void shortContent_isReturnedAsIs() {
String prefix = "SHORTSUMMARY-" + System.nanoTime() + "-";
String content = prefix + "短内容";
saveFragment(content, content.length());
ReviewFeedItem item = findItem(prefix);
assertNotNull(item);
assertEquals(content.length(), item.getContentLength());
assertFalse(item.isContentTruncated());
assertEquals(content, item.getContent(), "短内容原样返回,展示行为不变");
}
@Test
void multiLineContent_collapsesWhitespaceInSummary() {
String prefix = "MULTILINESUMMARY-" + System.nanoTime() + "-";
// 超过上限且含多行,摘要里不应残留换行
saveFragment(prefix + "第一行\n第二行\n" + "y".repeat(SUMMARY_MAX * 2), SUMMARY_MAX * 3);
ReviewFeedItem item = findItem(prefix);
assertNotNull(item);
assertTrue(item.isContentTruncated());
assertFalse(item.getContent().contains("\n"), "摘要把换行压成空格,避免撑高列表卡片");
}
@Test
void feedPayloadStaysBounded() {
// 造 20 条超长碎片,Feed 的总内容量应远小于原始总量
for (int i = 0; i < 20; i++) {
saveFragment("BULKSUMMARY-" + i + "-" + System.nanoTime() + "-", 3000);
}
List<ReviewFeedItem> feed = reviewService.getReviewFeed(100, "recent");
int summarizedChars = feed.stream().mapToInt(item -> item.getContent().length()).sum();
int originalChars = feed.stream().mapToInt(ReviewFeedItem::getContentLength).sum();
assertTrue(originalChars > summarizedChars * 5,
"Feed 响应体积应显著下降:原文 " + originalChars + " 字,摘要 " + summarizedChars + " 字");
}
}