- 新增 POST /api/admin/users/leaderboard/refresh-today:同步强制重建当天快照 并返回合并区间;前端 useRefreshTodayLeaderboard + AdminUserLeaderboardPage 「刷新当天」按钮 - Gateway 调度循环在每日 ADMIN_LEADERBOARD_FINALIZE_HOUR(默认凌晨1点)对前一天 强制重算一次,避免临近午夜落库的 runs/tool-calls 漏统计;判定逻辑下沉为纯函数 deerflow.persistence.admin_stats.finalize.should_finalize_previous_day - 新增 scripts/sql/column_additions_mysql.sql:既有 MySQL 库手动补列的幂等脚本 (Alembic 自动迁移已禁用,create_all 不改既有表) - CLAUDE.md 补充排行榜快照、Schema 迁移约定,并补齐技能/知识库选模型的文档 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
236 lines
6.4 KiB
TypeScript
236 lines
6.4 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import {
|
|
addAdminMemoryEntry,
|
|
addAdminV1Fact,
|
|
deleteAdminMemoryEntry,
|
|
deleteAdminV1Fact,
|
|
generateUserLeaderboardReport,
|
|
getUserMemory,
|
|
getUserAnalytics,
|
|
getUserLeaderboard,
|
|
refreshTodayLeaderboard,
|
|
listUserQa,
|
|
listUserThreadMessages,
|
|
listUserThreads,
|
|
replaceAdminMemoryEntry,
|
|
updateAdminV1Fact,
|
|
updateAdminV1Section,
|
|
} from "./api";
|
|
|
|
export function useUserLeaderboard(
|
|
range: {
|
|
since: string;
|
|
until: string;
|
|
days?: number;
|
|
},
|
|
limit = 20,
|
|
) {
|
|
return useQuery({
|
|
queryKey: [
|
|
"admin-activity",
|
|
"leaderboard",
|
|
range.since,
|
|
range.until,
|
|
limit,
|
|
],
|
|
queryFn: () => getUserLeaderboard(range, limit),
|
|
refetchInterval: (query) => {
|
|
const status = query.state.data?.status;
|
|
return status === "running" || status === "refreshing" ? 3000 : false;
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Force-rebuild today's snapshot, then prime + invalidate the matching
|
|
* leaderboard query so the page shows the freshly recomputed numbers.
|
|
*/
|
|
export function useRefreshTodayLeaderboard() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({
|
|
range,
|
|
limit = 20,
|
|
}: {
|
|
range: { since: string; until: string; days?: number };
|
|
limit?: number;
|
|
}) => refreshTodayLeaderboard(range, limit),
|
|
onSuccess: (data, variables) => {
|
|
qc.setQueryData(
|
|
[
|
|
"admin-activity",
|
|
"leaderboard",
|
|
variables.range.since,
|
|
variables.range.until,
|
|
variables.limit ?? 20,
|
|
],
|
|
data,
|
|
);
|
|
qc.invalidateQueries({ queryKey: ["admin-activity", "leaderboard"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUserAnalytics(
|
|
userId: string | null,
|
|
range: { since: string; until: string; days?: number },
|
|
limit = 20,
|
|
) {
|
|
return useQuery({
|
|
queryKey: ["admin-activity", "analytics", userId, range.since, range.until, limit],
|
|
queryFn: () => getUserAnalytics(userId!, range, limit),
|
|
enabled: !!userId,
|
|
});
|
|
}
|
|
|
|
export function useGenerateUserLeaderboardReport() {
|
|
return useMutation({
|
|
mutationFn: ({
|
|
range,
|
|
limit,
|
|
modelName,
|
|
}: {
|
|
range: { since: string; until: string; days?: number };
|
|
limit?: number;
|
|
modelName?: string;
|
|
}) => generateUserLeaderboardReport(range, limit ?? 30, modelName),
|
|
});
|
|
}
|
|
|
|
/** Threads owned by a given user. Disabled until a user is picked. */
|
|
export function useUserThreads(userId: string | null) {
|
|
return useQuery({
|
|
queryKey: ["admin-activity", "threads", userId],
|
|
queryFn: () => listUserThreads(userId!),
|
|
enabled: !!userId,
|
|
});
|
|
}
|
|
|
|
export function useUserQa(
|
|
userId: string | null,
|
|
params: { page: number; pageSize: number; q?: string },
|
|
) {
|
|
return useQuery({
|
|
queryKey: ["admin-activity", "qa", userId, params.page, params.pageSize, params.q ?? ""],
|
|
queryFn: () => listUserQa(userId!, params),
|
|
enabled: !!userId,
|
|
});
|
|
}
|
|
|
|
/** Messages of a given thread. Disabled until a thread is picked. */
|
|
export function useUserThreadMessages(
|
|
userId: string | null,
|
|
threadId: string | null,
|
|
) {
|
|
return useQuery({
|
|
queryKey: ["admin-activity", "messages", userId, threadId],
|
|
queryFn: () => listUserThreadMessages(userId!, threadId!),
|
|
enabled: !!userId && !!threadId,
|
|
});
|
|
}
|
|
|
|
/** All memory buckets of a given user. Disabled until a user is picked. */
|
|
export function useUserMemory(userId: string | null) {
|
|
return useQuery({
|
|
queryKey: ["admin-activity", "memory", userId],
|
|
queryFn: () => getUserMemory(userId!),
|
|
enabled: !!userId,
|
|
});
|
|
}
|
|
|
|
function useMemoryInvalidate(userId: string) {
|
|
const qc = useQueryClient();
|
|
return () => qc.invalidateQueries({ queryKey: ["admin-activity", "memory", userId] });
|
|
}
|
|
|
|
export function useAdminAddMemoryEntry(userId: string) {
|
|
const invalidate = useMemoryInvalidate(userId);
|
|
return useMutation({
|
|
mutationFn: ({ content, agent }: { content: string; agent: string | null }) =>
|
|
addAdminMemoryEntry(userId, content, agent),
|
|
onSuccess: invalidate,
|
|
});
|
|
}
|
|
|
|
export function useAdminReplaceMemoryEntry(userId: string) {
|
|
const invalidate = useMemoryInvalidate(userId);
|
|
return useMutation({
|
|
mutationFn: ({ oldText, content, agent }: { oldText: string; content: string; agent: string | null }) =>
|
|
replaceAdminMemoryEntry(userId, oldText, content, agent),
|
|
onSuccess: invalidate,
|
|
});
|
|
}
|
|
|
|
export function useAdminDeleteMemoryEntry(userId: string) {
|
|
const invalidate = useMemoryInvalidate(userId);
|
|
return useMutation({
|
|
mutationFn: ({ oldText, agent }: { oldText: string; agent: string | null }) =>
|
|
deleteAdminMemoryEntry(userId, oldText, agent),
|
|
onSuccess: invalidate,
|
|
});
|
|
}
|
|
|
|
// ── V1 CRUD hooks ──────────────────────────────────────────────────────────
|
|
|
|
export function useAdminUpdateV1Section(userId: string) {
|
|
const invalidate = useMemoryInvalidate(userId);
|
|
return useMutation({
|
|
mutationFn: ({
|
|
sectionType,
|
|
field,
|
|
summary,
|
|
agent,
|
|
}: {
|
|
sectionType: "user" | "history";
|
|
field: string;
|
|
summary: string;
|
|
agent: string | null;
|
|
}) => updateAdminV1Section(userId, sectionType, field, summary, agent),
|
|
onSuccess: invalidate,
|
|
});
|
|
}
|
|
|
|
export function useAdminAddV1Fact(userId: string) {
|
|
const invalidate = useMemoryInvalidate(userId);
|
|
return useMutation({
|
|
mutationFn: ({
|
|
content,
|
|
category,
|
|
confidence,
|
|
agent,
|
|
}: {
|
|
content: string;
|
|
category: string;
|
|
confidence: number;
|
|
agent: string | null;
|
|
}) => addAdminV1Fact(userId, content, category, confidence, agent),
|
|
onSuccess: invalidate,
|
|
});
|
|
}
|
|
|
|
export function useAdminUpdateV1Fact(userId: string) {
|
|
const invalidate = useMemoryInvalidate(userId);
|
|
return useMutation({
|
|
mutationFn: ({
|
|
factId,
|
|
updates,
|
|
agent,
|
|
}: {
|
|
factId: string;
|
|
updates: { content?: string; category?: string; confidence?: number };
|
|
agent: string | null;
|
|
}) => updateAdminV1Fact(userId, factId, updates, agent),
|
|
onSuccess: invalidate,
|
|
});
|
|
}
|
|
|
|
export function useAdminDeleteV1Fact(userId: string) {
|
|
const invalidate = useMemoryInvalidate(userId);
|
|
return useMutation({
|
|
mutationFn: ({ factId, agent }: { factId: string; agent: string | null }) =>
|
|
deleteAdminV1Fact(userId, factId, agent),
|
|
onSuccess: invalidate,
|
|
});
|
|
}
|