fix(telemetry): P-OPN-08 评审P2整改——公开统计下游异常软降级零汇总(契约永不报错·对齐list非脆失败)+测试

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
lili 2026-06-18 13:56:42 -07:00
parent c7a58b8c45
commit 5f2d0021fd
2 changed files with 28 additions and 2 deletions

View File

@ -124,11 +124,18 @@ public class GameStatServiceImpl implements GameStatService {
// 数据源经 project-api:对该创作者 status==4 作品聚合(发布数/累计试玩/累计点赞)——发布态权威 + play_count/like_count 计数列均在 project,
// 故聚合收敛于 project,不读本模块日聚合表(避免 telemetry 日聚合再 join project 发布态的脆弱跨表)。
// 经对方 -api 的 CommonResult.getCheckedData() 解包(失败抛业务异常,不静默吞)。
CreatorPublicSummaryDTO dto = projectApi.getCreatorPublishedSummary(creatorId).getCheckedData();
PublicCreatorSummaryRespVO vo = new PublicCreatorSummaryRespVO();
vo.setCreatorId(creatorId); // 回填目标创作者 ID(作主页归属锚,必返)
// 公开主页只读聚合:契约「无作品→计数恒 0,永不报错」。下游(project)异常时【软降级零汇总】,
// 不把瞬时下游故障变 5xx(与 list 端点的非脆失败语义对齐;评审 P2 整改)。失败/返回 null 统一兜零。
CreatorPublicSummaryDTO dto;
try {
dto = projectApi.getCreatorPublishedSummary(creatorId).getCheckedData();
} catch (Exception e) {
log.warn("[getPublicCreatorSummary] project 侧聚合失败,软降级零汇总 creatorId={}", creatorId, e);
dto = null;
}
if (dto == null) {
// 防御性兜底:project 侧约定永不返回 null,此处仍兜零值,保证对外各计数非 null
vo.setPublishedCount(0L);
vo.setTotalPlayCount(0L);
vo.setTotalLikeCount(0L);

View File

@ -236,6 +236,25 @@ class GameStatServiceImplTest extends BaseMockitoUnitTest {
assertEquals(0L, vo.getTotalLikeCount());
}
/**
* 公开统计:project 侧下游异常(getCheckedData 解包失败 / 瞬时 DB 错)→ 软降级零汇总,
* 绝不把 5xx 抛给匿名公开页(契约「永不报错」;评审 P2 整改,与 list 端点非脆失败对齐)。
*/
@Test
void testGetPublicCreatorSummary_downstreamError_softDegradesToZero() {
Long targetCreator = 11L;
when(projectApi.getCreatorPublishedSummary(targetCreator))
.thenThrow(new RuntimeException("downstream boom"));
// 不抛异常:软降级为零汇总(creatorId 仍必返)
PublicCreatorSummaryRespVO vo = gameStatService.getPublicCreatorSummary(targetCreator);
assertEquals(targetCreator, vo.getCreatorId());
assertEquals(0L, vo.getPublishedCount());
assertEquals(0L, vo.getTotalPlayCount());
assertEquals(0L, vo.getTotalLikeCount());
}
// ============================== 测试辅助 ==============================
/** 构造一行趋势聚合 Map(statDate 可为 LocalDate 或 java.sql.Date,验证类型兼容) */