feat(lpt-ai):管理员面板 —— 请求日志记录与查看
新增文件: - src/admin/store.ts:环形缓冲区(200条),文件持久化为 data/logs.json - src/admin/routes.ts:admin API(列表/详情/清除)+ 自包含 HTML 面板 - src/admin/index.ts:统一导出 面板功能: - GET /admin:绿白配色自包含面板,零外部依赖 - 统计栏:总请求/成功/失败/平均耗时 - 日志表格:时间、端点、状态、错误、耗时、模型 - 点击行展开查看完整请求体/响应体 - 端点筛选下拉、一键清除 - 响应式布局 修改文件: - src/routes/ai.ts:两个 AI handler 均注入日志记录 (记录时间戳、端点、模型、耗时、状态码、请求体、响应体、错误) - src/index.ts:注册 adminRoutes 验证: - npm run typecheck 通过 - npm test 4 个测试通过
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* 请求日志存储 —— 环形缓冲区 + 可选文件持久化。
|
||||
*
|
||||
* 每条日志记录 AI 路由的一次完整请求/响应,供 /admin 面板查看。
|
||||
*/
|
||||
|
||||
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
// ---- 类型 ----
|
||||
|
||||
export interface LogEntry {
|
||||
id: number;
|
||||
timestamp: string; // ISO 8601
|
||||
endpoint: string; // 如 "/ai/aggregate-report"
|
||||
model: string;
|
||||
durationMs: number;
|
||||
status: number; // HTTP status code
|
||||
requestBody: unknown;
|
||||
responseBody: unknown;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface LogSummary {
|
||||
id: number;
|
||||
timestamp: string;
|
||||
endpoint: string;
|
||||
model: string;
|
||||
durationMs: number;
|
||||
status: number;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
// ---- 存储实现 ----
|
||||
|
||||
const MAX_ENTRIES = 200;
|
||||
const DATA_DIR = resolve(process.cwd(), "data");
|
||||
const LOG_FILE = resolve(DATA_DIR, "logs.json");
|
||||
|
||||
class LogStore {
|
||||
private entries: LogEntry[] = [];
|
||||
private nextId = 1;
|
||||
private dirty = false;
|
||||
|
||||
constructor() {
|
||||
this.load();
|
||||
}
|
||||
|
||||
/** 追加一条日志,超出上限时移除最旧的 */
|
||||
add(entry: Omit<LogEntry, "id">): LogEntry {
|
||||
const full: LogEntry = { id: this.nextId++, ...entry };
|
||||
this.entries.push(full);
|
||||
while (this.entries.length > MAX_ENTRIES) {
|
||||
this.entries.shift();
|
||||
}
|
||||
this.dirty = true;
|
||||
this.flush(); // fire-and-forget
|
||||
return full;
|
||||
}
|
||||
|
||||
/** 列出摘要(不含请求/响应体,减少面板传输量) */
|
||||
list(endpoint?: string): LogSummary[] {
|
||||
let result = [...this.entries];
|
||||
if (endpoint) {
|
||||
result = result.filter((e) => e.endpoint === endpoint);
|
||||
}
|
||||
// 倒序:最新在前
|
||||
result.reverse();
|
||||
return result.map(({ id, timestamp, endpoint, model, durationMs, status, error }) => ({
|
||||
id, timestamp, endpoint, model, durationMs, status, error,
|
||||
}));
|
||||
}
|
||||
|
||||
/** 获取单条完整日志 */
|
||||
get(id: number): LogEntry | undefined {
|
||||
return this.entries.find((e) => e.id === id);
|
||||
}
|
||||
|
||||
/** 清空 */
|
||||
clear(): void {
|
||||
this.entries = [];
|
||||
this.dirty = true;
|
||||
this.flush();
|
||||
}
|
||||
|
||||
/** 端点列表(用于筛选下拉) */
|
||||
endpoints(): string[] {
|
||||
return [...new Set(this.entries.map((e) => e.endpoint))].sort();
|
||||
}
|
||||
|
||||
// ---- 持久化 ----
|
||||
|
||||
private load(): void {
|
||||
try {
|
||||
if (existsSync(LOG_FILE)) {
|
||||
const raw = readFileSync(LOG_FILE, "utf-8");
|
||||
const data = JSON.parse(raw) as { nextId: number; entries: LogEntry[] };
|
||||
this.nextId = data.nextId ?? 1;
|
||||
this.entries = data.entries ?? [];
|
||||
// 裁剪
|
||||
while (this.entries.length > MAX_ENTRIES) {
|
||||
this.entries.shift();
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// 文件损坏或不存在,使用空存储
|
||||
}
|
||||
}
|
||||
|
||||
private flush(): void {
|
||||
if (!this.dirty) return;
|
||||
this.dirty = false;
|
||||
try {
|
||||
if (!existsSync(DATA_DIR)) {
|
||||
mkdirSync(DATA_DIR, { recursive: true });
|
||||
}
|
||||
writeFileSync(LOG_FILE, JSON.stringify({ nextId: this.nextId, entries: this.entries }), "utf-8");
|
||||
} catch {
|
||||
// 写入失败不阻塞服务
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const logStore = new LogStore();
|
||||
Reference in New Issue
Block a user