feat(logs): 视频轮询时间线落库(重启不丢)

- 新增 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
This commit is contained in:
jidekin committed 2026-10-23 15:34:28 +08:00
1 parent f68aad743a
commit 2b15db905b
4 files changed
+88 -6

No files matched your search

+16
View File
@@ -1,5 +1,21 @@
# CHANGELOG # 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) ## 2026-10-23 · 用户管理里的「调用日志」页签移除(2.9.6 → 2.9.7)
老板:「admin 用户管理里面的调用日志可以去掉了」。 老板:「admin 用户管理里面的调用日志可以去掉了」。
+50
View File
@@ -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_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_status ON api_calls(status, ts);
CREATE INDEX IF NOT EXISTS idx_calls_rid ON api_calls(rid); 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", _COLUMNS = ("ts", "rid", "account", "key_id", "kind", "method", "path", "model",
@@ -287,6 +299,7 @@ class CallLogStore:
try: try:
with self._lock: with self._lock:
cur = self._db.execute("DELETE FROM api_calls WHERE ts < ?", (cutoff,)) cur = self._db.execute("DELETE FROM api_calls WHERE ts < ?", (cutoff,))
self._db.execute("DELETE FROM video_polls WHERE ts < ?", (cutoff,))
self._db.commit() self._db.commit()
n = cur.rowcount or 0 n = cur.rowcount or 0
if n: if n:
@@ -439,6 +452,43 @@ class CallLogStore:
out["queued"] = len(self._q) out["queued"] = len(self._q)
return out 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、月度用量导出使用)──────────────────── # ── 聚合(供 /metrics、/health、月度用量导出使用)────────────────────
def usage_snapshot(self, *, account: str | None = None, days: int = 0) -> dict[str, Any]: def usage_snapshot(self, *, account: str | None = None, days: int = 0) -> dict[str, Any]:
"""按模型/供应商/账号聚合调用数、耗时与 token(形状对齐内存态 `Metrics.snapshot()`)。 """按模型/供应商/账号聚合调用数、耗时与 token(形状对齐内存态 `Metrics.snapshot()`)。
+16 -6
View File
@@ -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.6.1 → 2.7.0(按老板要求整个删除「火山方舟 Coding Plan」供应商:配置/模型/代码/文档/前端下拉/环境变量全清)。
# 2026-09-30: 2.7.0 → 2.7.1(界面提示白话化:熔断器→供应商自动保护、引擎→运行环境、slug→标识;删 120 个未引用死键)。 # 2026-09-30: 2.7.0 → 2.7.1(界面提示白话化:熔断器→供应商自动保护、引擎→运行环境、slug→标识;删 120 个未引用死键)。
# ★ 版本号纪律:每次改动都要往上加,小改动加小版本号、大改动加大版本号(README 有专章)。 # ★ 版本号纪律:每次改动都要往上加,小改动加小版本号、大改动加大版本号(README 有专章)。
VERSION = "2.9.7" VERSION = "2.9.8"
app = FastAPI( app = FastAPI(
title="API Gateway", title="API Gateway",
@@ -3162,12 +3162,22 @@ async def metrics_prometheus(request: Request, _auth=Depends(require_auth)):
# GET /logs(P1-4) # GET /logs(P1-4)
# ───────────────────────────────────── # ─────────────────────────────────────
def _attach_polls(entries: list[dict]) -> list[dict]: 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: for e in entries:
vid = e.get("video_id") vid = e.get("video_id") or (e.get("served") if e.get("kind") == "video" else None)
if vid: if not vid:
continue
e["video_id"] = vid e["video_id"] = vid
p = video_polls(vid) p = db.get(vid) or video_polls(vid)
if p: if p:
e["polls"] = p e["polls"] = p
e.setdefault("poll_last", p[-1].get("status")) e.setdefault("poll_last", p[-1].get("status"))
@@ -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}, "reasoning_tokens": it.get("tok_reasoning") or 0},
"id": it.get("id"), "id": it.get("id"),
}) })
return {"entries": items, "source": "db"} return {"entries": _attach_polls(items), "source": "db"}
@app.get("/logs") @app.get("/logs")
+6
View File
@@ -49,6 +49,12 @@ def video_poll_push(video_id: str, snap: dict) -> None:
dq = deque(maxlen=_VP_MAX_POLLS) dq = deque(maxlen=_VP_MAX_POLLS)
VIDEO_POLLS[video_id] = dq VIDEO_POLLS[video_id] = dq
dq.append(snap) 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]: def video_polls(video_id: str) -> list[dict]: