zcbot/web/routers
caoqianming 7354578aaa refactor(storage): 计费读侧收口 core/storage/usage_report.py + 首批 DB 级测试
风险点(架构审查 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>
2026-07-23 10:33:13 +08:00
..
__init__.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00
asr.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00
authroutes.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00
files.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00
kb.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00
messages.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00
misc.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00
models.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00
schedules.py refactor(storage): 计费读侧收口 core/storage/usage_report.py + 首批 DB 级测试 2026-07-23 10:33:13 +08:00
skills_memory.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00
tasks.py refactor(storage): 计费读侧收口 core/storage/usage_report.py + 首批 DB 级测试 2026-07-23 10:33:13 +08:00
wechat.py refactor(web): app.py 拆分——73 路由迁 11 个 router 模块,3972 行减至 732 2026-07-23 10:18:57 +08:00