feat: problems local stats, user status, admin panel enhancements, rating text

- Problems page: replace Luogu pass rate with local submission stats
  (local_submit_count, local_ac_count)
- Problems page: add user AC/fail status column (user_ac, user_fail_count)
- Admin users: add total_submissions and total_ac columns
- Admin users: add detail panel with submissions/rating/redeem tabs
- Admin: new endpoint GET /api/v1/admin/users/{id}/rating-history
- Rating history: note field includes problem title via JOIN
- Me page: translate task codes to friendly labels with icons
- Me page: problem links in rating history are clickable
- Wrong book service, learning note scoring, note image controller
- Backend SQL uses batch queries for performance

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
这个提交包含在:
cryptocommuniums-afk
2026-02-16 17:35:22 +08:00
父节点 7860414ae5
当前提交 cfbe9a0363
修改 22 个文件,包含 1366 行新增26 行删除

查看文件

@@ -1,17 +1,26 @@
"use client";
import { useEffect, useState } from "react";
import React, { useEffect, useState } from "react";
import { apiFetch } from "@/lib/api";
import { apiFetch, type RatingHistoryItem } from "@/lib/api";
import { readToken } from "@/lib/auth";
import { useI18nText } from "@/lib/i18n";
import { RefreshCw, Save, Shield, UserCog, Users } from "lucide-react";
import {
ChevronDown,
ChevronUp,
RefreshCw,
Save,
Shield,
Users,
} from "lucide-react";
type AdminUser = {
id: number;
username: string;
rating: number;
created_at: number;
total_submissions: number;
total_ac: number;
};
type ListResp = {
@@ -21,17 +30,155 @@ type ListResp = {
page_size: number;
};
type SubmissionRow = {
id: number;
problem_id: number;
status: string;
score: number;
language: string;
created_at: number;
};
type RedeemRow = {
id: number;
item_name: string;
quantity: number;
day_type: string;
total_cost: number;
created_at: number;
};
function fmtTs(v: number): string {
if (!v) return "-";
return new Date(v * 1000).toLocaleString();
}
function DetailPanel({ userId, tx }: { userId: number; tx: (zh: string, en: string) => string }) {
const [tab, setTab] = useState<"subs" | "rating" | "redeem">("subs");
const [subs, setSubs] = useState<SubmissionRow[]>([]);
const [ratingH, setRatingH] = useState<RatingHistoryItem[]>([]);
const [redeems, setRedeems] = useState<RedeemRow[]>([]);
const [loading, setLoading] = useState(false);
const token = readToken() ?? "";
useEffect(() => {
setLoading(true);
const loadTab = async () => {
try {
if (tab === "subs") {
const d = await apiFetch<{ items: SubmissionRow[] }>(
`/api/v1/submissions?user_id=${userId}&page=1&page_size=50`,
undefined, token
);
setSubs(d.items ?? []);
} else if (tab === "rating") {
const d = await apiFetch<RatingHistoryItem[]>(
`/api/v1/admin/users/${userId}/rating-history?limit=100`,
undefined, token
);
setRatingH(Array.isArray(d) ? d : []);
} else {
const d = await apiFetch<RedeemRow[]>(
`/api/v1/admin/redeem-records?user_id=${userId}&limit=100`,
undefined, token
);
setRedeems(Array.isArray(d) ? d : []);
}
} catch { /* ignore */ }
setLoading(false);
};
void loadTab();
}, [tab, userId, token]);
const tabCls = (t: string) =>
`px-3 py-1 text-xs border ${tab === t ? "bg-zinc-900 text-white" : "bg-white text-zinc-700 hover:bg-zinc-100"}`;
return (
<div className="bg-zinc-50 border-t p-3 text-xs">
<div className="flex gap-1 mb-2">
<button className={tabCls("subs")} onClick={() => setTab("subs")}>
{tx("提交记录", "Submissions")}
</button>
<button className={tabCls("rating")} onClick={() => setTab("rating")}>
{tx("积分历史", "Rating History")}
</button>
<button className={tabCls("redeem")} onClick={() => setTab("redeem")}>
{tx("兑换记录", "Redeem Records")}
</button>
</div>
{loading && <p className="text-zinc-500">{tx("加载中...", "Loading...")}</p>}
{!loading && tab === "subs" && (
<div className="max-h-48 overflow-y-auto">
<table className="min-w-full text-xs">
<thead><tr className="text-left text-zinc-500">
<th className="pr-2">ID</th><th className="pr-2">{tx("题目", "Problem")}</th>
<th className="pr-2">{tx("状态", "Status")}</th><th className="pr-2">{tx("分数", "Score")}</th>
<th>{tx("时间", "Time")}</th>
</tr></thead>
<tbody>
{subs.map((s) => (
<tr key={s.id} className="border-t border-zinc-200">
<td className="pr-2">{s.id}</td>
<td className="pr-2">P{s.problem_id}</td>
<td className={`pr-2 font-bold ${s.status === "AC" ? "text-emerald-600" : "text-red-600"}`}>{s.status}</td>
<td className="pr-2">{s.score}</td>
<td className="text-zinc-500">{fmtTs(s.created_at)}</td>
</tr>
))}
{subs.length === 0 && <tr><td colSpan={5} className="text-zinc-400 py-2">{tx("无记录", "No records")}</td></tr>}
</tbody>
</table>
</div>
)}
{!loading && tab === "rating" && (
<div className="max-h-48 overflow-y-auto space-y-1">
{ratingH.map((item, i) => (
<div key={i} className="flex justify-between border-b border-zinc-200 pb-1">
<span>
<span className={`font-bold ${item.change > 0 ? "text-emerald-600" : "text-red-600"}`}>
{item.change > 0 ? `+${item.change}` : item.change}
</span>
<span className="ml-2 text-zinc-600">{item.note}</span>
</span>
<span className="text-zinc-400">{fmtTs(item.created_at)}</span>
</div>
))}
{ratingH.length === 0 && <p className="text-zinc-400">{tx("无记录", "No records")}</p>}
</div>
)}
{!loading && tab === "redeem" && (
<div className="max-h-48 overflow-y-auto">
<table className="min-w-full text-xs">
<thead><tr className="text-left text-zinc-500">
<th className="pr-2">{tx("物品", "Item")}</th><th className="pr-2">{tx("数量", "Qty")}</th>
<th className="pr-2">{tx("花费", "Cost")}</th><th>{tx("时间", "Time")}</th>
</tr></thead>
<tbody>
{redeems.map((r) => (
<tr key={r.id} className="border-t border-zinc-200">
<td className="pr-2">{r.item_name}</td>
<td className="pr-2">{r.quantity}</td>
<td className="pr-2 text-red-600">-{r.total_cost}</td>
<td className="text-zinc-500">{fmtTs(r.created_at)}</td>
</tr>
))}
{redeems.length === 0 && <tr><td colSpan={4} className="text-zinc-400 py-2">{tx("无记录", "No records")}</td></tr>}
</tbody>
</table>
</div>
)}
</div>
);
}
export default function AdminUsersPage() {
const { tx } = useI18nText();
const [items, setItems] = useState<AdminUser[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [msg, setMsg] = useState("");
const [expandedId, setExpandedId] = useState<number | null>(null);
const load = async () => {
setLoading(true);
@@ -110,13 +257,16 @@ export default function AdminUsersPage() {
<Shield size={14} />
Rating
</th>
<th className="px-3 py-2">{tx("提交", "Subs")}</th>
<th className="px-3 py-2">AC</th>
<th className="px-3 py-2">{tx("创建时间", "Created At")}</th>
<th className="px-3 py-2">{tx("操作", "Action")}</th>
</tr>
</thead>
<tbody>
{items.map((user) => (
<tr key={user.id} className="border-t">
<React.Fragment key={user.id}>
<tr className="border-t">
<td className="px-3 py-2">{user.id}</td>
<td className="px-3 py-2">{user.username}</td>
<td className="px-3 py-2">
@@ -133,8 +283,10 @@ export default function AdminUsersPage() {
}}
/>
</td>
<td className="px-3 py-2">{user.total_submissions}</td>
<td className="px-3 py-2 text-emerald-700 font-bold">{user.total_ac}</td>
<td className="px-3 py-2 text-zinc-600">{fmtTs(user.created_at)}</td>
<td className="px-3 py-2">
<td className="px-3 py-2 flex items-center gap-1">
<button
className="rounded border px-3 py-1 text-xs hover:bg-zinc-100 flex items-center gap-1"
onClick={() => void updateRating(user.id, Math.max(0, Number(user.rating) || 0))}
@@ -142,12 +294,22 @@ export default function AdminUsersPage() {
<Save size={12} />
{tx("保存", "Save")}
</button>
<button
className="rounded border px-2 py-1 text-xs hover:bg-zinc-100"
onClick={() => setExpandedId(expandedId === user.id ? null : user.id)}
>
{expandedId === user.id ? <ChevronUp size={14} /> : <ChevronDown size={14} />}
</button>
</td>
</tr>
{expandedId === user.id && (
<tr><td colSpan={7} className="p-0"><DetailPanel userId={user.id} tx={tx} /></td></tr>
)}
</React.Fragment>
))}
{!loading && items.length === 0 && (
<tr>
<td className="px-3 py-6 text-center text-zinc-500" colSpan={5}>
<td className="px-3 py-6 text-center text-zinc-500" colSpan={7}>
{tx("暂无用户数据", "No users found")}
</td>
</tr>
@@ -165,6 +327,9 @@ export default function AdminUsersPage() {
<p className="text-xs text-zinc-500">
{tx("创建时间:", "Created: ")}
{fmtTs(user.created_at)}
{" | "}
{tx("提交:", "Subs: ")}{user.total_submissions}
{" | AC: "}{user.total_ac}
</p>
<div className="flex items-center gap-2">
<span className="text-xs text-zinc-600">Rating</span>
@@ -186,7 +351,14 @@ export default function AdminUsersPage() {
>
{tx("保存", "Save")}
</button>
<button
className="rounded border px-2 py-1 text-xs"
onClick={() => setExpandedId(expandedId === user.id ? null : user.id)}
>
{expandedId === user.id ? tx("收起", "Hide") : tx("详情", "Detail")}
</button>
</div>
{expandedId === user.id && <DetailPanel userId={user.id} tx={tx} />}
</div>
))}
{!loading && items.length === 0 && (

查看文件

@@ -1,6 +1,6 @@
"use client";
import { useEffect, useMemo, useState } from "react";
import React, { useEffect, useMemo, useState } from "react";
import {
ArrowRightLeft,
Calendar,
@@ -135,6 +135,33 @@ export default function MePage() {
return name;
};
const formatRatingNote = (note: string, type: string): React.ReactNode => {
// Daily task codes
const taskLabels: Record<string, [string, string]> = {
login_checkin: ["每日签到 🎯", "Daily Sign-in 🎯"],
daily_submit: ["每日提交 📝", "Daily Submission 📝"],
first_ac: ["首次通过 ⭐", "First AC ⭐"],
code_quality: ["代码质量 🛠️", "Code Quality 🛠️"],
};
if (type === "daily_task" && taskLabels[note]) {
return isZh ? taskLabels[note][0] : taskLabels[note][1];
}
// Solution view: "Problem 1234:Title"
const m = note.match(/^Problem (\d+):(.*)$/);
if (m) {
const pid = m[1];
const title = m[2].trim();
return (
<a href={`/problems/${pid}`} className="hover:underline text-[color:var(--mc-diamond)]">
{isZh ? `查看题解 P${pid}` : `View Solution P${pid}`}
{title ? ` · ${title}` : ""}
</a>
);
}
// Redeem items keep original text
return note;
};
const loadAll = async () => {
setLoading(true);
setError("");
@@ -388,7 +415,7 @@ export default function MePage() {
{item.change > 0 ? <TrendingUp size={14} /> : <TrendingDown size={14} />}
{item.change > 0 ? `+${item.change}` : item.change}
</span>
<span className="ml-2">{item.note}</span>
<span className="ml-2">{formatRatingNote(item.note, item.type)}</span>
</span>
<span className="text-[color:var(--mc-stone-dark)]">
{new Date(item.created_at * 1000).toLocaleString()}

查看文件

@@ -301,6 +301,14 @@ export default function ProblemDetailPage() {
const [policyIssues, setPolicyIssues] = useState<Cpp14PolicyIssue[]>([]);
const [policyMsg, setPolicyMsg] = useState("");
const [noteText, setNoteText] = useState("" );
const [noteSaving, setNoteSaving] = useState(false);
const [noteScoring, setNoteScoring] = useState(false);
const [noteScore, setNoteScore] = useState<number | null>(null);
const [noteRating, setNoteRating] = useState<number | null>(null);
const [noteFeedback, setNoteFeedback] = useState("");
const [noteMsg, setNoteMsg] = useState("");
const [showSolutions, setShowSolutions] = useState(false);
const [expandedCodes, setExpandedCodes] = useState<Set<number>>(new Set());
const [unlockConfirm, setUnlockConfirm] = useState(false);
@@ -332,6 +340,57 @@ export default function ProblemDetailPage() {
if (!problem) return "";
return sanitizeStatementMarkdown(problem);
}, [problem]);
const saveLearningNote = async () => {
const token = readToken();
if (!token) {
setNoteMsg(tx("请先登录后再保存笔记。", "Please login to save notes."));
return;
}
if (!problemId) return;
setNoteSaving(true);
setNoteMsg("");
try {
await apiFetch<{ note: string }>(`/api/v1/me/wrong-book/${problemId}`, {
method: "PATCH",
body: JSON.stringify({ note: noteText }),
}, token);
setNoteMsg(tx("笔记已保存。", "Notes saved."));
} catch (e: unknown) {
setNoteMsg(String(e));
} finally {
setNoteSaving(false);
}
};
const scoreLearningNote = async () => {
const token = readToken();
if (!token) {
setNoteMsg(tx("请先登录后再评分。", "Please login to score notes."));
return;
}
if (!problemId) return;
setNoteScoring(true);
setNoteMsg("");
try {
const resp = await apiFetch<{
note_score: number;
note_rating: number;
note_feedback_md: string;
}>(`/api/v1/me/wrong-book/${problemId}/note-score`, {
method: "POST",
body: JSON.stringify({ note: noteText }),
}, token);
setNoteScore(resp.note_score);
setNoteRating(resp.note_rating);
setNoteFeedback(resp.note_feedback_md || "");
setNoteMsg(tx("评分完成。", "Scored."));
} catch (e: unknown) {
setNoteMsg(String(e));
} finally {
setNoteScoring(false);
}
};
const sampleInput = problem?.sample_input ?? "";
const problemId = problem?.id ?? 0;
const printableAnswerMarkdown = useMemo(
@@ -841,6 +900,43 @@ export default function ProblemDetailPage() {
</div>
)}
</div>
<div className="mt-5 rounded border-[3px] border-black bg-[color:var(--mc-plank)] p-3 shadow-[3px_3px_0_rgba(0,0,0,0.45)]">
<div className="flex items-center justify-between gap-2">
<h3 className="font-bold text-black">{tx("学习笔记(看完视频后上传/粘贴)", "Learning Notes (paste after watching)")}</h3>
<span className="text-xs text-zinc-700">{tx("满分100分 = rating 10分", "100 pts = rating 10")}</span>
</div>
<textarea
className="mt-2 w-full rounded-none border-[2px] border-black bg-[color:var(--mc-plank-light)] p-2 text-xs text-black shadow-[2px_2px_0_rgba(0,0,0,0.35)]"
rows={8}
value={noteText}
placeholder={tx("建议写:学习目标/关键概念/代码模板/踩坑与修复/总结", "Suggested: goals / key ideas / code template / pitfalls / summary")}
onChange={(e) => setNoteText(e.target.value)}
/>
<div className="mt-2 flex flex-wrap gap-2">
<button className="mc-btn mc-btn-success text-xs" onClick={() => void saveLearningNote()} disabled={noteSaving}>
{noteSaving ? tx("保存中...", "Saving...") : tx("保存笔记", "Save")}
</button>
<button className="mc-btn text-xs" onClick={() => void scoreLearningNote()} disabled={noteScoring}>
{noteScoring ? tx("评分中...", "Scoring...") : tx("笔记评分", "Score")}
</button>
{noteScore !== null && noteRating !== null && (
<span className="text-xs text-black self-center">
{tx("得分:", "Score: ")}{noteScore}/100 · {tx("评级:", "Rating: ")}{noteRating}/10
</span>
)}
</div>
{noteMsg && <p className="mt-2 text-xs text-[color:var(--mc-stone-dark)]">{noteMsg}</p>}
{noteFeedback && (
<div className="mt-3 border-t-2 border-dashed border-black pt-3">
<MarkdownRenderer markdown={noteFeedback} className="problem-markdown text-black" />
</div>
)}
</div>
</section>
<section className="flex flex-col gap-4 print:hidden">

查看文件

@@ -18,6 +18,7 @@ import {
} from "lucide-react";
import { apiFetch } from "@/lib/api";
import { readToken } from "@/lib/auth";
import { useI18nText } from "@/lib/i18n";
type Problem = {
@@ -27,6 +28,10 @@ type Problem = {
source: string;
llm_profile_json: string;
created_at: number;
local_submit_count?: number;
local_ac_count?: number;
user_ac?: boolean;
user_fail_count?: number;
};
type ProblemListResp = {
@@ -55,6 +60,13 @@ type Preset = {
};
const PRESETS: Preset[] = [
{
key: "cpp-basic",
labelZh: "C++基础",
labelEn: "C++ Basics",
sourcePrefix: "course:cpp-basic:",
tags: [],
},
{
key: "csp-beginner-default",
labelZh: "CSP J/S 入门预设",
@@ -95,6 +107,14 @@ const PRESETS: Preset[] = [
];
const QUICK_CARDS = [
{
presetKey: "cpp-basic",
titleZh: "C++基础",
titleEn: "C++ Basics",
descZh: "零基础入门任务",
descEn: "Zero-to-One Quests",
icon: Book,
},
{
presetKey: "csp-j",
titleZh: "CSP-J 试炼",
@@ -170,10 +190,10 @@ function resolvePid(problem: Problem, profile: ProblemProfile | null): string {
return /^[A-Za-z]\d+$/.test(head) ? head : String(problem.id);
}
function resolvePassRate(profile: ProblemProfile | null): string {
const accepted = profile?.stats?.total_accepted;
const submitted = profile?.stats?.total_submit;
if (!submitted || submitted <= 0 || accepted === undefined) return "-";
function resolvePassRate(problem: Problem): string {
const submitted = problem.local_submit_count ?? 0;
const accepted = problem.local_ac_count ?? 0;
if (submitted <= 0) return "-";
const rate = ((accepted / submitted) * 100).toFixed(1);
return `${accepted}/${submitted} (${rate}%)`;
}
@@ -224,7 +244,7 @@ export default function ProblemsPage() {
if (preset.sourcePrefix) params.set("source_prefix", preset.sourcePrefix);
if (preset.tags && preset.tags.length > 0) params.set("tags", preset.tags.join(","));
const data = await apiFetch<ProblemListResp>(`/api/v1/problems?${params.toString()}`);
const data = await apiFetch<ProblemListResp>(`/api/v1/problems?${params.toString()}`, undefined, readToken() ?? undefined);
setItems(data.items ?? []);
setTotalCount(data.total_count ?? 0);
} catch (e: unknown) {
@@ -279,7 +299,7 @@ export default function ProblemsPage() {
</div>
</div>
<section className="mt-4 grid gap-3 md:grid-cols-3">
<section className="mt-4 grid gap-3 md:grid-cols-4">
{QUICK_CARDS.map((card) => {
const active = presetKey === card.presetKey;
return (
@@ -394,7 +414,16 @@ export default function ProblemsPage() {
{difficultyIcon(problem.difficulty)} T{problem.difficulty}
</span>
</div>
<p className="text-xs text-[color:var(--mc-stone)]">{tx("完成率:", "Clear Rate: ")}{resolvePassRate(profile)}</p>
<p className="text-xs text-[color:var(--mc-stone)]">{tx("完成率:", "Clear Rate: ")}{resolvePassRate(problem)}</p>
{problem.user_ac !== undefined && (
<p className="text-xs">
{problem.user_ac
? <span className="text-[color:var(--mc-green)] font-bold"> AC</span>
: problem.user_fail_count && problem.user_fail_count > 0
? <span className="text-[color:var(--mc-red)]"> ×{problem.user_fail_count}</span>
: <span className="text-[color:var(--mc-stone-dark)]">-</span>}
</p>
)}
<div className="flex flex-wrap gap-1">
{tags.length === 0 && <span className="text-xs text-[color:var(--mc-stone-dark)]">-</span>}
{tags.map((tag) => (
@@ -423,6 +452,7 @@ export default function ProblemsPage() {
<th className="px-3 py-2">{tx("编号", "ID")}</th>
<th className="px-3 py-2">{tx("任务标题", "Quest Title")}</th>
<th className="px-3 py-2">{tx("完成率", "Clear Rate")}</th>
<th className="px-3 py-2">{tx("状态", "Status")}</th>
<th className="px-3 py-2">{tx("难度", "Tier")}</th>
<th className="px-3 py-2">{tx("标签", "Tags")}</th>
<th className="px-3 py-2">{tx("来源", "Source")}</th>
@@ -440,7 +470,18 @@ export default function ProblemsPage() {
{problem.title}
</Link>
</td>
<td className="px-3 py-2 text-[color:var(--mc-stone)]">{resolvePassRate(profile)}</td>
<td className="px-3 py-2 text-[color:var(--mc-stone)]">{resolvePassRate(problem)}</td>
<td className="px-3 py-2">
{problem.user_ac !== undefined ? (
problem.user_ac
? <span className="text-[color:var(--mc-green)] font-bold"> AC</span>
: problem.user_fail_count && problem.user_fail_count > 0
? <span className="text-[color:var(--mc-red)]"> ×{problem.user_fail_count}</span>
: <span className="text-[color:var(--mc-stone-dark)]">-</span>
) : (
<span className="text-[color:var(--mc-stone-dark)]">-</span>
)}
</td>
<td className={`px-3 py-2 font-bold ${difficultyClass(problem.difficulty)}`}>
{difficultyIcon(problem.difficulty)} {problem.difficulty}
</td>
@@ -464,7 +505,7 @@ export default function ProblemsPage() {
})}
{!loading && rows.length === 0 && (
<tr>
<td className="px-3 py-6 text-center text-[color:var(--mc-stone)]" colSpan={6}>
<td className="px-3 py-6 text-center text-[color:var(--mc-stone)]" colSpan={7}>
{tx(
"没有找到任务。请尝试其他频道或刷新地图。",
"No quests found. Try different channel or reload map."