feat: 排行榜日快照分析 + 数据库连接池/索引调优
- 排行榜改为按天预计算快照(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>
This commit is contained in:
1 parent
1b75887615
commit
e7c745a512
26 files changed
+1980
-564
No files matched your search
@@ -1,47 +1,21 @@
|
||||
"use client";
|
||||
|
||||
import { Loader2, MessageSquareIcon, SearchIcon, UserIcon } from "lucide-react";
|
||||
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, useUserThreadMessages, useUserThreads } from "@/core/admin/hooks";
|
||||
import type { AdminMemoryBucket, AdminMessage } from "@/core/admin/api";
|
||||
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";
|
||||
|
||||
/** Flatten a langchain message dump (or raw string / block list) into text. */
|
||||
function extractText(content: unknown): string {
|
||||
if (content == null) return "";
|
||||
if (typeof content === "string") return content;
|
||||
if (Array.isArray(content)) {
|
||||
return content
|
||||
.map((block) => {
|
||||
if (typeof block === "string") return block;
|
||||
if (block && typeof block === "object") {
|
||||
const text = (block as Record<string, unknown>).text;
|
||||
if (typeof text === "string") return text;
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.join("");
|
||||
}
|
||||
if (typeof content === "object") {
|
||||
const inner = (content as Record<string, unknown>).content;
|
||||
if (inner !== undefined) return extractText(inner);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function roleOf(message: AdminMessage): "user" | "assistant" | null {
|
||||
const type = (message.type ?? "").toLowerCase();
|
||||
if (type === "human") return "user";
|
||||
if (type === "ai" || type === "aimessagechunk") return "assistant";
|
||||
return null; // tool results and system messages are hidden from the Q&A view
|
||||
}
|
||||
const QA_PAGE_SIZE = 20;
|
||||
|
||||
function fmtTime(iso: string): string {
|
||||
if (!iso) return "";
|
||||
@@ -49,24 +23,39 @@ function fmtTime(iso: string): string {
|
||||
return Number.isNaN(d.getTime()) ? iso : d.toLocaleString();
|
||||
}
|
||||
|
||||
/** A single Q&A bubble. */
|
||||
function MessageRow({ message }: { message: AdminMessage }) {
|
||||
const role = roleOf(message);
|
||||
if (!role) return null;
|
||||
const text = extractText(message.content).trim();
|
||||
if (!text) return null; // skip empty AI turns (e.g. tool-call-only responses)
|
||||
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={cn("flex flex-col gap-1", role === "user" ? "items-start" : "items-end")}>
|
||||
<span className="text-muted-foreground text-xs">
|
||||
{role === "user" ? "用户提问" : "AI 回答"}
|
||||
</span>
|
||||
<div
|
||||
className={cn(
|
||||
"max-w-[85%] whitespace-pre-wrap rounded-lg border px-3 py-2 text-sm",
|
||||
role === "user" ? "bg-muted" : "bg-primary/5 border-primary/20",
|
||||
)}
|
||||
>
|
||||
{text}
|
||||
<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>
|
||||
);
|
||||
@@ -115,24 +104,33 @@ export default function AdminUserActivityPage() {
|
||||
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: messages, isLoading: messagesLoading } = useUserThreadMessages(
|
||||
userId,
|
||||
threadId,
|
||||
);
|
||||
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 ?? [];
|
||||
@@ -144,9 +142,17 @@ export default function AdminUserActivityPage() {
|
||||
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">
|
||||
@@ -203,8 +209,19 @@ export default function AdminUserActivityPage() {
|
||||
|
||||
{/* Pane 2: threads */}
|
||||
<div className="flex w-72 shrink-0 flex-col border-r">
|
||||
<div className="text-muted-foreground border-b px-3 py-2 text-xs font-medium">
|
||||
会话列表{threads ? `(${threads.length})` : ""}
|
||||
<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 && (
|
||||
@@ -229,6 +246,8 @@ export default function AdminUserActivityPage() {
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setThreadId(thread.thread_id);
|
||||
setQaQuery(thread.thread_id);
|
||||
setQaPage(1);
|
||||
setTab("qa");
|
||||
}}
|
||||
className={cn(
|
||||
@@ -270,28 +289,63 @@ export default function AdminUserActivityPage() {
|
||||
|
||||
<ScrollArea className="min-h-0 flex-1">
|
||||
{tab === "qa" && (
|
||||
<div className="p-4">
|
||||
{!threadId && (
|
||||
<div className="text-muted-foreground flex flex-col items-center gap-2 py-16 text-sm">
|
||||
<MessageSquareIcon className="size-6" />
|
||||
请从中间列选择一个会话
|
||||
<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>
|
||||
)}
|
||||
{threadId && messagesLoading && (
|
||||
<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>
|
||||
)}
|
||||
{threadId && !messagesLoading && (
|
||||
{!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">
|
||||
{(messages ?? []).map((m, i) => (
|
||||
<MessageRow key={m.id ?? i} message={m} />
|
||||
{(qaData?.items ?? []).map((item) => (
|
||||
<QaItemCard key={item.run_id} item={item} />
|
||||
))}
|
||||
{(messages ?? []).every((m) => !roleOf(m)) && (
|
||||
<div className="text-muted-foreground py-12 text-center text-sm">
|
||||
该会话暂无问答记录
|
||||
</div>
|
||||
)}
|
||||
<Pagination
|
||||
page={qaPage}
|
||||
pageSize={QA_PAGE_SIZE}
|
||||
total={qaData?.total ?? 0}
|
||||
onPageChange={setQaPage}
|
||||
className="pt-2"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user