风险点(架构审查 P0#2):UsageEvent.units JSONB 的 key 由 usage.py 写入, 读侧 cast(units[...].astext) 却散在 web/admin.py 与 web 各处硬编码——写读 跨文件隐式耦合且零测试,改 key 会静默算错计费统计。 - 新增 core/storage/usage_report.py:units 读侧唯一出口(列表达式单一事实 源),含 task_usage_aggregates(逐 task 批量)/ usage_overview(全局+7d 趋势)/ models_usage(按模型)/ user_usage_page(按用户分页) - web/admin.py 三个内联聚合函数删除,改调 usage_report;web 层不再出现 任何 JSONB cast(grep 已核) - web/common.usage_aggregates 移除,tasks/schedules 路由改引 core 版 - tests/test_usage_report.py:6 个 DB 级测试锁口径(cost 全 kind 合计、 token/cache_hit 仅 chat、task_id 可空的 kb_ingest 不进 task 聚合、cutoff 过滤、用户分页含档案字段)。无 PG 自动 skip;只插/删测试专属 user 的行。 发现并记录:ZCBOT_DB_URL 平时靠 import litellm 的隐式 dotenv 进 env, 测试显式从 .env 抠 key 不背 8s 重依赖。 292 测试全过,测试数据零残留。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> |
||
|---|---|---|
| .. | ||
| __init__.py | ||
| disk_quota.py | ||
| engine.py | ||
| models.py | ||
| usage.py | ||
| usage_report.py | ||
| utils.py | ||