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
+9
View File
@@ -1790,6 +1790,7 @@ components:
- translations
- createdAt
- updatedAt
- statsSyncedAt
properties:
id:
type: string
@@ -1858,6 +1859,14 @@ components:
format: date-time
readOnly: true
description: 最近更新时间。
statsSyncedAt:
type: [string, 'null']
format: date-time
readOnly: true
description: |
统计副本最近同步时间(P2 A27:ov→cn 统计快照在本相册的应用时刻);
null 表示尚无回传应用(viewCountReplica 副本位仍为初始 0)。照片级
点赞副本(PhotoRecord.likeCount)的同步时间不经相册行表达。
AlbumList:
allOf:
+7
View File
@@ -908,6 +908,13 @@ export interface components {
* @description 最近更新时间。
*/
readonly updatedAt: string;
/**
* Format: date-time
* @description 统计副本最近同步时间(P2 A27:ov→cn 统计快照在本相册的应用时刻);
* null 表示尚无回传应用(viewCountReplica 副本位仍为初始 0)。照片级
* 点赞副本(PhotoRecord.likeCount)的同步时间不经相册行表达。
*/
readonly statsSyncedAt: string | null;
};
AlbumList: components["schemas"]["CursorPageMeta"] & {
items: components["schemas"]["AlbumSummary"][];
@@ -0,0 +1,12 @@
-- P2 Task 9:cn 侧统计回传副本列(ov→cn stat-snapshots 接收位)。
-- albums.viewCountReplica/statsVersion/statsSyncedAt:相册浏览数替换式副本 +
-- 回传替换版本(仅更高 statVersion 替换,绝不相加)+ 最近应用时刻(A27 展示
-- 同步时间);photos.statsVersion:likeCount 副本的替换版本比较位
-- (photos.like_count 已存在,P1 起即为副本预留位)。全列带默认值,存量行
-- (副本未同步态 = 0/NULL)与 P1 流程不受影响。无 VARCHAR 新列,不涉校对手决。
ALTER TABLE `albums` ADD COLUMN `statsSyncedAt` DATETIME(6) NULL,
ADD COLUMN `statsVersion` INTEGER NOT NULL DEFAULT 0,
ADD COLUMN `viewCountReplica` INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE `photos` ADD COLUMN `statsVersion` INTEGER NOT NULL DEFAULT 0;
+10 -1
View File
@@ -67,6 +67,12 @@ model albums {
originalDownloadEnabled Boolean @default(false)
watermarkVersion Int @default(0)
entityVersion Int @default(1) // P2 Task 3:album.updated 事件版本(同 photos 模式)
// P2 Task 9 统计回传副本位(ov→cn):viewCountReplica 为 ov album_stats
// 权威值的替换式副本;statsVersion 是回传替换版本(仅更高 statVersion 替换,
// 绝不相加);statsSyncedAt 记最近一次**应用**时刻(A27 同步时间展示)。
viewCountReplica Int @default(0)
statsVersion Int @default(0)
statsSyncedAt DateTime? @db.DateTime(6)
createdAt DateTime @default(now()) @db.DateTime(6)
updatedAt DateTime @updatedAt @db.DateTime(6)
@@ -153,7 +159,10 @@ model photos {
processingState String @default("pending_upload") // pending_upload|uploaded|processing|ready|processing_failed
errorCode String? @db.VarChar(64)
visibility String @default("unpublished") // unpublished|published|hidden|deleted
likeCount Int @default(0) // P2 起为统计副本
likeCount Int @default(0) // P2 起为统计副本(ov likeTotal 替换式回传)
// P2 Task 9:回传替换版本(ov photos_ov.countVersion 的 cn 侧镜像比较位,
// 仅更高 statVersion 替换 likeCount,重复/乱序回传幂等)。
statsVersion Int @default(0)
entityVersion Int @default(1)
@@index([albumId, visibility, publishedAt, id])
@@index([albumId, uploaderId, uploadedAt])