feat(engine): 实现同步引擎与 engine_status 快照

apps/engine 独立进程(syncRound + 防重叠调度);上游 log/token;QPU 采样;
server 读 engine_status;时刻出参 toUnixSec;中文日志;zod-openapi 扩展顺序修复。
This commit is contained in:
2026-10-07 12:54:13 +08:00
parent 3e12db1706
commit d47cb5543c
28 changed files with 676 additions and 102 deletions
+10 -7
View File
@@ -3,7 +3,7 @@ import {
ERR,
KeyCrypto,
normalizeExpiresAt,
unixToIso,
toUnixSec,
type KeyItem,
} from "@new-api-key-usage/core";
import type { ApiKeyRow } from "@new-api-key-usage/db";
@@ -30,14 +30,14 @@ function toKeyItem(row: ApiKeyRow & { member_name?: string | null }): KeyItem {
status: row.status as "active" | "disabled",
member_id: row.member_id,
member_name: row.member_name ?? null,
registered_at: unixToIso(row.registered_at)!,
log_synced_at: unixToIso(row.log_synced_at),
usage_synced_at: unixToIso(row.usage_synced_at),
registered_at: toUnixSec(row.registered_at)!,
log_synced_at: toUnixSec(row.log_synced_at),
usage_synced_at: toUnixSec(row.usage_synced_at),
total_used: row.total_used,
total_available: row.total_available,
total_granted: row.total_granted,
unlimited_quota: row.unlimited_quota,
expires_at: unixToIso(row.expires_at),
expires_at: toUnixSec(row.expires_at),
};
}
@@ -85,8 +85,11 @@ export function adminKeyRoutes() {
}
let rows = await c.get("repos").keys.listAll(filter);
if (q.recent_failed === "true") {
const { getEngineStatus } = await import("./admin-engine.ts");
const failed = new Set(getEngineStatus().last_round?.failed_api_key_ids ?? []);
const snap = await c.get("repos").engine.getSnapshot();
const failed = new Set(
((snap?.last_round as { failed_api_key_ids?: number[] } | null)
?.failed_api_key_ids) ?? [],
);
rows = rows.filter((r) => failed.has(r.id));
}
return c.json(rows.map(toKeyItem), 200);