统一PostJudge接口

This commit is contained in:
undefined committed 2020-07-18 13:10:04 +08:00
1 parent 0ee306d0cf
commit 9efcdf9441
9 files changed
+108 -166

No files matched your search

+47 -27
View File
@@ -1,6 +1,7 @@
import yaml from 'js-yaml';
import { ObjectID } from 'mongodb';
import { parseTimeMS, parseMemoryMB } from '../utils';
import { JudgeResultBody, Rdoc } from '../interface';
import * as record from '../model/record';
import * as problem from '../model/problem';
import * as builtin from '../model/builtin';
@@ -12,12 +13,31 @@ import {
Route, Handler, Connection, ConnectionHandler, Types, param,
} from '../service/server';
async function _postJudge(rdoc) {
interface _PostJudgeBody {
status: number,
domainId: string,
uid: number,
_id: ObjectID,
pid: number,
score: number,
rejudged: boolean,
}
interface PostJudgeBodyContest extends _PostJudgeBody {
tid: ObjectID,
ttype: number,
}
type PostJudgeBody = _PostJudgeBody | PostJudgeBodyContest;
function _isWithContest(v: PostJudgeBody): v is PostJudgeBodyContest {
return !!((v as PostJudgeBodyContest).tid);
}
async function _postJudge(rdoc: PostJudgeBody) {
const accept = rdoc.status === builtin.STATUS.STATUS_ACCEPTED;
bus.publish('record_change', rdoc);
if (rdoc.type === 'run') return;
const tasks = [];
if (rdoc.tid) {
if (_isWithContest(rdoc)) {
tasks.push(
contest.updateStatus(
rdoc.domainId, rdoc.tid, rdoc.uid,
@@ -36,7 +56,7 @@ async function _postJudge(rdoc) {
await Promise.all(tasks);
}
export async function next(body) {
export async function next(body: JudgeResultBody) {
if (body.rid) body.rid = new ObjectID(body.rid);
if (body.tid) body.tid = new ObjectID(body.tid);
let rdoc = await record.get(body.domainId, body.rid);
@@ -44,56 +64,56 @@ export async function next(body) {
const $push: any = {};
if (body.case) {
const c: any = {};
c.memory = body.case.memory_kb || body.case.memory;
c.time = body.case.time_ms || body.case.time;
c.judgeText = body.case.judge_text || body.case.judgeText || body.case.message;
c.memory = body.case.memory;
c.time = body.case.time;
c.message = body.case.message;
c.status = body.case.status;
rdoc.testCases.push(c);
$push.testCases = c;
}
if (body.judge_text || body.message) {
rdoc.judgeTexts.push(body.judge_text || body.message);
$push.judgeTexts = body.judge_text || body.message;
if (body.message) {
rdoc.judgeTexts.push(body.message);
$push.judgeTexts = body.message;
}
if (body.compiler_text) {
rdoc.compilerTexts.push(body.compiler_text);
$push.compilerTexts = body.compiler_text;
if (body.compilerText) {
rdoc.compilerTexts.push(body.compilerText);
$push.compilerTexts = body.compilerText;
}
if (body.status) $set.status = body.status;
if (body.score) $set.score = body.score;
if (body.time_ms) $set.time = body.time_ms || body.time;
if (body.memory_kb) $set.memory = body.memory_kb || body.memory;
if (body.time) $set.time = body.time;
if (body.memory) $set.memory = body.memory;
if (body.progress) $set.progress = body.progress;
rdoc = await record.update(body.domainId, body.rid, $set, $push);
bus.publish('record_change', rdoc);
bus.publish('record_change', { rdoc, $set, $push });
}
export async function end(body) {
export async function end(body: JudgeResultBody) {
if (body.rid) body.rid = new ObjectID(body.rid);
if (body.tid) body.tid = new ObjectID(body.tid);
let rdoc = await record.get(body.domainId, body.rid);
const $set: any = {};
const $push: any = {};
const $unset = { progress: '' };
if (body.judge_text) {
rdoc.judgeTexts.push(body.judge_text);
$push.judgeTexts = body.judge_text;
if (body.message) {
rdoc.judgeTexts.push(body.message);
$push.judgeTexts = body.message;
}
if (body.compiler_text) {
rdoc.compilerTexts.push(body.compiler_text);
$push.compilerTexts = body.compiler_text;
if (body.compilerText) {
rdoc.compilerTexts.push(body.compilerText);
$push.compilerTexts = body.compilerText;
}
if (body.status) $set.status = body.status;
if (body.score) $set.score = body.score;
if (body.stdout) $set.stdout = body.stdout;
if (body.stderr) $set.stderr = body.stderr;
if (body.time_ms) $set.time = body.time_ms;
if (body.memory_kb) $set.memory = body.memory_kb;
if (body.time) $set.time = body.time;
if (body.memory) $set.memory = body.memory;
$set.judgeAt = new Date();
$set.judger = body.judger;
rdoc = await record.update(body.domainId, body.rid, $set, $push, $unset);
await _postJudge(rdoc);
rdoc = await record.update(body.domainId, body.rid, $set, $push);
bus.publish('record_change', { rdoc }); // trigger a full update
}
class PretestHandler extends Handler {
+6 -6
View File
@@ -128,10 +128,10 @@ class SystemScriptHandler extends SystemHandler {
domainId,
rid,
status: STATUS.STATUS_ACCEPTED,
judge_text: ret.toString(),
message: ret.toString(),
judger: 1,
time_ms: time,
memory_kb: 0,
time,
memory: 0,
});
})
.catch((err: Error) => {
@@ -140,10 +140,10 @@ class SystemScriptHandler extends SystemHandler {
domainId,
rid,
status: STATUS.STATUS_SYSTEM_ERROR,
judge_text: `${err}\n${err.stack}`,
message: `${err}\n${err.stack}`,
judger: 1,
time_ms: time,
memory_kb: 0,
time,
memory: 0,
});
});
}, 300);
+8 -1
View File
@@ -254,6 +254,7 @@ class ProblemPretestConnectionHandler extends ConnectionHandler {
@param('pid', Types.String)
async prepare(domainId: string, pid: string) {
const pdoc = await problem.get(domainId, pid);
if (pdoc.hidden) this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
if (!pdoc) throw new ProblemNotFoundError(domainId, pid);
this.pid = pdoc.docId.toString();
this.domainId = domainId;
@@ -261,12 +262,18 @@ class ProblemPretestConnectionHandler extends ConnectionHandler {
}
async onRecordChange(data) {
const rdoc: Rdoc = data.value;
const rdoc: Rdoc = data.value.rdoc;
if (
rdoc.uid !== this.user._id
|| rdoc.pid.toString() !== this.pid
|| rdoc.domainId !== this.domainId
) return;
rdoc.compilerTexts = [];
rdoc.judgeTexts = [];
// @ts-ignore
rdoc.testCases = rdoc.testCases.map((c) => ({
status: c.status,
}));
if (rdoc.tid) return;
this.send({ rdoc });
}
+14 -8
View File
@@ -11,6 +11,7 @@ import * as bus from '../service/bus';
import {
Route, Handler, Connection, ConnectionHandler, Types, param,
} from '../service/server';
import { Rdoc } from '../interface';
const RecordHandler = contest.ContestHandlerMixin(Handler);
@@ -117,8 +118,9 @@ class RecordMainConnectionHandler extends RecordConnectionHandler {
async message(msg) {
if (msg.rids instanceof Array) {
const rids = msg.rids.map((id: string) => new ObjectID(id));
const rdocs = await record.getMulti(
this.domainId, { _id: { $in: msg.rids.map((id) => new ObjectID(id)) } },
this.domainId, { _id: { $in: rids } },
).toArray();
for (const rdoc of rdocs) {
this.onRecordChange({ value: rdoc });
@@ -131,7 +133,7 @@ class RecordMainConnectionHandler extends RecordConnectionHandler {
}
async onRecordChange(data) {
const rdoc = data.value;
const rdoc: Rdoc = data.value.rdoc;
if (rdoc.tid && rdoc.tid.toString() !== this.tid) return;
// eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([
@@ -139,7 +141,7 @@ class RecordMainConnectionHandler extends RecordConnectionHandler {
problem.get(this.domainId, rdoc.pid, null, false),
]);
if (pdoc && pdoc.hidden && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
else this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
}
}
@@ -160,12 +162,16 @@ class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(Connecti
}
async onRecordChange(data) {
const rdoc = data.value;
const rdoc: Rdoc = data.value.rdoc;
const { $set, $push } = data.value;
if (rdoc._id.toString() !== this.rid) return;
this.send({
status_html: await this.renderHTML('record_detail_status.html', { rdoc }),
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc }),
});
if ($set) this.send({ $set, $push });
else {
this.send({
status_html: await this.renderHTML('record_detail_status.html', { rdoc }),
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc }),
});
}
}
async cleanup() {