feat(logs): 实时日志改为读库,重启不丢
- 新增 GET /logs/recent?n=200(从 api_calls 取最近 N 条,形状与 /logs 条目一致) - 权限口径不变:只看自己的(admin 也只看自己);看全站走 /logs/calls - 前端实时页改调 /logs/recent,保留 5 秒自动刷新 - 修 query() 漏选 tok_* 四列导致实时页 token 显示为 0 - 版本 2.9.5 → 2.9.6
This commit is contained in:
1 parent
36db428b90
commit
4799c6d3f5
4 files changed
+68
-3
No files matched your search
@@ -1,5 +1,25 @@
|
|||||||
# CHANGELOG
|
# CHANGELOG
|
||||||
|
|
||||||
|
## 2026-10-23 · 实时日志改为读库:重启不再丢(2.9.5 → 2.9.6)
|
||||||
|
|
||||||
|
老板:「实时的日志为什么一重启就没了,也要落地可以查看」。
|
||||||
|
|
||||||
|
**原因**:「实时」页签读的是 `AccessLog` 内存环形缓冲(默认 500 条),进程一重启就清空。
|
||||||
|
但其实**每条访问记录早就在落库了**(`api_calls`),只是这个页面没去读它。
|
||||||
|
|
||||||
|
**改动**:
|
||||||
|
- 新增 `GET /logs/recent?n=200`:从 `api_calls` 取最近 N 条,字段形状与原来 `/logs` 的条目
|
||||||
|
完全一致(body/response/tokens/ok/stream/model/status/latency…),前端**零渲染改动**。
|
||||||
|
- `/logs/recent` 权限口径与 `/logs` 一致:**只看自己的**(admin 也只看自己的;看全站走 `/logs/calls`)。
|
||||||
|
- 前端「实时」页签改调 `/logs/recent?n=200`,保留 5 秒自动刷新。
|
||||||
|
- **顺带修**:`query()` 的 SELECT 列表漏了 `tok_prompt/tok_completion/tok_cached/tok_reasoning`
|
||||||
|
四列 → 实时页的 token 显示为 0(库里其实有值)。补上后正常。
|
||||||
|
|
||||||
|
**实测**:发 2 条调用 → **重启网关** → 实时页仍是 9 条,含那 2 条(「重启持久化验证 第1/2条」);
|
||||||
|
token 正常(输入 41 / 输出 12 / 推理 12);成员视角只看到自己的 89 条(全为 chenghongchang)。
|
||||||
|
|
||||||
|
**仍有的小差异**:视频轮询时间线(`polls`)只在内存里合成,重启后不显示(视频任务本身的记录仍在)。
|
||||||
|
|
||||||
## 2026-10-23 · 修复:流式请求的「请求内容」为空(2.9.4 → 2.9.5)
|
## 2026-10-23 · 修复:流式请求的「请求内容」为空(2.9.4 → 2.9.5)
|
||||||
|
|
||||||
老板:「为什么请求内容看不到,是空的」。
|
老板:「为什么请求内容看不到,是空的」。
|
||||||
|
|||||||
+2
-1
@@ -345,7 +345,8 @@ class CallLogStore:
|
|||||||
# 列表页也要「内容」列(2026-10-23 老板要求):始终取 prompt 生成单行预览,
|
# 列表页也要「内容」列(2026-10-23 老板要求):始终取 prompt 生成单行预览,
|
||||||
# 但不把动辄几千字的原文整包吐给列表(with_content=1 才给完整 prompt/completion)。
|
# 但不把动辄几千字的原文整包吐给列表(with_content=1 才给完整 prompt/completion)。
|
||||||
cols = ("id, ts, rid, account, key_id, kind, method, path, model, served, provider, "
|
cols = ("id, ts, rid, account, key_id, kind, method, path, model, served, provider, "
|
||||||
"caller, stream, ok, status, latency_ms, ip, ua, ver, prompt")
|
"caller, stream, ok, status, latency_ms, ip, ua, ver, prompt, "
|
||||||
|
"tok_prompt, tok_completion, tok_cached, tok_reasoning")
|
||||||
if with_content:
|
if with_content:
|
||||||
cols += ", completion"
|
cols += ", completion"
|
||||||
limit = max(1, min(int(limit), 500))
|
limit = max(1, min(int(limit), 500))
|
||||||
|
|||||||
+44
-1
@@ -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.5"
|
VERSION = "2.9.6"
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="API Gateway",
|
title="API Gateway",
|
||||||
@@ -3182,6 +3182,49 @@ def _logs_for_account(account: str, n: int = 200) -> list[dict]:
|
|||||||
return _attach_polls(rows)
|
return _attach_polls(rows)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/logs/recent")
|
||||||
|
async def logs_recent(request: Request, n: int = 200, _auth=Depends(require_auth)):
|
||||||
|
"""「实时」页签的数据源(2026-10-23 起改为读库)。
|
||||||
|
|
||||||
|
原来 /logs 读的是内存环形缓冲(500 条,**重启即清**)。但每条调用其实都已经
|
||||||
|
落在 `api_calls` 里了,所以这里直接从库里取最近 N 条 —— **重启不再丢**,
|
||||||
|
且字段形状与 /logs 的条目保持一致(body/response/tokens 等),前端零改动即可渲染。
|
||||||
|
权限口径同 /logs:**只看自己的**(admin 也只看自己的;要看全站走 /logs/calls)。
|
||||||
|
"""
|
||||||
|
me = request.state.account
|
||||||
|
n = max(1, min(int(n), 500))
|
||||||
|
rows = calllog.store.query(limit=n, offset=0, account=None, with_content=True)
|
||||||
|
items = []
|
||||||
|
for it in rows.get("items", []):
|
||||||
|
acct = it.get("account") or it.get("caller") or ""
|
||||||
|
if acct != me and (it.get("caller") or "") != me:
|
||||||
|
continue
|
||||||
|
items.append({
|
||||||
|
"ts": it.get("ts"),
|
||||||
|
"rid": it.get("rid"),
|
||||||
|
"account": acct,
|
||||||
|
"kind": it.get("kind"),
|
||||||
|
"method": it.get("method"),
|
||||||
|
"path": it.get("path"),
|
||||||
|
"model": it.get("model"),
|
||||||
|
"served": it.get("served"),
|
||||||
|
"provider": it.get("provider"),
|
||||||
|
"caller": it.get("caller"),
|
||||||
|
"ok": bool(it.get("ok")),
|
||||||
|
"stream": bool(it.get("stream")),
|
||||||
|
"status": it.get("status"),
|
||||||
|
"ms": it.get("latency_ms"),
|
||||||
|
"body": (it.get("prompt") or "")[:8000],
|
||||||
|
"response": (it.get("completion") or "")[:8000],
|
||||||
|
"tokens": {"prompt_tokens": it.get("tok_prompt") or 0,
|
||||||
|
"completion_tokens": it.get("tok_completion") or 0,
|
||||||
|
"cached_tokens": it.get("tok_cached") or 0,
|
||||||
|
"reasoning_tokens": it.get("tok_reasoning") or 0},
|
||||||
|
"id": it.get("id"),
|
||||||
|
})
|
||||||
|
return {"entries": items, "source": "db"}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/logs")
|
@app.get("/logs")
|
||||||
async def logs(request: Request, n: int = 100, _auth=Depends(require_auth)):
|
async def logs(request: Request, n: int = 100, _auth=Depends(require_auth)):
|
||||||
# 第十一轮 #2:日志页对**所有人(含 admin)都只显示自己的**。
|
# 第十一轮 #2:日志页对**所有人(含 admin)都只显示自己的**。
|
||||||
|
|||||||
@@ -201,7 +201,8 @@ async function load() {
|
|||||||
await api(`/v1/videos/${encodeURIComponent(e.video_id)}?model=${encodeURIComponent(e.served || e.model || '')}`)
|
await api(`/v1/videos/${encodeURIComponent(e.video_id)}?model=${encodeURIComponent(e.served || e.model || '')}`)
|
||||||
} catch { /* 轮询失败不打断刷新;后端会记为该次的失败快照 */ }
|
} catch { /* 轮询失败不打断刷新;后端会记为该次的失败快照 */ }
|
||||||
}
|
}
|
||||||
const j = await api('/logs?n=200')
|
// 2026-10-23:改读库(/logs/recent)—— 内存环缓冲重启即丢,库里才是持久的
|
||||||
|
const j = await api('/logs/recent?n=200')
|
||||||
entries.value = j.entries || []
|
entries.value = j.entries || []
|
||||||
// 只保留仍存在的已展开行:行被顶出窗口/被删则对应 key 一并清掉
|
// 只保留仍存在的已展开行:行被顶出窗口/被删则对应 key 一并清掉
|
||||||
const alive = new Set(entries.value.map(rowKey))
|
const alive = new Set(entries.value.map(rowKey))
|
||||||
|
|||||||
Reference in new issue
Block a user