feat(sync): stat snapshot return path with version replacement

P2 Task 9 统计回传(ov→cn):
- cn 迁移:albums 增 viewCountReplica/statsVersion/statsSyncedAt、
  photos 增 statsVersion(like_count 已有副本位;无 VARCHAR 新列不涉校对手决)
- cn 接收端 POST /v1/internal/sync/stat-snapshots(InternalSyncGuard):
  整批单事务,逐快照 updateMany WHERE statsVersion < statVersion —— 仅更高
  statVersion 以权威 total 整体替换副本(绝不相加),重复/乱序幂等忽略;
  accepted 只数实际应用条数;views 应用同时推进 statsSyncedAt(A27 展示)
- ov 回传发送器 services/sync/stat-return.ts:聚合 outbox_stat_events 未发送
  行按(kind, photoId|albumId)取 MAX(statVersion) 折叠突发 → HMAC 推送 →
  200 后按计数器标记截至该版本全部源行 sentAt;失败留待下轮重发;main.ts
  第三循环 STAT_RETURN_INTERVAL_MS(默认 60s,env 可调)
- 契约 AlbumSummary + statsSyncedAt(string|null readOnly,+gen);listAlbums/
  getAlbum 经 toSummaryResponse 暴露(管理 UI 列留 P4a)
- e2e(services/sync stat-return.e2e-spec,7 例):逐次点赞回传替换式收敛
  (2→5→10 非累加)、浏览窗口去重回传 + statsSyncedAt、重复回传 accepted=0
  值不动(A27 不重复累计)、乱序高版本替换(total 可下调)+ 低版本忽略、
  发送端折叠只发最高版本且被折叠行随批确认、管理列表 likeCount/statsSyncedAt
  展示、新端点 401/400 守卫

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-09-18 18:45:43 +08:00
parent 1f2a0c658f
commit 955bea49b5
13 changed files with 933 additions and 17 deletions
+54
View File
@@ -30,6 +30,7 @@ import { Injectable } from '@nestjs/common';
import type { components } from '@photo/contracts';
import type { Prisma } from '@photo/db-cn';
import { PrismaService } from '../prisma/prisma.service';
import type { StatSnapshotDto } from './dto';
import {
type AlbumCategoryInput,
buildAlbumUpdatedPayload,
@@ -41,6 +42,7 @@ import {
export type BootstrapResponse = components['schemas']['BootstrapResponse'];
export type ReconcileResponse = components['schemas']['ReconcileResponse'];
export type DigestResponse = components['schemas']['DigestResponse'];
export type StatSnapshotResponse = components['schemas']['StatSnapshotResponse'];
export type SyncEventWire = components['schemas']['SyncEvent'];
/** 线格式 schema 版本(契约 SyncEvent.schemaVersion 当前为 1)。 */
@@ -502,6 +504,58 @@ export class SyncService {
};
}
/**
* 统计快照回传接收(P2 Task 9,ov→cn):整批单事务,逐快照做**版本替换**——
* 仅当 cn 侧 statsVersion < snapshot.statVersion 时以权威 total 整体替换副本
* (绝不相加),重复回传(同版本)与乱序(更低版本)均幂等忽略。这是 A27
* 「不重复累计」的接收端锚点:total 恒为 ov 权威快照值(非增量),即使未来
* 出现下调修正(total 变小),更高版本也照常替换——版本是唯一权威。
*
* 副本位:likes → photos.likeCount(比较位 photos.statsVersion);
* views → albums.viewCountReplica(比较位 albums.statsVersion,
* 同时推进 statsSyncedAt=本批应用时刻,A27 展示同步时间;likes 快照的
* photoId 在 cn 必然存在(照片源于 cn),不存在/更旧版本 → 该条不计入
* accepted,不报错——与「旧版本忽略不计」同一幂等口径)。
* accepted 只数**实际应用**(新版本生效)的条数(契约:旧版本被忽略的不计)。
*/
async receiveStatSnapshots(
snapshots: StatSnapshotDto[],
): Promise<StatSnapshotResponse> {
const syncedAt = new Date();
return this.prisma.$transaction(async (tx) => {
let accepted = 0;
for (const snapshot of snapshots) {
if (snapshot.kind === 'likes' && snapshot.photoId !== undefined) {
const applied = await tx.photos.updateMany({
where: {
id: snapshot.photoId,
statsVersion: { lt: snapshot.statVersion },
},
data: {
likeCount: snapshot.total,
statsVersion: snapshot.statVersion,
},
});
accepted += applied.count;
} else if (snapshot.kind === 'views') {
const applied = await tx.albums.updateMany({
where: {
id: snapshot.albumId,
statsVersion: { lt: snapshot.statVersion },
},
data: {
viewCountReplica: snapshot.total,
statsVersion: snapshot.statVersion,
statsSyncedAt: syncedAt,
},
});
accepted += applied.count;
}
}
return { accepted };
});
}
/** 相册 id 集合 → 分类快照输入(分类行 + 翻译行配对;升序确定性由构建器排)。 */
private async categoryInputsByAlbumIds(
albumIds: string[],