From 5c75d62a3e3c14a287f711a7ae652c95e5c3d0fc Mon Sep 17 00:00:00 2001 From: jidekin <294297426@qq.com> Date: Fri, 23 Oct 2026 15:34:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(logs):=20=E8=A7=86=E9=A2=91=E8=BD=AE?= =?UTF-8?q?=E8=AF=A2=E6=97=B6=E9=97=B4=E7=BA=BF=E8=90=BD=E5=BA=93=EF=BC=88?= =?UTF-8?q?=E9=87=8D=E5=90=AF=E4=B8=8D=E4=B8=A2=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 video_polls 表(video_id/ts/status/progress/ok/error/raw,索引 (video_id,ts)) - metrics.video_poll_push 同步落库(唯一写入点,同时保留内存态) - _attach_polls 改为优先读库、回落内存;/logs/recent 补上 _attach_polls 调用 - 保留策略与 api_calls 共用 90 天 - 版本 2.9.7 → 2.9.8 --- CHANGELOG.md | 16 ++++++++++++++++ app/calllog.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ app/main.py | 30 ++++++++++++++++++++---------- app/metrics.py | 6 ++++++ 4 files changed, 92 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e396eb..6cc0870 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # CHANGELOG +## 2026-10-23 · 视频轮询时间线落库(2.9.7 → 2.9.8) + +老板:「视频任务的轮询时间线也要落库」。 + +轮询快照原来只存在内存(`metrics.VIDEO_POLLS`,有界 200 任务 × 500 条),重启就没了, +日志页展开视频任务时只能看到创建那条、看不到「排队 → 生成中 → 完成」的过程。 + +- **新表 `video_polls`**(同一 SQLite 库):`video_id / ts / status / progress / ok / error / raw`, + 索引 `(video_id, ts)`。保留策略与 `api_calls` 共用(90 天一起清)。 +- **写入**:`metrics.video_poll_push()` 是唯一写入点 —— 它同时写内存(保持原有行为)与库 + (轮询频率低,单条 insert 足够,不排队)。 +- **读取**:`_attach_polls()` 改为**优先读库**、库里没有再回落内存;`/logs/recent`(实时页签) + 之前漏了调用 `_attach_polls`,一并补上。 +- **实测**:模拟 3 次轮询(created/processing/completed)→ 库内 3 条 → **重启网关** → + `/logs/recent` 仍返回 3 条轮询(`✅ 持久化成功`)。测试数据已清理。 + ## 2026-10-23 · 用户管理里的「调用日志」页签移除(2.9.6 → 2.9.7) 老板:「admin 用户管理里面的调用日志可以去掉了」。 diff --git a/app/calllog.py b/app/calllog.py index bd477b3..86902ea 100644 --- a/app/calllog.py +++ b/app/calllog.py @@ -81,6 +81,18 @@ CREATE INDEX IF NOT EXISTS idx_calls_model ON api_calls(model, ts); CREATE INDEX IF NOT EXISTS idx_calls_ok ON api_calls(ok, ts); CREATE INDEX IF NOT EXISTS idx_calls_status ON api_calls(status, ts); CREATE INDEX IF NOT EXISTS idx_calls_rid ON api_calls(rid); + +CREATE TABLE IF NOT EXISTS video_polls ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + video_id TEXT NOT NULL, + ts REAL NOT NULL, + status TEXT, + progress REAL, + ok INTEGER, + error TEXT, + raw TEXT +); +CREATE INDEX IF NOT EXISTS idx_vp_vid ON video_polls(video_id, ts); """ _COLUMNS = ("ts", "rid", "account", "key_id", "kind", "method", "path", "model", @@ -287,6 +299,7 @@ class CallLogStore: try: with self._lock: cur = self._db.execute("DELETE FROM api_calls WHERE ts < ?", (cutoff,)) + self._db.execute("DELETE FROM video_polls WHERE ts < ?", (cutoff,)) self._db.commit() n = cur.rowcount or 0 if n: @@ -439,6 +452,43 @@ class CallLogStore: out["queued"] = len(self._q) return out + # ── 视频轮询快照(2026-10-23 落库:重启后仍能看到轮询时间线)──────────── + def add_poll(self, video_id: str, snap: dict[str, Any]) -> None: + """记一次视频轮询。直接落库(轮询频率低,单条 insert 足够)。""" + if not self.enabled or self._db is None or not video_id: + return + try: + with self._lock: + self._db.execute( + "INSERT INTO video_polls (video_id, ts, status, progress, ok, error, raw) " + "VALUES (?,?,?,?,?,?,?)", + (video_id, float(snap.get("ts") or time.time()), + snap.get("status"), snap.get("progress"), + 1 if snap.get("ok") else 0, + _clip(snap.get("error"), 2000), _clip(snap.get("raw"), 4000))) + self._db.commit() + except Exception as e: # noqa: BLE001 + self.last_error = f"{type(e).__name__}: {e}" + + def polls_for(self, video_ids: list[str], max_per: int = 200) -> dict[str, list[dict[str, Any]]]: + """按 video_id 批量取轮询时间线(升序),供日志页合成展示。""" + if not self.enabled or self._db is None or not video_ids: + return {} + out: dict[str, list[dict[str, Any]]] = {} + try: + with self._lock: + for vid in {v for v in video_ids if v}: + rows = self._db.execute( + "SELECT ts, status, progress, ok, error, raw FROM video_polls " + "WHERE video_id = ? ORDER BY ts ASC LIMIT ?", (vid, max_per)).fetchall() + if rows: + out[vid] = [{"ts": r[0], "status": r[1], "progress": r[2], + "ok": bool(r[3]), "error": r[4], "raw": r[5]} + for r in rows] + except Exception as e: # noqa: BLE001 + log.warning("读取视频轮询失败: %s", e) + return out + # ── 聚合(供 /metrics、/health、月度用量导出使用)──────────────────── def usage_snapshot(self, *, account: str | None = None, days: int = 0) -> dict[str, Any]: """按模型/供应商/账号聚合调用数、耗时与 token(形状对齐内存态 `Metrics.snapshot()`)。 diff --git a/app/main.py b/app/main.py index 6c310a5..3043a46 100644 --- a/app/main.py +++ b/app/main.py @@ -355,7 +355,7 @@ _docs_on = config.server.docs_enabled # 安全默认关(report: /docs、/ope # 2026-09-30: 2.6.1 → 2.7.0(按老板要求整个删除「火山方舟 Coding Plan」供应商:配置/模型/代码/文档/前端下拉/环境变量全清)。 # 2026-09-30: 2.7.0 → 2.7.1(界面提示白话化:熔断器→供应商自动保护、引擎→运行环境、slug→标识;删 120 个未引用死键)。 # ★ 版本号纪律:每次改动都要往上加,小改动加小版本号、大改动加大版本号(README 有专章)。 -VERSION = "2.9.7" +VERSION = "2.9.8" app = FastAPI( title="API Gateway", @@ -3162,15 +3162,25 @@ async def metrics_prometheus(request: Request, _auth=Depends(require_auth)): # GET /logs(P1-4) # ───────────────────────────────────── def _attach_polls(entries: list[dict]) -> list[dict]: - """给带 video_id 的创建日志按需附加内存里累积的轮询快照(第十一轮:轮询合成一条)。""" + """给带 video_id 的创建日志附加轮询快照(第十一轮:轮询合成一条)。 + + 2026-10-23:轮询已落库 → **优先从库里取**(重启后仍可看);库里没有才回落到内存态。 + """ + vids = [e.get("video_id") or e.get("served") for e in entries if e.get("kind") == "video"] + db = {} + try: + db = calllog.store.polls_for([v for v in vids if v]) + except Exception: # noqa: BLE001 + db = {} for e in entries: - vid = e.get("video_id") - if vid: - e["video_id"] = vid - p = video_polls(vid) - if p: - e["polls"] = p - e.setdefault("poll_last", p[-1].get("status")) + vid = e.get("video_id") or (e.get("served") if e.get("kind") == "video" else None) + if not vid: + continue + e["video_id"] = vid + p = db.get(vid) or video_polls(vid) + if p: + e["polls"] = p + e.setdefault("poll_last", p[-1].get("status")) return entries @@ -3222,7 +3232,7 @@ async def logs_recent(request: Request, n: int = 200, _auth=Depends(require_auth "reasoning_tokens": it.get("tok_reasoning") or 0}, "id": it.get("id"), }) - return {"entries": items, "source": "db"} + return {"entries": _attach_polls(items), "source": "db"} @app.get("/logs") diff --git a/app/metrics.py b/app/metrics.py index d424ca8..87a8ac2 100644 --- a/app/metrics.py +++ b/app/metrics.py @@ -49,6 +49,12 @@ def video_poll_push(video_id: str, snap: dict) -> None: dq = deque(maxlen=_VP_MAX_POLLS) VIDEO_POLLS[video_id] = dq dq.append(snap) + # 2026-10-23:同时落库 —— 内存态重启即丢,库里才持久(日志页优先读库) + try: + from app import calllog as _calllog + _calllog.store.add_poll(video_id, snap) + except Exception: # noqa: BLE001 + pass def video_polls(video_id: str) -> list[dict]: