Files
photo-live/packages/db-cn/prisma/migrations/20260920104345_usage_thresholds_alerts/migration.sql
T
浅念 4937eeecff feat(api): usage summary with threshold alerts
P4a Task 5(A23 后台侧):用量统计与阈值告警后端全链。

- cn 迁移(校对手决 utf8mb4_0900_bin):usage_thresholds 单行配置表
  (迁移内种子行,storageBytes/perAlbumBytes BigInt?,null=维度停用)+
  usage_alerts 告警账本(kind storage|perAlbum、albumId?、resolvedAt?
  索引)。「同 kind+albumId 活跃告警唯一」为应用级约束(MySQL 无部分
  唯一索引):评估事务以单行配置 FOR UPDATE 作串行化锚点闭合竞态。
- 共享核心 packages/db-cn/src/usage.ts:computeUsageAggregates(非
  deleted 口径实时 SUM:photos groupBy + 变体 JOIN raw)+ evaluateUsage
  (storage/perAlbum 两维超阈 ensure、回落/停用 resolve、去重)+
  evaluateUsageSafe(自开事务、失败仅记日志不拖垮业务主流程)。api 与
  worker 共用(放置原因见文件头)。
- api:GET /admin/usage/summary(相册聚合 + totals + 阈值 + 告警:活跃
  在前、近期解除补位,合计 100)与 PUT /admin/usage/thresholds(缺省
  保持/显式 null 清除 + 契约「即刻重新评估」);仅 admin。BigInt 运输
  决策:库内 BIGINT、JSON 出入口 Number() 收敛(2^53≈9PB 上限,DTO 以
  MAX_SAFE_INTEGER 拒绝超界输入杜绝静默失真)。
- 检查点(提交后评估):worker 发布事务后(仅真实 published)、worker
  水印任务完成后、api photos 可见性迁移(PATCH/软删除)与批量隐藏后、
  阈值 PUT 后。
- tests:usage e2e +6(汇总数学/降序/标题、403/401、400 校验矩阵、
  perAlbum 告警产生-去重-上调解除、storage 告警-null 清除解除、hidden
  计入+deleted 回落且穿过 api 检查点);两侧 reset 增补 usage 表截断 +
  单行重种。全量回归:api e2e 214/214、api 单测 51/51、worker 单测
  14/14 + e2e 13/13。

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-20 19:01:41 +08:00

35 lines
1.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- P4a Task 5:用量阈值(单行配置表,迁移内种子行)+ 阈值告警账本(A23 后台侧)。
-- 校对手决:Prisma 生成 DDL 固定 utf8mb4_unicode_ci,逐表改写为库级约定的
-- utf8mb4_0900_bin(id/kind/albumId 等字符串列的大小写敏感语义依赖它)。
-- 「同 kind+albumId 活跃告警唯一」为应用级约束(MySQL 无部分唯一索引):
-- evaluateUsage 以单行配置 FOR UPDATE 作串行化锚点(packages/db-cn/src/usage.ts)。
-- 阈值两维缺省 NULL=停用;种子行固定 id 见 USAGE_THRESHOLDS_SINGLETON_ID。
-- CreateTable
CREATE TABLE `usage_thresholds` (
`id` VARCHAR(191) NOT NULL,
`storageBytes` BIGINT NULL,
`perAlbumBytes` BIGINT NULL,
`updatedAt` DATETIME(6) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin;
-- 单行种子(PUT /admin/usage/thresholds 恒 upsert 本行;缺失行由 evaluate
-- 兜底视作双维停用,但迁移种子保证常规路径下不变量成立)。
INSERT INTO `usage_thresholds` (`id`, `storageBytes`, `perAlbumBytes`, `updatedAt`)
VALUES ('usage-thresholds-single-row', NULL, NULL, CURRENT_TIMESTAMP(6));
-- CreateTable
CREATE TABLE `usage_alerts` (
`id` VARCHAR(191) NOT NULL,
`kind` VARCHAR(16) NOT NULL,
`albumId` VARCHAR(64) NULL,
`message` VARCHAR(1024) NOT NULL,
`createdAt` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
`resolvedAt` DATETIME(6) NULL,
INDEX `usage_alerts_resolvedAt_idx`(`resolvedAt`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin;