原来只有 `apps/web` 在 Prettier 下(配置在 `apps/web/.prettierrc.toml`、脚本在 web 的 package.json),后端和契约从来没格式化过 —— 手写在 100 列上下,`db/schema.ts` 还是 drizzle-kit pull 留下的 tab 缩进。两套口径分叉久了,跨端改一处就得记着「这边 什么风格」。 - 配置搬到根目录 `.prettierrc.toml`,内容不变(`semi=false`,其余全默认, printWidth 80 —— 和前端已有的格式一致,不另立一套宽度); - 脚本统一成根目录 `bun run fmt`,覆盖 `apps/*/src`、`apps/web/tests` 和两个构建 配置;web 自己那份 `fmt` 和重复的 prettier 依赖删掉; - `.prettierignore` 挡掉两类不该碰的:drizzle-kit 生成的 `src/db/meta/` 结构快照 (它是 db:generate 的比对输入,只该由 drizzle-kit 写)、unplugin 每次 dev 都会 重写的 `auto-imports.d.ts` / `components.d.ts`; - 全量跑了一遍。纯格式,无行为改动:api typecheck / check:routes / check:ast、 前端 type-check 全过,起 api 打了接口确认正常。前端这 39 个文件的小改动是 prettier 版本漂移(类型断言的换行口径变了),不是新配置带来的。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e600fd24cf
commit
ed56a209ea
122 files changed
+10557
-4843
No files matched your search
+364
-157
@@ -10,7 +10,17 @@ import {
|
||||
type FlowchartStatistics,
|
||||
type FlowchartSubmission,
|
||||
} from "@oj2/contract"
|
||||
import { and, asc, count, desc, eq, inArray, isNull, sql, type SQL } from "drizzle-orm"
|
||||
import {
|
||||
and,
|
||||
asc,
|
||||
count,
|
||||
desc,
|
||||
eq,
|
||||
inArray,
|
||||
isNull,
|
||||
sql,
|
||||
type SQL,
|
||||
} from "drizzle-orm"
|
||||
import { Hono } from "hono"
|
||||
|
||||
import { requireAuth, requireTeacher, type AppEnv } from "../auth/middleware"
|
||||
@@ -38,8 +48,16 @@ function flowchartThrottleKey(userId: number) {
|
||||
return `flowchart:${userId}`
|
||||
}
|
||||
|
||||
function canView(user: import("../auth/session").AuthUser, row: { userId: number }, problem: { createdById: number }) {
|
||||
return row.userId === user.id || isAdminRole(user) || problem.createdById === user.id
|
||||
function canView(
|
||||
user: import("../auth/session").AuthUser,
|
||||
row: { userId: number },
|
||||
problem: { createdById: number },
|
||||
) {
|
||||
return (
|
||||
row.userId === user.id ||
|
||||
isAdminRole(user) ||
|
||||
problem.createdById === user.id
|
||||
)
|
||||
}
|
||||
|
||||
function flowchartData(
|
||||
@@ -67,20 +85,48 @@ function flowchartData(
|
||||
}
|
||||
|
||||
flowchartRoutes.post("/flowcharts", requireAuth, async (c) => {
|
||||
const parsed = createFlowchartRequestSchema.safeParse(await c.req.json().catch(() => null))
|
||||
if (!parsed.success || JSON.stringify(parsed.data?.flowchartData ?? {}).length > 500 * 1024) {
|
||||
return failure(c, 400, "invalid-request", parsed.error?.issues[0]?.message ?? "Flowchart data is too large")
|
||||
const parsed = createFlowchartRequestSchema.safeParse(
|
||||
await c.req.json().catch(() => null),
|
||||
)
|
||||
if (
|
||||
!parsed.success ||
|
||||
JSON.stringify(parsed.data?.flowchartData ?? {}).length > 500 * 1024
|
||||
) {
|
||||
return failure(
|
||||
c,
|
||||
400,
|
||||
"invalid-request",
|
||||
parsed.error?.issues[0]?.message ?? "Flowchart data is too large",
|
||||
)
|
||||
}
|
||||
const [problem] = await db.select({ id: schema.problem.id, allow: schema.problem.allowFlowchart }).from(schema.problem)
|
||||
.where(eq(schema.problem.id, parsed.data.problemId)).limit(1)
|
||||
if (!problem) return failure(c, 404, "problem-not-found", "Problem does not exist")
|
||||
if (!problem.allow) return failure(c, 400, "flowchart-not-allowed", "This problem does not allow flowchart submission")
|
||||
const [problem] = await db
|
||||
.select({ id: schema.problem.id, allow: schema.problem.allowFlowchart })
|
||||
.from(schema.problem)
|
||||
.where(eq(schema.problem.id, parsed.data.problemId))
|
||||
.limit(1)
|
||||
if (!problem)
|
||||
return failure(c, 404, "problem-not-found", "Problem does not exist")
|
||||
if (!problem.allow)
|
||||
return failure(
|
||||
c,
|
||||
400,
|
||||
"flowchart-not-allowed",
|
||||
"This problem does not allow flowchart submission",
|
||||
)
|
||||
// 限流:每次提交都会触发一次外部 AI 调用,是和判题沙箱同级的有限资源。
|
||||
// 身份前缀单独开一个桶,**不能**直接用 user id —— 那是代码提交在用的桶,
|
||||
// 共用的话学生在机房连着交几次代码,流程图这边就会莫名其妙交不上去。
|
||||
const throttle = await consumeToken("user", flowchartThrottleKey(c.get("user")!.id))
|
||||
const throttle = await consumeToken(
|
||||
"user",
|
||||
flowchartThrottleKey(c.get("user")!.id),
|
||||
)
|
||||
if (!throttle.allowed) {
|
||||
return failure(c, 429, "too-many-submissions", `Please wait ${Math.floor(throttle.wait)} seconds`)
|
||||
return failure(
|
||||
c,
|
||||
429,
|
||||
"too-many-submissions",
|
||||
`Please wait ${Math.floor(throttle.wait)} seconds`,
|
||||
)
|
||||
}
|
||||
const id = randomBytes(16).toString("hex")
|
||||
await db.insert(schema.flowchartSubmission).values({
|
||||
@@ -104,10 +150,22 @@ flowchartRoutes.post("/flowcharts", requireAuth, async (c) => {
|
||||
try {
|
||||
await flowchartQueue.add("evaluate", { submissionId: id }, { jobId: id })
|
||||
} catch (error) {
|
||||
await db.update(schema.flowchartSubmission).set({ status: 3 }).where(eq(schema.flowchartSubmission.id, id))
|
||||
return failure(c, 502, "queue-unavailable", "Evaluation queue is unavailable")
|
||||
await db
|
||||
.update(schema.flowchartSubmission)
|
||||
.set({ status: 3 })
|
||||
.where(eq(schema.flowchartSubmission.id, id))
|
||||
return failure(
|
||||
c,
|
||||
502,
|
||||
"queue-unavailable",
|
||||
"Evaluation queue is unavailable",
|
||||
)
|
||||
}
|
||||
return success(c, { submissionId: id, status: "pending" } satisfies CreateFlowchartResponse, 201)
|
||||
return success(
|
||||
c,
|
||||
{ submissionId: id, status: "pending" } satisfies CreateFlowchartResponse,
|
||||
201,
|
||||
)
|
||||
})
|
||||
|
||||
/**
|
||||
@@ -128,20 +186,27 @@ async function flowchartProblemFilter(displayId: string) {
|
||||
const problems = await db
|
||||
.select({ id: schema.problem.id })
|
||||
.from(schema.problem)
|
||||
.where(and(
|
||||
sql`lower(${schema.problem.displayId}) = lower(${displayId})`,
|
||||
// 流程图题都是公开题(快照里那 12 道 contest_id 全为空),
|
||||
// 比赛题的 _id 撞号是常态,不该被筛进来
|
||||
isNull(schema.problem.contestId),
|
||||
))
|
||||
.where(
|
||||
and(
|
||||
sql`lower(${schema.problem.displayId}) = lower(${displayId})`,
|
||||
// 流程图题都是公开题(快照里那 12 道 contest_id 全为空),
|
||||
// 比赛题的 _id 撞号是常态,不该被筛进来
|
||||
isNull(schema.problem.contestId),
|
||||
),
|
||||
)
|
||||
return problems.length
|
||||
? inArray(schema.flowchartSubmission.problemId, problems.map((row) => row.id))
|
||||
? inArray(
|
||||
schema.flowchartSubmission.problemId,
|
||||
problems.map((row) => row.id),
|
||||
)
|
||||
: sql`false`
|
||||
}
|
||||
|
||||
async function flowchartUserFilter(username: string) {
|
||||
const ids = (await matchedUsers(username)).map((row) => row.id)
|
||||
return ids.length ? inArray(schema.flowchartSubmission.userId, ids) : sql`false`
|
||||
return ids.length
|
||||
? inArray(schema.flowchartSubmission.userId, ids)
|
||||
: sql`false`
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,46 +252,69 @@ flowchartRoutes.get("/flowcharts", requireAuth, async (c) => {
|
||||
// 与代码提交列表同一套口径(submission.ts 的 GET /submissions):关掉
|
||||
// submission_list_show_all 时非管理员看不到列表。流程图这边一直漏了这道门,
|
||||
// 学生把语言切成「流程图」、用户名随便填一个字就能翻出全班的 AI 评分。
|
||||
if (!(await getBooleanOption("submission_list_show_all", true)) && !isAdminRole(user)) {
|
||||
if (
|
||||
!(await getBooleanOption("submission_list_show_all", true)) &&
|
||||
!isAdminRole(user)
|
||||
) {
|
||||
return success(c, { results: [], total: 0 } satisfies FlowchartList)
|
||||
}
|
||||
// 「只看自己」盖过用户名;普通学生不填用户名时也只看自己
|
||||
const onlyMyself = c.req.query("myself") === "1" || (!username && user.adminType === "Regular User")
|
||||
const onlyMyself =
|
||||
c.req.query("myself") === "1" ||
|
||||
(!username && user.adminType === "Regular User")
|
||||
const filters: Array<SQL | undefined> = []
|
||||
filters.push(...await Promise.all([
|
||||
displayId ? flowchartProblemFilter(displayId) : undefined,
|
||||
!onlyMyself && username ? flowchartUserFilter(username) : undefined,
|
||||
]))
|
||||
filters.push(
|
||||
...(await Promise.all([
|
||||
displayId ? flowchartProblemFilter(displayId) : undefined,
|
||||
!onlyMyself && username ? flowchartUserFilter(username) : undefined,
|
||||
])),
|
||||
)
|
||||
if (onlyMyself) filters.push(eq(schema.flowchartSubmission.userId, user.id))
|
||||
if (c.req.query("today") === "1") filters.push(sql`${schema.flowchartSubmission.createTime} >= ${todayStart()}`)
|
||||
if (["S", "A", "B", "C"].includes(grade ?? "")) filters.push(eq(schema.flowchartSubmission.aiGrade, grade!))
|
||||
if (c.req.query("today") === "1")
|
||||
filters.push(
|
||||
sql`${schema.flowchartSubmission.createTime} >= ${todayStart()}`,
|
||||
)
|
||||
if (["S", "A", "B", "C"].includes(grade ?? ""))
|
||||
filters.push(eq(schema.flowchartSubmission.aiGrade, grade!))
|
||||
const where = and(...filters)
|
||||
const [totalRows, rows] = await Promise.all([
|
||||
// 筛条件已经全落在 flowchart_submission 自己的列上,count 不挂任何 join
|
||||
db.select({ value: count() }).from(schema.flowchartSubmission).where(where),
|
||||
db.select(flowchartListColumns)
|
||||
db
|
||||
.select(flowchartListColumns)
|
||||
.from(schema.flowchartSubmission)
|
||||
.innerJoin(schema.user, eq(schema.flowchartSubmission.userId, schema.user.id))
|
||||
.innerJoin(schema.problem, eq(schema.flowchartSubmission.problemId, schema.problem.id))
|
||||
.innerJoin(
|
||||
schema.user,
|
||||
eq(schema.flowchartSubmission.userId, schema.user.id),
|
||||
)
|
||||
.innerJoin(
|
||||
schema.problem,
|
||||
eq(schema.flowchartSubmission.problemId, schema.problem.id),
|
||||
)
|
||||
.where(where)
|
||||
.orderBy(desc(schema.flowchartSubmission.createTime)).limit(limit).offset(offset),
|
||||
.orderBy(desc(schema.flowchartSubmission.createTime))
|
||||
.limit(limit)
|
||||
.offset(offset),
|
||||
])
|
||||
return success(c, {
|
||||
results: rows.map(({ flowchart, username, problem }) => ({
|
||||
id: flowchart.id,
|
||||
username,
|
||||
problem: problem.displayId,
|
||||
problemTitle: problem.title,
|
||||
status: flowchart.status,
|
||||
createTime: flowchart.createTime,
|
||||
aiScore: flowchart.aiScore,
|
||||
aiGrade: flowchart.aiGrade,
|
||||
aiProvider: flowchart.aiProvider,
|
||||
aiModel: flowchart.aiModel,
|
||||
processingTime: flowchart.processingTime,
|
||||
evaluationTime: flowchart.evaluationTime,
|
||||
showLink: canView(user, flowchart, problem),
|
||||
} satisfies FlowchartListItem)),
|
||||
results: rows.map(
|
||||
({ flowchart, username, problem }) =>
|
||||
({
|
||||
id: flowchart.id,
|
||||
username,
|
||||
problem: problem.displayId,
|
||||
problemTitle: problem.title,
|
||||
status: flowchart.status,
|
||||
createTime: flowchart.createTime,
|
||||
aiScore: flowchart.aiScore,
|
||||
aiGrade: flowchart.aiGrade,
|
||||
aiProvider: flowchart.aiProvider,
|
||||
aiModel: flowchart.aiModel,
|
||||
processingTime: flowchart.processingTime,
|
||||
evaluationTime: flowchart.evaluationTime,
|
||||
showLink: canView(user, flowchart, problem),
|
||||
}) satisfies FlowchartListItem,
|
||||
),
|
||||
total: totalRows[0]?.value ?? 0,
|
||||
} satisfies FlowchartList)
|
||||
})
|
||||
@@ -262,20 +350,24 @@ flowchartRoutes.get("/flowcharts/statistics", requireTeacher, async (c) => {
|
||||
eq(schema.flowchartSubmission.status, FLOWCHART_COMPLETED),
|
||||
sql`${schema.flowchartSubmission.createTime} <= ${end}`,
|
||||
]
|
||||
if (start) filters.push(sql`${schema.flowchartSubmission.createTime} >= ${start}`)
|
||||
if (start)
|
||||
filters.push(sql`${schema.flowchartSubmission.createTime} >= ${start}`)
|
||||
|
||||
const displayId = c.req.query("problemId")?.trim()
|
||||
if (displayId) {
|
||||
const [problem] = await db
|
||||
.select({ id: schema.problem.id })
|
||||
.from(schema.problem)
|
||||
.where(and(
|
||||
sql`lower(${schema.problem.displayId}) = lower(${displayId})`,
|
||||
isNull(schema.problem.contestId),
|
||||
eq(schema.problem.visible, true),
|
||||
))
|
||||
.where(
|
||||
and(
|
||||
sql`lower(${schema.problem.displayId}) = lower(${displayId})`,
|
||||
isNull(schema.problem.contestId),
|
||||
eq(schema.problem.visible, true),
|
||||
),
|
||||
)
|
||||
.limit(1)
|
||||
if (!problem) return failure(c, 404, "problem-not-found", "Problem does not exist")
|
||||
if (!problem)
|
||||
return failure(c, 404, "problem-not-found", "Problem does not exist")
|
||||
filters.push(eq(schema.flowchartSubmission.problemId, problem.id))
|
||||
}
|
||||
|
||||
@@ -286,13 +378,17 @@ flowchartRoutes.get("/flowcharts/statistics", requireTeacher, async (c) => {
|
||||
if (username) {
|
||||
const ids = matched.map((row) => row.id)
|
||||
// 一个账号都没匹配上时得留个恒假条件,否则「查无此班」变成「全站统计」
|
||||
filters.push(ids.length ? inArray(schema.flowchartSubmission.userId, ids) : sql`false`)
|
||||
filters.push(
|
||||
ids.length ? inArray(schema.flowchartSubmission.userId, ids) : sql`false`,
|
||||
)
|
||||
}
|
||||
const where = and(...filters)
|
||||
// 花名册:只有指定了用户名才谈得上「班级人数」,不指定时分母无意义。
|
||||
// 未禁用的普通用户才进分母,教师和管理员不算
|
||||
const roster = username
|
||||
? matched.filter((row) => !row.isDisabled && row.adminType === "Regular User")
|
||||
? matched.filter(
|
||||
(row) => !row.isDisabled && row.adminType === "Regular User",
|
||||
)
|
||||
: []
|
||||
|
||||
/**
|
||||
@@ -304,46 +400,56 @@ flowchartRoutes.get("/flowcharts/statistics", requireTeacher, async (c) => {
|
||||
* (criteria 255 + suggestions 64 + feedback 47),现在 2134 条无感,5 万条就是一次
|
||||
* 点击 18MB,而老师是开着面板反复切时段、切班的。
|
||||
*/
|
||||
const [[totals], gradeRows, criteriaRows, textRows, submittedRows] = await Promise.all([
|
||||
db
|
||||
.select({
|
||||
total: count(),
|
||||
/**
|
||||
* 均分拆成 sum / count 两项,不直接用 `avg()`:分母是**有分数的条数**而不是
|
||||
* 总条数(对齐 Django 的 Avg(),它跳过 NULL),拆开之后这个口径在代码里是
|
||||
* 写明的,也省掉 avg() 在空集上回 NULL 还要兜底。
|
||||
*/
|
||||
scoreSum: sql<number>`coalesce(sum(${schema.flowchartSubmission.aiScore}), 0)`.mapWith(Number),
|
||||
scoreCount: sql<number>`count(${schema.flowchartSubmission.aiScore})::int`.mapWith(Number),
|
||||
// 完成人数。user_id 和 username 一一对应,按哪个 distinct 都一样,
|
||||
// 按 user_id 就不必 join user
|
||||
completedCount: sql<number>`count(distinct ${schema.flowchartSubmission.userId})::int`.mapWith(Number),
|
||||
})
|
||||
.from(schema.flowchartSubmission)
|
||||
.where(where),
|
||||
db
|
||||
.select({ grade: schema.flowchartSubmission.aiGrade, n: count() })
|
||||
.from(schema.flowchartSubmission)
|
||||
.where(where)
|
||||
.groupBy(schema.flowchartSubmission.aiGrade),
|
||||
/**
|
||||
* 各项**平均分**。`ai_criteria_details` 是 `{ 项名: { score, max, comment } }`,
|
||||
* 用 jsonb_each 展开之后按项名分组。分数不是数字的项整项跳过,和原来 JS 那句
|
||||
* `typeof detail.score !== "number"` 的 continue 一致。
|
||||
*
|
||||
* **那道 `jsonb_typeof(...) = 'object'` 的闸不能省,而且要写在 jsonb_each 的参数里。**
|
||||
* 不能省:撞上标量(历史脏数据)jsonb_each 直接抛错,整个面板 500 ——
|
||||
* 拿 `'5'::jsonb` 和 `'[1,2]'::jsonb` 各插一行验过。
|
||||
*
|
||||
* 写在哪儿则纯是规划器的脸色:挪进 where 当基表过滤条件时,53350 行的探针上
|
||||
* 实测 180ms → 360ms,因为计划从「并行 Partial HashAggregate」换成了「串行
|
||||
* GroupAggregate + 21 万行外部归并排序、落盘 26MB」。两种写法都正确,选快的那个。
|
||||
*
|
||||
* 每项的**满分**不在这里取,见下面 criteriaMax 的注释:在这条 SQL 里按
|
||||
* create_time 取「最新那条」要给 21 万行(4 项 × 5 万条)排序,同一个探针上
|
||||
* 实测 254ms → 842ms,而满分本来就是几个常数。
|
||||
*/
|
||||
db.execute<{ key: string; avg: number }>(sql`
|
||||
const [[totals], gradeRows, criteriaRows, textRows, submittedRows] =
|
||||
await Promise.all([
|
||||
db
|
||||
.select({
|
||||
total: count(),
|
||||
/**
|
||||
* 均分拆成 sum / count 两项,不直接用 `avg()`:分母是**有分数的条数**而不是
|
||||
* 总条数(对齐 Django 的 Avg(),它跳过 NULL),拆开之后这个口径在代码里是
|
||||
* 写明的,也省掉 avg() 在空集上回 NULL 还要兜底。
|
||||
*/
|
||||
scoreSum:
|
||||
sql<number>`coalesce(sum(${schema.flowchartSubmission.aiScore}), 0)`.mapWith(
|
||||
Number,
|
||||
),
|
||||
scoreCount:
|
||||
sql<number>`count(${schema.flowchartSubmission.aiScore})::int`.mapWith(
|
||||
Number,
|
||||
),
|
||||
// 完成人数。user_id 和 username 一一对应,按哪个 distinct 都一样,
|
||||
// 按 user_id 就不必 join user
|
||||
completedCount:
|
||||
sql<number>`count(distinct ${schema.flowchartSubmission.userId})::int`.mapWith(
|
||||
Number,
|
||||
),
|
||||
})
|
||||
.from(schema.flowchartSubmission)
|
||||
.where(where),
|
||||
db
|
||||
.select({ grade: schema.flowchartSubmission.aiGrade, n: count() })
|
||||
.from(schema.flowchartSubmission)
|
||||
.where(where)
|
||||
.groupBy(schema.flowchartSubmission.aiGrade),
|
||||
/**
|
||||
* 各项**平均分**。`ai_criteria_details` 是 `{ 项名: { score, max, comment } }`,
|
||||
* 用 jsonb_each 展开之后按项名分组。分数不是数字的项整项跳过,和原来 JS 那句
|
||||
* `typeof detail.score !== "number"` 的 continue 一致。
|
||||
*
|
||||
* **那道 `jsonb_typeof(...) = 'object'` 的闸不能省,而且要写在 jsonb_each 的参数里。**
|
||||
* 不能省:撞上标量(历史脏数据)jsonb_each 直接抛错,整个面板 500 ——
|
||||
* 拿 `'5'::jsonb` 和 `'[1,2]'::jsonb` 各插一行验过。
|
||||
*
|
||||
* 写在哪儿则纯是规划器的脸色:挪进 where 当基表过滤条件时,53350 行的探针上
|
||||
* 实测 180ms → 360ms,因为计划从「并行 Partial HashAggregate」换成了「串行
|
||||
* GroupAggregate + 21 万行外部归并排序、落盘 26MB」。两种写法都正确,选快的那个。
|
||||
*
|
||||
* 每项的**满分**不在这里取,见下面 criteriaMax 的注释:在这条 SQL 里按
|
||||
* create_time 取「最新那条」要给 21 万行(4 项 × 5 万条)排序,同一个探针上
|
||||
* 实测 254ms → 842ms,而满分本来就是几个常数。
|
||||
*/
|
||||
db.execute<{ key: string; avg: number }>(sql`
|
||||
select e.key as key, avg((e.value->>'score')::double precision) as avg
|
||||
from ${schema.flowchartSubmission}
|
||||
cross join lateral jsonb_each(
|
||||
@@ -354,25 +460,25 @@ flowchartRoutes.get("/flowcharts/statistics", requireTeacher, async (c) => {
|
||||
where ${where} and jsonb_typeof(e.value->'score') = 'number'
|
||||
group by e.key
|
||||
`),
|
||||
// 词云的原料。只有这条要读大列,所以只有它按时间倒序取最近的 N 条
|
||||
db
|
||||
.select({
|
||||
criteria: schema.flowchartSubmission.aiCriteriaDetails,
|
||||
feedback: schema.flowchartSubmission.aiFeedback,
|
||||
suggestions: schema.flowchartSubmission.aiSuggestions,
|
||||
})
|
||||
.from(schema.flowchartSubmission)
|
||||
.where(where)
|
||||
.orderBy(desc(schema.flowchartSubmission.createTime))
|
||||
.limit(WORDCLOUD_TEXT_LIMIT),
|
||||
// 「谁没做」只在有花名册时算得出来,行数也就一个班
|
||||
roster.length
|
||||
? db
|
||||
.selectDistinct({ userId: schema.flowchartSubmission.userId })
|
||||
.from(schema.flowchartSubmission)
|
||||
.where(where)
|
||||
: [],
|
||||
])
|
||||
// 词云的原料。只有这条要读大列,所以只有它按时间倒序取最近的 N 条
|
||||
db
|
||||
.select({
|
||||
criteria: schema.flowchartSubmission.aiCriteriaDetails,
|
||||
feedback: schema.flowchartSubmission.aiFeedback,
|
||||
suggestions: schema.flowchartSubmission.aiSuggestions,
|
||||
})
|
||||
.from(schema.flowchartSubmission)
|
||||
.where(where)
|
||||
.orderBy(desc(schema.flowchartSubmission.createTime))
|
||||
.limit(WORDCLOUD_TEXT_LIMIT),
|
||||
// 「谁没做」只在有花名册时算得出来,行数也就一个班
|
||||
roster.length
|
||||
? db
|
||||
.selectDistinct({ userId: schema.flowchartSubmission.userId })
|
||||
.from(schema.flowchartSubmission)
|
||||
.where(where)
|
||||
: [],
|
||||
])
|
||||
|
||||
if (!totals || totals.total === 0) {
|
||||
return success(c, {
|
||||
@@ -423,7 +529,8 @@ flowchartRoutes.get("/flowcharts/statistics", requireTeacher, async (c) => {
|
||||
if (!criteriaMax.has(key)) {
|
||||
criteriaMax.set(key, typeof detail.max === "number" ? detail.max : 100)
|
||||
}
|
||||
if (typeof detail.comment === "string" && detail.comment) pushText(detail.comment)
|
||||
if (typeof detail.comment === "string" && detail.comment)
|
||||
pushText(detail.comment)
|
||||
}
|
||||
if (row.feedback) pushText(row.feedback)
|
||||
if (row.suggestions) pushText(row.suggestions)
|
||||
@@ -431,13 +538,18 @@ flowchartRoutes.get("/flowcharts/statistics", requireTeacher, async (c) => {
|
||||
|
||||
const criteriaAverages: Record<string, { avg: number; max: number }> = {}
|
||||
for (const row of criteriaRows) {
|
||||
criteriaAverages[row.key] = { avg: rounded(row.avg, 1), max: criteriaMax.get(row.key) ?? 100 }
|
||||
criteriaAverages[row.key] = {
|
||||
avg: rounded(row.avg, 1),
|
||||
max: criteriaMax.get(row.key) ?? 100,
|
||||
}
|
||||
}
|
||||
|
||||
const submitted = new Set(submittedRows.map((row) => row.userId))
|
||||
return success(c, {
|
||||
totalCount: totals.total,
|
||||
avgScore: totals.scoreCount ? rounded(totals.scoreSum / totals.scoreCount, 1) : 0,
|
||||
avgScore: totals.scoreCount
|
||||
? rounded(totals.scoreSum / totals.scoreCount, 1)
|
||||
: 0,
|
||||
gradeDistribution,
|
||||
criteriaAverages,
|
||||
personCount: roster.length,
|
||||
@@ -453,33 +565,74 @@ flowchartRoutes.get("/flowcharts/statistics", requireTeacher, async (c) => {
|
||||
})
|
||||
|
||||
flowchartRoutes.get("/flowcharts/:id", requireAuth, async (c) => {
|
||||
const [row] = await db.select({ flowchart: schema.flowchartSubmission, username: schema.user.username, problem: schema.problem })
|
||||
.from(schema.flowchartSubmission).innerJoin(schema.user, eq(schema.flowchartSubmission.userId, schema.user.id))
|
||||
.innerJoin(schema.problem, eq(schema.flowchartSubmission.problemId, schema.problem.id))
|
||||
.where(eq(schema.flowchartSubmission.id, c.req.param("id"))).limit(1)
|
||||
if (!row || !canView(c.get("user")!, row.flowchart, row.problem)) return failure(c, 404, "flowchart-not-found", "Submission does not exist")
|
||||
const [row] = await db
|
||||
.select({
|
||||
flowchart: schema.flowchartSubmission,
|
||||
username: schema.user.username,
|
||||
problem: schema.problem,
|
||||
})
|
||||
.from(schema.flowchartSubmission)
|
||||
.innerJoin(
|
||||
schema.user,
|
||||
eq(schema.flowchartSubmission.userId, schema.user.id),
|
||||
)
|
||||
.innerJoin(
|
||||
schema.problem,
|
||||
eq(schema.flowchartSubmission.problemId, schema.problem.id),
|
||||
)
|
||||
.where(eq(schema.flowchartSubmission.id, c.req.param("id")))
|
||||
.limit(1)
|
||||
if (!row || !canView(c.get("user")!, row.flowchart, row.problem))
|
||||
return failure(c, 404, "flowchart-not-found", "Submission does not exist")
|
||||
return success(c, flowchartData(row.flowchart, row.username))
|
||||
})
|
||||
|
||||
flowchartRoutes.post("/flowcharts/:id/retry", requireAuth, async (c) => {
|
||||
const user = c.get("user")!
|
||||
const [row] = await db.select({ flowchart: schema.flowchartSubmission, problem: schema.problem }).from(schema.flowchartSubmission)
|
||||
.innerJoin(schema.problem, eq(schema.flowchartSubmission.problemId, schema.problem.id))
|
||||
.where(eq(schema.flowchartSubmission.id, c.req.param("id"))).limit(1)
|
||||
if (!row || !canView(user, row.flowchart, row.problem)) return failure(c, 404, "flowchart-not-found", "Submission does not exist")
|
||||
if (![2, 3].includes(row.flowchart.status)) return failure(c, 409, "retry-not-allowed", "Submission is not in a state that allows retry")
|
||||
const [row] = await db
|
||||
.select({ flowchart: schema.flowchartSubmission, problem: schema.problem })
|
||||
.from(schema.flowchartSubmission)
|
||||
.innerJoin(
|
||||
schema.problem,
|
||||
eq(schema.flowchartSubmission.problemId, schema.problem.id),
|
||||
)
|
||||
.where(eq(schema.flowchartSubmission.id, c.req.param("id")))
|
||||
.limit(1)
|
||||
if (!row || !canView(user, row.flowchart, row.problem))
|
||||
return failure(c, 404, "flowchart-not-found", "Submission does not exist")
|
||||
if (![2, 3].includes(row.flowchart.status))
|
||||
return failure(
|
||||
c,
|
||||
409,
|
||||
"retry-not-allowed",
|
||||
"Submission is not in a state that allows retry",
|
||||
)
|
||||
// canView 允许本人重试自己的提交,不限流的话学生可以反复点着刷 AI 调用。
|
||||
// 教师放行:重新判题是他们的日常操作,成批点几十行是正常用法
|
||||
if (!isAdminRole(user)) {
|
||||
const throttle = await consumeToken("user", flowchartThrottleKey(user.id))
|
||||
if (!throttle.allowed) {
|
||||
return failure(c, 429, "too-many-submissions", `Please wait ${Math.floor(throttle.wait)} seconds`)
|
||||
return failure(
|
||||
c,
|
||||
429,
|
||||
"too-many-submissions",
|
||||
`Please wait ${Math.floor(throttle.wait)} seconds`,
|
||||
)
|
||||
}
|
||||
}
|
||||
await db.update(schema.flowchartSubmission).set({
|
||||
status: 0, aiScore: null, aiGrade: null, aiFeedback: null, aiSuggestions: null,
|
||||
aiCriteriaDetails: {}, processingTime: null, evaluationTime: null,
|
||||
}).where(eq(schema.flowchartSubmission.id, row.flowchart.id))
|
||||
await db
|
||||
.update(schema.flowchartSubmission)
|
||||
.set({
|
||||
status: 0,
|
||||
aiScore: null,
|
||||
aiGrade: null,
|
||||
aiFeedback: null,
|
||||
aiSuggestions: null,
|
||||
aiCriteriaDetails: {},
|
||||
processingTime: null,
|
||||
evaluationTime: null,
|
||||
})
|
||||
.where(eq(schema.flowchartSubmission.id, row.flowchart.id))
|
||||
try {
|
||||
// jobId 必须**正好三段**:bullmq 对含 `:` 的自定义 id 有一条兼容老的可重复
|
||||
// 任务的校验(job.js 的 `split(':').length !== 3`),两段会直接抛
|
||||
@@ -493,28 +646,82 @@ flowchartRoutes.post("/flowcharts/:id/retry", requireAuth, async (c) => {
|
||||
)
|
||||
} catch (error) {
|
||||
// 入队失败就落 FAILED,别把提交丢在 PENDING 上 —— 和 POST /flowcharts 同一处理
|
||||
await db.update(schema.flowchartSubmission).set({ status: 3 }).where(eq(schema.flowchartSubmission.id, row.flowchart.id))
|
||||
return failure(c, 502, "queue-unavailable", "Evaluation queue is unavailable")
|
||||
await db
|
||||
.update(schema.flowchartSubmission)
|
||||
.set({ status: 3 })
|
||||
.where(eq(schema.flowchartSubmission.id, row.flowchart.id))
|
||||
return failure(
|
||||
c,
|
||||
502,
|
||||
"queue-unavailable",
|
||||
"Evaluation queue is unavailable",
|
||||
)
|
||||
}
|
||||
return success(c, { submissionId: row.flowchart.id, status: "pending" } satisfies CreateFlowchartResponse)
|
||||
return success(c, {
|
||||
submissionId: row.flowchart.id,
|
||||
status: "pending",
|
||||
} satisfies CreateFlowchartResponse)
|
||||
})
|
||||
|
||||
flowchartRoutes.get("/problems/:id/flowchart/current", requireAuth, async (c) => {
|
||||
const problemId = queryInteger(c.req.param("id"), 0, { min: 1 })
|
||||
const rows = await db.select({ score: schema.flowchartSubmission.aiScore, grade: schema.flowchartSubmission.aiGrade })
|
||||
.from(schema.flowchartSubmission).where(and(eq(schema.flowchartSubmission.userId, c.get("user")!.id), eq(schema.flowchartSubmission.problemId, problemId), eq(schema.flowchartSubmission.status, 2)))
|
||||
.orderBy(desc(schema.flowchartSubmission.createTime))
|
||||
return success(c, { count: rows.length, score: rows[0]?.score ?? 0, grade: rows[0]?.grade ?? "" } satisfies FlowchartCurrent)
|
||||
})
|
||||
flowchartRoutes.get(
|
||||
"/problems/:id/flowchart/current",
|
||||
requireAuth,
|
||||
async (c) => {
|
||||
const problemId = queryInteger(c.req.param("id"), 0, { min: 1 })
|
||||
const rows = await db
|
||||
.select({
|
||||
score: schema.flowchartSubmission.aiScore,
|
||||
grade: schema.flowchartSubmission.aiGrade,
|
||||
})
|
||||
.from(schema.flowchartSubmission)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.flowchartSubmission.userId, c.get("user")!.id),
|
||||
eq(schema.flowchartSubmission.problemId, problemId),
|
||||
eq(schema.flowchartSubmission.status, 2),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(schema.flowchartSubmission.createTime))
|
||||
return success(c, {
|
||||
count: rows.length,
|
||||
score: rows[0]?.score ?? 0,
|
||||
grade: rows[0]?.grade ?? "",
|
||||
} satisfies FlowchartCurrent)
|
||||
},
|
||||
)
|
||||
|
||||
flowchartRoutes.get("/problems/:id/flowchart/history", requireAuth, async (c) => {
|
||||
const problemId = queryInteger(c.req.param("id"), 0, { min: 1 })
|
||||
const page = queryInteger(c.req.query("page"), 0, { min: 0 })
|
||||
const rows = await db.select({ flowchart: schema.flowchartSubmission, username: schema.user.username })
|
||||
.from(schema.flowchartSubmission).innerJoin(schema.user, eq(schema.flowchartSubmission.userId, schema.user.id))
|
||||
.where(and(eq(schema.flowchartSubmission.userId, c.get("user")!.id), eq(schema.flowchartSubmission.problemId, problemId), eq(schema.flowchartSubmission.status, 2)))
|
||||
.orderBy(asc(schema.flowchartSubmission.createTime))
|
||||
const selected = page === 0 ? rows.at(-1) : rows[page - 1]
|
||||
if (page > rows.length) return failure(c, 400, "page-out-of-range", "Page out of range")
|
||||
return success(c, { submission: selected ? flowchartData(selected.flowchart, selected.username) : null, count: rows.length } satisfies FlowchartDetail)
|
||||
})
|
||||
flowchartRoutes.get(
|
||||
"/problems/:id/flowchart/history",
|
||||
requireAuth,
|
||||
async (c) => {
|
||||
const problemId = queryInteger(c.req.param("id"), 0, { min: 1 })
|
||||
const page = queryInteger(c.req.query("page"), 0, { min: 0 })
|
||||
const rows = await db
|
||||
.select({
|
||||
flowchart: schema.flowchartSubmission,
|
||||
username: schema.user.username,
|
||||
})
|
||||
.from(schema.flowchartSubmission)
|
||||
.innerJoin(
|
||||
schema.user,
|
||||
eq(schema.flowchartSubmission.userId, schema.user.id),
|
||||
)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.flowchartSubmission.userId, c.get("user")!.id),
|
||||
eq(schema.flowchartSubmission.problemId, problemId),
|
||||
eq(schema.flowchartSubmission.status, 2),
|
||||
),
|
||||
)
|
||||
.orderBy(asc(schema.flowchartSubmission.createTime))
|
||||
const selected = page === 0 ? rows.at(-1) : rows[page - 1]
|
||||
if (page > rows.length)
|
||||
return failure(c, 400, "page-out-of-range", "Page out of range")
|
||||
return success(c, {
|
||||
submission: selected
|
||||
? flowchartData(selected.flowchart, selected.username)
|
||||
: null,
|
||||
count: rows.length,
|
||||
} satisfies FlowchartDetail)
|
||||
},
|
||||
)
|
||||
Reference in new issue
Block a user