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
+92 -10

No files matched your search

+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_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()`)。
+20 -10
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.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")
+6
View File
@@ -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]: