- 排行榜改为按天预计算快照(admin_leaderboard_daily_stats),请求只读取合并,重统计移至后台调度;过去日不可变、当天 TTL 刷新,避免在线聚合长时间占用数据库 - 为 runs/tool_call_metrics/llm_call_metrics/threads_meta 增加面向时间段+用户聚合的复合索引(model __table_args__ + migration 20260608_01) - 连接池可调:DatabaseConfig 新增 max_overflow / pool_timeout,接入 postgres/mysql 引擎;config.yaml 上调 pool_size=20/max_overflow=20 - 新增手动执行的索引脚本 scripts/sql/leaderboard_indexes_mysql.sql(幂等,适用于已存在数据、create_all 不补索引且 alembic 关闭的部署) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
380 lines
15 KiB
TypeScript
380 lines
15 KiB
TypeScript
"use client";
|
||
|
||
import { Loader2, MessageSquareIcon, SearchIcon, UserIcon, XIcon } from "lucide-react";
|
||
import { useEffect, useMemo, useState } from "react";
|
||
import { useSearchParams } from "react-router-dom";
|
||
|
||
import { Badge } from "@/components/ui/badge";
|
||
import { Button } from "@/components/ui/button";
|
||
import { Input } from "@/components/ui/input";
|
||
import { Pagination } from "@/components/ui/pagination";
|
||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||
import { useUserMemory, useUserQa, useUserThreads } from "@/core/admin/hooks";
|
||
import type { AdminMemoryBucket, AdminUserQaItem } from "@/core/admin/api";
|
||
import { useAllUsers } from "@/core/auth/hooks";
|
||
import { cn } from "@/lib/utils";
|
||
|
||
const QA_PAGE_SIZE = 20;
|
||
|
||
function fmtTime(iso: string): string {
|
||
if (!iso) return "";
|
||
const d = new Date(iso);
|
||
return Number.isNaN(d.getTime()) ? iso : d.toLocaleString();
|
||
}
|
||
|
||
function fmtNumber(value: number): string {
|
||
return new Intl.NumberFormat("zh-CN").format(value || 0);
|
||
}
|
||
|
||
function QaItemCard({ item }: { item: AdminUserQaItem }) {
|
||
const isError = item.status === "error" || item.status === "timeout";
|
||
return (
|
||
<div className="rounded-md border bg-background p-4">
|
||
<div className="mb-3 flex flex-wrap items-center gap-2">
|
||
<span className="text-muted-foreground text-xs">{fmtTime(item.created_at)}</span>
|
||
<Badge variant={isError ? "destructive" : "secondary"}>{item.status || "unknown"}</Badge>
|
||
{item.model_name && <Badge variant="outline">{item.model_name}</Badge>}
|
||
{item.assistant_id && <Badge variant="outline">{item.assistant_id}</Badge>}
|
||
<span className="text-muted-foreground text-xs">{fmtNumber(item.total_tokens)} tokens</span>
|
||
</div>
|
||
{(item.thread_title || item.thread_id) && (
|
||
<div className="text-muted-foreground mb-3 truncate text-xs">
|
||
{item.thread_title || "未命名会话"} · {item.thread_id}
|
||
</div>
|
||
)}
|
||
<div className="grid gap-3">
|
||
<div>
|
||
<div className="text-muted-foreground mb-1 text-xs font-medium">用户提问</div>
|
||
<div className="bg-muted whitespace-pre-wrap rounded-md px-3 py-2 text-sm">
|
||
{item.question || "空问题"}
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<div className="text-muted-foreground mb-1 text-xs font-medium">AI 回答</div>
|
||
<div className="border-primary/20 bg-primary/5 max-h-64 overflow-auto whitespace-pre-wrap rounded-md border px-3 py-2 text-sm">
|
||
{item.answer || "暂无回答"}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
/** Render one Memory V2 bucket: a flat list of USER.md / MEMORY.md entries. */
|
||
function MemoryBucketView({ bucket }: { bucket: AdminMemoryBucket }) {
|
||
const raw = (bucket.data as { entries?: unknown }).entries;
|
||
const entries = Array.isArray(raw)
|
||
? raw.filter(
|
||
(e): e is string => typeof e === "string" && e.trim().length > 0,
|
||
)
|
||
: [];
|
||
|
||
return (
|
||
<div className="rounded-md border p-4">
|
||
<div className="mb-3 flex items-center gap-2">
|
||
<Badge variant={bucket.name === "global" ? "default" : "secondary"}>
|
||
{bucket.name === "global" ? "用户画像" : `Agent: ${bucket.name}`}
|
||
</Badge>
|
||
{entries.length > 0 && (
|
||
<span className="text-muted-foreground text-xs">
|
||
{entries.length} 条
|
||
</span>
|
||
)}
|
||
</div>
|
||
{entries.length === 0 ? (
|
||
<p className="text-muted-foreground text-sm">该记忆库暂无内容。</p>
|
||
) : (
|
||
<ul className="flex flex-col gap-1.5">
|
||
{entries.map((entry, i) => (
|
||
<li key={i} className="flex items-start gap-2 text-sm">
|
||
<span className="text-muted-foreground">·</span>
|
||
<span className="flex-1 whitespace-pre-wrap">{entry}</span>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default function AdminUserActivityPage() {
|
||
const [searchParams] = useSearchParams();
|
||
const { data: users, isLoading: usersLoading } = useAllUsers(true);
|
||
const [userId, setUserId] = useState<string | null>(null);
|
||
const [threadId, setThreadId] = useState<string | null>(null);
|
||
const [userQuery, setUserQuery] = useState("");
|
||
const [qaQuery, setQaQuery] = useState("");
|
||
const [qaPage, setQaPage] = useState(1);
|
||
const [tab, setTab] = useState<"qa" | "memory">("qa");
|
||
const targetUserId = searchParams.get("user");
|
||
|
||
const { data: threads, isLoading: threadsLoading } = useUserThreads(userId);
|
||
const { data: qaData, isLoading: qaLoading, isFetching: qaFetching } = useUserQa(userId, {
|
||
page: qaPage,
|
||
pageSize: QA_PAGE_SIZE,
|
||
q: qaQuery,
|
||
});
|
||
const { data: memory, isLoading: memoryLoading } = useUserMemory(userId);
|
||
|
||
useEffect(() => {
|
||
if (targetUserId) {
|
||
setUserId(targetUserId);
|
||
setThreadId(null);
|
||
setQaQuery("");
|
||
setQaPage(1);
|
||
setTab("qa");
|
||
}
|
||
}, [targetUserId]);
|
||
|
||
useEffect(() => {
|
||
setQaPage(1);
|
||
}, [userId, qaQuery]);
|
||
|
||
const filteredUsers = useMemo(() => {
|
||
const needle = userQuery.trim().toLowerCase();
|
||
const list = users ?? [];
|
||
return needle ? list.filter((u) => u.email.toLowerCase().includes(needle)) : list;
|
||
}, [users, userQuery]);
|
||
|
||
const selectedUser = users?.find((u) => u.id === userId) ?? null;
|
||
|
||
const selectUser = (id: string) => {
|
||
setUserId(id);
|
||
setThreadId(null);
|
||
setQaQuery("");
|
||
setQaPage(1);
|
||
setTab("qa");
|
||
};
|
||
|
||
const clearQaFilter = () => {
|
||
setThreadId(null);
|
||
setQaQuery("");
|
||
setQaPage(1);
|
||
};
|
||
|
||
return (
|
||
<div className="flex size-full flex-col">
|
||
<div className="border-b px-6 py-4">
|
||
<h1 className="text-xl font-semibold">用户活动审计</h1>
|
||
<p className="text-muted-foreground mt-0.5 text-sm">
|
||
查看任意用户的历史问答与记忆(只读)。
|
||
</p>
|
||
</div>
|
||
|
||
<div className="flex min-h-0 flex-1">
|
||
{/* Pane 1: users */}
|
||
<div className="flex w-64 shrink-0 flex-col border-r">
|
||
<div className="relative p-2">
|
||
<SearchIcon className="text-muted-foreground absolute left-4 top-1/2 size-4 -translate-y-1/2" />
|
||
<Input
|
||
value={userQuery}
|
||
onChange={(e) => setUserQuery(e.target.value)}
|
||
placeholder="搜索用户邮箱"
|
||
className="pl-8"
|
||
/>
|
||
</div>
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
{usersLoading && (
|
||
<div className="text-muted-foreground flex items-center justify-center gap-2 py-8 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中
|
||
</div>
|
||
)}
|
||
{!usersLoading && filteredUsers.length === 0 && (
|
||
<div className="text-muted-foreground py-8 text-center text-sm">无用户</div>
|
||
)}
|
||
<div className="flex flex-col p-1">
|
||
{filteredUsers.map((u) => (
|
||
<button
|
||
key={u.id}
|
||
type="button"
|
||
onClick={() => selectUser(u.id)}
|
||
className={cn(
|
||
"flex items-center gap-2 rounded-md px-2 py-2 text-left text-sm",
|
||
u.id === userId ? "bg-accent" : "hover:bg-muted",
|
||
)}
|
||
>
|
||
<UserIcon className="text-muted-foreground size-4 shrink-0" />
|
||
<span className="flex-1 truncate">{u.email}</span>
|
||
{u.system_role === "admin" && (
|
||
<Badge variant="outline" className="shrink-0 text-xs">
|
||
管理员
|
||
</Badge>
|
||
)}
|
||
</button>
|
||
))}
|
||
</div>
|
||
</ScrollArea>
|
||
</div>
|
||
|
||
{/* Pane 2: threads */}
|
||
<div className="flex w-72 shrink-0 flex-col border-r">
|
||
<div className="flex items-center justify-between gap-2 border-b px-3 py-2">
|
||
<span className="text-muted-foreground text-xs font-medium">
|
||
会话列表{threads ? `(${threads.length})` : ""}
|
||
</span>
|
||
<Button
|
||
type="button"
|
||
variant="ghost"
|
||
size="sm"
|
||
disabled={!userId || (!threadId && !qaQuery)}
|
||
onClick={clearQaFilter}
|
||
>
|
||
全部
|
||
</Button>
|
||
</div>
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
{!userId && (
|
||
<div className="text-muted-foreground py-8 text-center text-sm">
|
||
请先选择用户
|
||
</div>
|
||
)}
|
||
{userId && threadsLoading && (
|
||
<div className="text-muted-foreground flex items-center justify-center gap-2 py-8 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中
|
||
</div>
|
||
)}
|
||
{userId && !threadsLoading && (threads?.length ?? 0) === 0 && (
|
||
<div className="text-muted-foreground py-8 text-center text-sm">
|
||
该用户暂无会话
|
||
</div>
|
||
)}
|
||
<div className="flex flex-col p-1">
|
||
{(threads ?? []).map((thread) => (
|
||
<button
|
||
key={thread.thread_id}
|
||
type="button"
|
||
onClick={() => {
|
||
setThreadId(thread.thread_id);
|
||
setQaQuery(thread.thread_id);
|
||
setQaPage(1);
|
||
setTab("qa");
|
||
}}
|
||
className={cn(
|
||
"flex flex-col gap-0.5 rounded-md px-2 py-2 text-left",
|
||
thread.thread_id === threadId ? "bg-accent" : "hover:bg-muted",
|
||
)}
|
||
>
|
||
<span className="truncate text-sm">
|
||
{thread.display_name || "未命名会话"}
|
||
</span>
|
||
<span className="text-muted-foreground text-xs">
|
||
{fmtTime(thread.updated_at)}
|
||
</span>
|
||
</button>
|
||
))}
|
||
</div>
|
||
</ScrollArea>
|
||
</div>
|
||
|
||
{/* Pane 3: detail */}
|
||
<div className="flex min-w-0 flex-1 flex-col">
|
||
{!userId ? (
|
||
<div className="text-muted-foreground flex flex-1 items-center justify-center text-sm">
|
||
选择一个用户开始审计
|
||
</div>
|
||
) : (
|
||
<>
|
||
<div className="flex items-center justify-between gap-3 border-b px-4 py-2">
|
||
<span className="text-muted-foreground truncate text-sm">
|
||
{selectedUser?.email}
|
||
</span>
|
||
<Tabs value={tab} onValueChange={(v) => setTab(v as "qa" | "memory")}>
|
||
<TabsList>
|
||
<TabsTrigger value="qa">问答记录</TabsTrigger>
|
||
<TabsTrigger value="memory">记忆</TabsTrigger>
|
||
</TabsList>
|
||
</Tabs>
|
||
</div>
|
||
|
||
<ScrollArea className="min-h-0 flex-1">
|
||
{tab === "qa" && (
|
||
<div className="flex flex-col gap-4 p-4">
|
||
<div className="flex flex-col gap-2">
|
||
<div className="flex items-center gap-2">
|
||
<div className="relative min-w-0 flex-1">
|
||
<SearchIcon className="text-muted-foreground absolute left-3 top-1/2 size-4 -translate-y-1/2" />
|
||
<Input
|
||
value={qaQuery}
|
||
onChange={(event) => {
|
||
setQaQuery(event.target.value);
|
||
setThreadId(null);
|
||
setQaPage(1);
|
||
}}
|
||
placeholder="搜索问题、回答、运行 ID 或会话 ID"
|
||
className="pl-9"
|
||
/>
|
||
</div>
|
||
{qaQuery && (
|
||
<Button type="button" variant="outline" size="sm" onClick={clearQaFilter}>
|
||
<XIcon className="size-4" />
|
||
清空
|
||
</Button>
|
||
)}
|
||
</div>
|
||
<div className="text-muted-foreground flex flex-wrap items-center gap-2 text-xs">
|
||
<span>共 {fmtNumber(qaData?.total ?? 0)} 条问答</span>
|
||
{threadId && <Badge variant="outline">已按会话过滤</Badge>}
|
||
{qaFetching && !qaLoading && (
|
||
<span className="inline-flex items-center gap-1">
|
||
<Loader2 className="size-3 animate-spin" /> 更新中
|
||
</span>
|
||
)}
|
||
</div>
|
||
</div>
|
||
|
||
{qaLoading && (
|
||
<div className="text-muted-foreground flex items-center justify-center gap-2 py-16 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中
|
||
</div>
|
||
)}
|
||
{!qaLoading && (qaData?.items.length ?? 0) === 0 && (
|
||
<div className="text-muted-foreground flex flex-col items-center gap-2 py-16 text-sm">
|
||
<MessageSquareIcon className="size-6" />
|
||
暂无匹配的问答记录
|
||
</div>
|
||
)}
|
||
{!qaLoading && (qaData?.items.length ?? 0) > 0 && (
|
||
<div className="flex flex-col gap-3">
|
||
{(qaData?.items ?? []).map((item) => (
|
||
<QaItemCard key={item.run_id} item={item} />
|
||
))}
|
||
<Pagination
|
||
page={qaPage}
|
||
pageSize={QA_PAGE_SIZE}
|
||
total={qaData?.total ?? 0}
|
||
onPageChange={setQaPage}
|
||
className="pt-2"
|
||
/>
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
|
||
{tab === "memory" && (
|
||
<div className="flex flex-col gap-3 p-4">
|
||
{memoryLoading && (
|
||
<div className="text-muted-foreground flex items-center justify-center gap-2 py-16 text-sm">
|
||
<Loader2 className="size-4 animate-spin" /> 加载中
|
||
</div>
|
||
)}
|
||
{!memoryLoading &&
|
||
(memory?.buckets ?? []).map((bucket) => (
|
||
<MemoryBucketView key={bucket.name} bucket={bucket} />
|
||
))}
|
||
{!memoryLoading && (memory?.buckets?.length ?? 0) === 0 && (
|
||
<div className="text-muted-foreground py-12 text-center text-sm">
|
||
该用户暂无记忆数据
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</ScrollArea>
|
||
</>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|