s2-dashboard-api.md 1.35 KB

S2: 后端 API — /api/dashboard 聚合查询

目标

在 app.py 中新增 GET /api/dashboard,聚合所有模块指标数据,返回 JSON。

涉及文件

  • app.py (新增路由 GET /api/dashboard)
  • test_dashboard_api.py (新建,API 集成测试)

数据流

circle.db (SQLite) + WeChat DB (SQLCipher) → Python 聚合 → JSON response

步骤

  1. 写测试 (RED): test_dashboard_api.py → 验证返回结构
  2. 实现路由 (GREEN): app.py → GET /api/dashboard
  3. REFACTOR: 清理代码
  4. VERIFY: pytest + smoketest

JSON 结构

{
  "chats": {groups, messages, speakers, shares},
  "articles": {total, summarized, unsummarized, learned, unlearned},
  "learn": {pending_review, approved, applied, rejected, unclassified, by_type: {skill, memory, todo, reference}},
  "reports": {daily, summaries, learn_records, images, last_generated},
  "jobs": {total, running, completed, failed, success_rate, last_completed},
  "auto_reply": {whitelist, pending, sent},
  "circle": {conversations, messages}
}

实现要点

  • WeChat DB 查询用 try/except 包裹,失败返回 null
  • 单个 DB 连接用完即关
  • 响应时间目标
  • 错误场景返回 500 + error 信息

验证命令

curl -s http://127.0.0.1:5000/api/dashboard | python3 -m json.tool | head -30
pytest test_dashboard_api.py -v
python3 test_smoke.py