diff --git a/CHANGELOG.md b/CHANGELOG.md index 71fb5b2..6afc072 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,35 @@ # CHANGELOG +## 2026-10-23 · 指标改由数据库提供(重启不再清零)+ 月度用量导出(2.8.0 → 2.9.0) + +老板:「全做」—— 指上一条列出的两项:① Dashboard 统计改从数据库读;② 按账号月度用量导出。 + +### ① 指标数据源:内存 → SQLite +- 新增 `_usage_snapshot()`:**调用数 / 供应商分布 / token / 耗时(含 p50/p95)** 全部改为从 + `api_calls` 聚合(保留期内 90 天),**进程重启不再清零**;**运行时长 / 在途请求 / 熔断状态** + 仍是进程内实时值。库不可用时自动回落到内存态(行为与之前一致)。 +- 影响端点:`GET /metrics`、`GET /metrics/prometheus`、`GET /health` 的 `models` 字段。 +- **口径变化(重要)**:Dashboard / 指标页看到的数字从「本次进程启动以来」变成「最近 90 天累计」, + 所以数字会一次变大。要"本次启动以来"的口径需要另加参数,目前没有。 +- 成员视角不变:仍只聚合自己的账号(`account` 过滤)。 + +### ② token 也落库了 +此前 `api_calls` 没存 token(方案里列了但首版没实现),导致指标切库后 token 只能是 0。 +本轮补齐: +- 表加 4 列 `tok_prompt / tok_completion / tok_cached / tok_reasoning`,**老库自动 ALTER 迁移**。 +- 用 **rid contextvar + token 侧信道** 把两处接起来:`_record_usage_metrics()` 只知道 token、 + 不知道 rid;中间件只知道 rid、不知道 token。现在中间件 `RID.set(rid)`, + 记账时 `calllog.note_tokens(RID.get(), …)`,落库时按 rid 取走并清空。 +- 直传流式(`_UsageScanner`)那条路径也补了。 + +### ③ 月度用量导出 +- `GET /logs/usage/monthly?month=YYYY-MM`:按账号聚合调用数 / 成功 / 失败 / 平均耗时 / 涉及模型数(仅管理员)。 +- `GET /logs/usage/monthly/export?month=YYYY-MM`:CSV(UTF-8 BOM,Excel 直接打开,含失败率)。 +- 前端:调用日志页加「月份选择 + 月度用量」按钮 → 弹窗显示按账号的表格 + 导出 CSV。 + +### ④ 顺手修 +- 模型统计排除「无模型行」(匿名 401 的早失败补记)—— 否则 Dashboard 上会出现一个 `?` 模型。 + ## 2026-10-23 · 调用日志落地(SQLite / 90 天 / 仅管理员可查全部)(2.7.3 → 2.8.0) 老板:「ai网关项目,现在我想把调用日志落地,有什么方案」→ 方案确认后:「1,要记 / 2,90天 / diff --git a/app/calllog.py b/app/calllog.py index 6d60639..6f02f64 100644 --- a/app/calllog.py +++ b/app/calllog.py @@ -69,6 +69,10 @@ CREATE TABLE IF NOT EXISTS api_calls ( ua TEXT, prompt TEXT, completion TEXT, + tok_prompt INTEGER DEFAULT 0, + tok_completion INTEGER DEFAULT 0, + tok_cached INTEGER DEFAULT 0, + tok_reasoning INTEGER DEFAULT 0, ver TEXT ); CREATE INDEX IF NOT EXISTS idx_calls_ts ON api_calls(ts); @@ -81,7 +85,8 @@ CREATE INDEX IF NOT EXISTS idx_calls_rid ON api_calls(rid); _COLUMNS = ("ts", "rid", "account", "key_id", "kind", "method", "path", "model", "served", "provider", "caller", "stream", "ok", "status", "latency_ms", - "ip", "ua", "prompt", "completion", "ver") + "ip", "ua", "prompt", "completion", + "tok_prompt", "tok_completion", "tok_cached", "tok_reasoning", "ver") _INSERT = f"INSERT INTO api_calls ({', '.join(_COLUMNS)}) VALUES ({', '.join('?' * len(_COLUMNS))})" @@ -124,6 +129,7 @@ class CallLogStore: db.execute("PRAGMA journal_mode=WAL") db.execute("PRAGMA synchronous=NORMAL") db.executescript(SCHEMA) + self._migrate(db) db.commit() try: os.chmod(self.path, 0o600) @@ -133,6 +139,15 @@ class CallLogStore: log.info("调用日志已就绪: %s(保留 %d 天)", self.path, self.retention_days) self.cleanup() + @staticmethod + def _migrate(db: sqlite3.Connection) -> None: + """轻量迁移:老库缺 token 列时补上(2026-10-23 加)。""" + have = {r[1] for r in db.execute("PRAGMA table_info(api_calls)")} + for col in ("tok_prompt", "tok_completion", "tok_cached", "tok_reasoning"): + if col not in have: + db.execute(f"ALTER TABLE api_calls ADD COLUMN {col} INTEGER DEFAULT 0") + log.info("调用日志表已补列: %s", col) + async def start(self) -> None: self.open() if not self.enabled or self._task is not None: @@ -353,6 +368,94 @@ class CallLogStore: out["queued"] = len(self._q) return out + # ── 聚合(供 /metrics、/health、月度用量导出使用)──────────────────── + def usage_snapshot(self, *, account: str | None = None, days: int = 0) -> dict[str, Any]: + """按模型/供应商/账号聚合调用数、耗时与 token(形状对齐内存态 `Metrics.snapshot()`)。 + + account 非空 → 只算该账号(成员视角);days=0 → 不限时间(用库内全部,即保留期内)。 + """ + if not self.enabled or self._db is None: + return {"enabled": False} + since = time.time() - days * 86400 if days and days > 0 else 0 + conds: list[str] = [] + params: list[Any] = [] + if since: + conds.append("ts >= ?") + params.append(since) + if account: + conds.append("account = ?") + params.append(account) + where = (" WHERE " + " AND ".join(conds)) if conds else "" + try: + with self._lock: + mcond = f"{where} AND model IS NOT NULL" if where else " WHERE model IS NOT NULL" + calls = {r[0]: {"ok": r[1] or 0, "fail": r[2] or 0} + for r in self._db.execute( + f"SELECT model, SUM(CASE WHEN ok=1 THEN 1 ELSE 0 END), " + f"SUM(CASE WHEN ok=0 THEN 1 ELSE 0 END) " + f"FROM api_calls{mcond} GROUP BY model", params)} + pcond = f"{where} AND provider IS NOT NULL" if where else " WHERE provider IS NOT NULL" + providers = {r[0] or "?": {"ok": r[1] or 0, "fail": r[2] or 0} + for r in self._db.execute( + f"SELECT provider, SUM(CASE WHEN ok=1 THEN 1 ELSE 0 END), " + f"SUM(CASE WHEN ok=0 THEN 1 ELSE 0 END) " + f"FROM api_calls{pcond} GROUP BY provider", params)} + latency: dict[str, Any] = {} + lcond = (f"{where} AND " if where else " WHERE ") + "model IS NOT NULL AND latency_ms IS NOT NULL" + for r in self._db.execute( + f"SELECT model, COUNT(*), AVG(latency_ms) FROM api_calls{lcond} GROUP BY model", + params): + latency[r[0]] = {"count": r[1], "avg": round(r[2] or 0, 3)} + # p50 / p95:表很小,用 offset 取分位值 + for mid, d in list(latency.items()): + for key, frac in (("p50", 0.5), ("p95", 0.95)): + off = max(0, int((d["count"] - 1) * frac)) + c2 = (f"{where} AND " if where else " WHERE ") + "model IS ? AND latency_ms IS NOT NULL" + row = self._db.execute( + f"SELECT latency_ms FROM api_calls{c2} ORDER BY latency_ms LIMIT 1 OFFSET ?", + [*params, mid, off]).fetchone() + d[key] = row[0] if row else None + tok = self._db.execute( + f"SELECT COALESCE(SUM(tok_prompt),0), COALESCE(SUM(tok_completion),0), " + f"COALESCE(SUM(tok_cached),0), COALESCE(SUM(tok_reasoning),0), COUNT(*) " + f"FROM api_calls{where}", params).fetchone() + except Exception as e: # noqa: BLE001 + log.warning("用量聚合失败: %s", e) + return {"enabled": False, "error": str(e)} + return {"enabled": True, "calls": calls, "providers": providers, "latency": latency, + "tokens": {"prompt": tok[0], "completion": tok[1], + "cached": tok[2], "reasoning": tok[3]}, + "total": tok[4], "since": since} + + def usage_monthly(self, month: str | None = None) -> dict[str, Any]: + """按月统计每个账号的用量(调用数 / 成功失败 / 平均耗时 / 内容字节 / token 不提供)。""" + if not self.enabled or self._db is None: + return {"enabled": False, "month": month, "items": []} + if not month: + month = time.strftime("%Y-%m") + try: + start = time.mktime(time.strptime(month + "-01", "%Y-%m-%d")) + except ValueError: + return {"enabled": False, "month": month, "error": "月份格式应为 YYYY-MM", "items": []} + # 下月 1 号 + y, m = int(month[:4]), int(month[5:7]) + ny, nm = (y + 1, 1) if m == 12 else (y, m + 1) + end = time.mktime(time.strptime(f"{ny:04d}-{nm:02d}-01", "%Y-%m-%d")) + try: + with self._lock: + rows = self._db.execute( + "SELECT account, COUNT(*), SUM(CASE WHEN ok=1 THEN 1 ELSE 0 END), " + "SUM(CASE WHEN ok=0 THEN 1 ELSE 0 END), AVG(latency_ms), " + "COUNT(DISTINCT model) FROM api_calls WHERE ts >= ? AND ts < ? " + "GROUP BY account ORDER BY COUNT(*) DESC", (start, end)).fetchall() + except Exception as e: # noqa: BLE001 + return {"enabled": False, "month": month, "error": str(e), "items": []} + items = [{"account": r[0] or "anonymous", "calls": r[1] or 0, "ok": r[2] or 0, + "fail": r[3] or 0, "avg_ms": round(r[4], 1) if r[4] else 0, + "models": r[5] or 0} for r in rows] + return {"enabled": True, "month": month, "items": items, + "total_calls": sum(i["calls"] for i in items)} + def export_rows(self, **kw: Any) -> list[dict[str, Any]]: """导出用:不带分页,最多 50000 条。""" kw.setdefault("limit", 50000) @@ -387,6 +490,12 @@ def _trim(d: dict[str, dict]) -> None: def _row(entry: dict[str, Any], meta: dict[str, Any]) -> dict[str, Any]: + rid = str(entry.get("rid") or meta.get("rid") or "") + tok = entry.get("tokens") + if not tok and rid: + with _tok_lock: + tok = TOKENS_BY_RID.pop(rid, None) + tok = tok or {} return { "ts": entry.get("ts") or time.time(), "rid": entry.get("rid") or meta.get("rid"), @@ -406,11 +515,39 @@ def _row(entry: dict[str, Any], meta: dict[str, Any]) -> dict[str, Any]: "ip": meta.get("ip", ""), "ua": _clip(meta.get("ua", ""), 200), "prompt": _clip(entry.get("body"), CONTENT_MAX), + "tok_prompt": int(tok.get("prompt_tokens") or 0), + "tok_completion": int(tok.get("completion_tokens") or 0), + "tok_cached": int(tok.get("cached_tokens") or 0), + "tok_reasoning": int(tok.get("reasoning_tokens") or 0), "completion": _clip(entry.get("response"), CONTENT_MAX), "ver": meta.get("ver", ""), } +# token 侧信道:`_record_usage_metrics()` 只知道 token、不知道 rid;请求中间件只知道 rid、 +# 不知道 token。用一个按 rid 索引的小字典把两半接起来(AccessLog.add 落库时取走并清空)。 +TOKENS_BY_RID: dict[str, dict[str, int]] = {} +_tok_lock = threading.Lock() +_TOK_MAX = 2000 + + +def note_tokens(rid: str, prompt: int, completion: int, + cached: int = 0, reasoning: int = 0) -> None: + """记下某次请求的 token 用量(由 `_record_usage_metrics` 调用)。""" + if not store.enabled or not rid: + return + with _tok_lock: + TOKENS_BY_RID[rid] = {"prompt_tokens": int(prompt or 0), + "completion_tokens": int(completion or 0), + "cached_tokens": int(cached or 0), + "reasoning_tokens": int(reasoning or 0)} + while len(TOKENS_BY_RID) > _TOK_MAX: + try: + TOKENS_BY_RID.pop(next(iter(TOKENS_BY_RID)), None) + except (StopIteration, KeyError): + break + + def on_access(entry: dict[str, Any]) -> None: """`AccessLog.add()` 调用:拿到访问日志条目;若响应元数据已就绪则立即落库。""" if not store.enabled: diff --git a/app/main.py b/app/main.py index 0ae0e09..a61237b 100644 --- a/app/main.py +++ b/app/main.py @@ -45,7 +45,7 @@ from app.config import (ConfigError, GatewayConfig, load_config, validate_warnin PROTOCOL_CHAT, PROTOCOL_MESSAGES, PROTOCOL_RESPONSES, PROTOCOLS, ALIAS_PROTOCOLS) from app import calllog -from app.metrics import (Metrics, AccessLog, RateLimiter, BODY_BY_RID, _BODY_MAX, +from app.metrics import (Metrics, AccessLog, RateLimiter, BODY_BY_RID, RID, _BODY_MAX, video_poll_push, video_polls, ACCOUNT, ROLE) from app.models import (ChatRequest, ImageRequest, VideoCreateRequestExtended, BatchRequest, @@ -351,7 +351,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.8.0" +VERSION = "2.9.0" app = FastAPI( title="API Gateway", @@ -606,6 +606,7 @@ def _flush_calllog(request: Request, rid: str, status: int, latency_ms: float, async def add_request_id(request: Request, call_next): rid = _sanitize_rid(request.headers.get("X-Request-ID")) or str(uuid.uuid4()) request.state.rid = rid + RID.set(rid) # 2026-10-23:token 记账靠这个 rid 关联到调用日志那条记录 t0 = time.perf_counter() status = 500 is_stream = False @@ -1441,6 +1442,11 @@ def _record_usage_metrics(usage) -> None: p, c, cached, reasoning = _usage_detail(usage) if p or c or cached or reasoning: _metrics.record_tokens(p, c, cached=cached, reasoning=reasoning) + # 2026-10-23:同时告诉调用日志(按 rid 暂存,落库时带上 token 数) + try: + calllog.note_tokens(RID.get(), p, c, cached=cached, reasoning=reasoning) + except Exception: # noqa: BLE001 + pass class _UsageScanner: @@ -1711,6 +1717,12 @@ async def _passthrough_stream(request, rid, caller, protocol, adapter, cb, _metrics.record_tokens(counts["prompt_tokens"], counts["completion_tokens"], cached=counts["cached_tokens"], reasoning=counts["reasoning_tokens"]) + try: + calllog.note_tokens(RID.get(), counts["prompt_tokens"], counts["completion_tokens"], + cached=counts["cached_tokens"], + reasoning=counts["reasoning_tokens"]) + except Exception: # noqa: BLE001 + pass _access.add({"method": "POST", "kind": "chat", "path": f"/v1/{protocol}", "rid": rid, "model": model_in, "served": model_in, "provider": pkey, "caller": caller, "ok": ok, "stream": True, "response": ""}) @@ -2993,6 +3005,33 @@ async def media_download(request: Request, url: str = "", filename: str = "", # ───────────────────────────────────── # GET /metrics + /metrics/prometheus(P1-3/8) # ───────────────────────────────────── +def _usage_snapshot(account: str | None = None) -> dict: + """指标快照(2026-10-23 起口径变化)。 + + **调用数 / 供应商分布 / token / 耗时** 来自 SQLite(`api_calls`,保留 90 天)—— + 所以**重启不再清零**,Dashboard 看到的是历史累计; + **运行时长 / 在途请求** 仍是进程内实时值(DB 里没有意义)。 + 库不可用时自动回落到内存态,行为与之前一致。 + """ + rt = _metrics.snapshot() + base: dict = {"uptime_seconds": rt.get("uptime_seconds"), + "in_flight": rt.get("in_flight", 0), + "in_flight_peak": rt.get("in_flight_peak", 0)} + db = calllog.store.usage_snapshot(account=account) if calllog.store.enabled else {"enabled": False} + if db.get("enabled"): + base.update({"calls": db.get("calls") or {}, + "providers": db.get("providers") or {}, + "tokens": db.get("tokens") or {}, + "latency": db.get("latency") or {}, + "total_calls": db.get("total", 0), + "source": "db"}) + else: + mem = _metrics.snapshot(account) + base.update({k: mem.get(k) for k in ("calls", "providers", "tokens", "latency") if k in mem}) + base["source"] = "memory" + return base + + @app.get("/metrics") async def metrics(request: Request, _auth=Depends(require_auth)): if not _cfg().server.metrics_enabled: @@ -3001,10 +3040,10 @@ async def metrics(request: Request, _auth=Depends(require_auth)): # 2026-10-22(老板):成员首页要看自己调用的次数统计,所以**不**把本端点收成仅管理员; # 按账号聚合的逻辑保持原样(全局字段的收敛在 /health 里做)。 if request.state.role == "admin": - snap = _metrics.snapshot() + snap = _usage_snapshot() snap["circuits"] = _circuits.statuses() # P2-16 熔断状态 else: - snap = _metrics.snapshot(request.state.account) + snap = _usage_snapshot(request.state.account) snap["circuits"] = [] return snap @@ -3025,7 +3064,7 @@ async def metrics_prometheus(request: Request, _auth=Depends(require_auth)): raise HTTPException(403, "仅管理员可抓取指标") if not _cfg().server.metrics_enabled: raise HTTPException(404, "metrics 已禁用") - snap = _metrics.snapshot() + snap = _usage_snapshot() lines = [ "# API Gateway Prometheus 指标", f'api_gateway_uptime_seconds {snap["uptime_seconds"]}', @@ -3203,6 +3242,36 @@ async def logs_calls(request: Request, start: str | None = None, end: str | None limit=limit, offset=offset, with_content=bool(content)) +@app.get("/logs/usage/monthly") +async def logs_usage_monthly(request: Request, month: str | None = None, + _auth=Depends(require_auth)): + """按月统计每个账号的用量(调用数/成功失败/平均耗时/覆盖模型数)。仅管理员。""" + _require_admin_logs(request) + return calllog.store.usage_monthly(month) + + +@app.get("/logs/usage/monthly/export") +async def logs_usage_monthly_export(request: Request, month: str | None = None, + _auth=Depends(require_auth)): + """月度用量 CSV 导出(按账号一行)。仅管理员。""" + _require_admin_logs(request) + data = calllog.store.usage_monthly(month) + if not data.get("enabled", True) and data.get("error"): + raise HTTPException(400, data["error"]) + import csv as _csv + import io as _io + buf = _io.StringIO() + w = _csv.writer(buf) + w.writerow(["月份", "账号", "调用数", "成功", "失败", "失败率", "平均耗时ms", "涉及模型数"]) + for it in data.get("items", []): + rate = f'{it["fail"] / it["calls"] * 100:.1f}%' if it.get("calls") else "-" + w.writerow([data.get("month"), it.get("account"), it.get("calls"), it.get("ok"), + it.get("fail"), rate, it.get("avg_ms"), it.get("models")]) + fname = f"usage-{data.get('month')}.csv" + return Response(content=buf.getvalue().encode("utf-8-sig"), media_type="text/csv", + headers={"Content-Disposition": f'attachment; filename="{fname}"'}) + + @app.get("/logs/calls/{call_id}") async def logs_call_detail(request: Request, call_id: int, _auth=Depends(require_auth)): _require_admin_logs(request) @@ -3930,7 +3999,7 @@ async def health(request: Request, _auth=Depends(require_auth_or_open)): if not getattr(request.state, "health_authed", False): return {"status": "ok"} cfg = _cfg() - snap = _metrics.snapshot() if cfg.server.metrics_enabled else {} + snap = _usage_snapshot() if cfg.server.metrics_enabled else {} # 2026-10-22(老板):成员首页需要"运行时长 / 并发 / 供应商列表"这些运行态, # 但**全局** token 总量、各模型的全局调用量与失败率、供应商熔断状态属运维数据, # 只应收口给管理员(此前任何有效 Bearer 都能读到全站用量)。 diff --git a/frontend/src/locales/en.ts b/frontend/src/locales/en.ts index ead80b2..7f395e1 100644 --- a/frontend/src/locales/en.ts +++ b/frontend/src/locales/en.ts @@ -229,6 +229,14 @@ export default { prompt: 'Prompt (request body, truncated)', completion: 'Completion (truncated)', loading: 'Loading…', + usageTitle: 'Monthly usage', + usageMonth: 'Month', + usageTotal: '{n} calls this month', + colCalls: 'Calls', + colOk: 'OK', + colFail: 'Failed', + colFailRate: 'Fail rate', + colModels: 'Models', }, users: { diff --git a/frontend/src/locales/zh.ts b/frontend/src/locales/zh.ts index c32ea8b..c1bf7d7 100644 --- a/frontend/src/locales/zh.ts +++ b/frontend/src/locales/zh.ts @@ -229,6 +229,14 @@ export default { prompt: '提示词(请求体,截断记录)', completion: '回复(截断记录)', loading: '加载中…', + usageTitle: '月度用量', + usageMonth: '选择月份', + usageTotal: '本月共 {n} 次调用', + colCalls: '调用数', + colOk: '成功', + colFail: '失败', + colFailRate: '失败率', + colModels: '模型数', }, users: { diff --git a/frontend/src/views/CallLogs.vue b/frontend/src/views/CallLogs.vue index 517c3b0..5d83bb7 100644 --- a/frontend/src/views/CallLogs.vue +++ b/frontend/src/views/CallLogs.vue @@ -17,6 +17,11 @@ + + + {{ t('calllogs.usageTitle') }} + {{ t('common.refresh') }} @@ -70,6 +75,31 @@ + + +
+ {{ usage.month || month }} + {{ t('calllogs.usageTotal', { n: usage.total_calls || 0 }) }} + + + {{ t('common.export') }} + +
+ + + + + + + + + + + + + +
+
@@ -125,6 +155,37 @@ const kw = ref('') const dlg = ref(false) const detail = ref(null) +// 月度用量(按账号聚合) +const month = ref(new Date().toISOString().slice(0, 7)) +const usageDlg = ref(false) +const usage = ref({}) + +async function openUsage() { + usageDlg.value = true + try { + usage.value = await api(`/logs/usage/monthly?month=${month.value}`) + } catch { + usage.value = { items: [] } + } +} + +async function exportUsage() { + const resp = await fetch(`/logs/usage/monthly/export?month=${month.value}`, { + headers: { Authorization: `Bearer ${getToken()}` }, + }) + if (!resp.ok) return + const blob = await resp.blob() + const href = URL.createObjectURL(blob) + const a = document.createElement('a') + a.href = href + a.download = `usage-${month.value}.csv` + a.style.display = 'none' + document.body.appendChild(a) + a.click() + a.remove() + setTimeout(() => URL.revokeObjectURL(href), 10_000) +} + const failCount = computed(() => rows.value.filter((r) => !r.ok).length) const avgMs = computed(() => { const v = rows.value.map((r) => Number(r.latency_ms)).filter((n) => Number.isFinite(n) && n > 0)