统一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

+1
View File
@@ -33,6 +33,7 @@ module.exports = {
'no-return-await': 'off',
'no-underscore-dangle': 'off',
'no-unused-vars': 'off',
'prefer-destructuring': 'off',
},
settings: {
'import/parsers': {
+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() {
+24
View File
@@ -199,6 +199,30 @@ export interface Script {
validate: any,
}
export interface JudgeResultBody {
domainId: string,
rid: ObjectID,
judger?: number,
tid?: ObjectID,
progress?: number
case?: {
status: number,
time: number,
memory: number,
message?: string,
},
status?: number,
score?: number,
time?: number,
memory?: number,
message?: string,
compilerText?: string,
// For pretest
stdout?: string,
stderr?: string,
}
declare global {
namespace NodeJS {
interface Global {
+6 -6
View File
@@ -64,9 +64,9 @@ async function runContest(...arg: Array<any>) {
await report({
case: {
status: STATUS.STATUS_ACCEPTED,
judgeText: `Contest ${tdoc.title} finished`,
time_ms: new Date().getTime() - start,
memory_kb: 0,
message: `Contest ${tdoc.title} finished`,
time: new Date().getTime() - start,
memory: 0,
score: 0,
},
});
@@ -115,9 +115,9 @@ export async function run({ domainId }, report: Function) {
await report({
case: {
status: STATUS.STATUS_ACCEPTED,
judgeText: `Domain ${domains[i]._id} finished`,
time_ms: new Date().getTime() - start,
memory_kb: 0,
message: `Domain ${domains[i]._id} finished`,
time: new Date().getTime() - start,
memory: 0,
score: 0,
},
progress: Math.floor(((parseInt(i, 10) + 1) / domains.length) * 100),
+2
View File
@@ -700,6 +700,8 @@ export class ConnectionHandler {
this.conn.close(code.toString(), reason);
}
async message(message: any) { } // eslint-disable-line
onerror(err: HydroError) {
console.error(err);
this.send({
-118
View File
@@ -1,118 +0,0 @@
import { NamedPage } from 'vj/misc/PageLoader';
import * as recordEnum from 'vj/constant/record';
const page = new NamedPage('judge_playground', async () => {
const { default: SockJs } = await import('../components/socket');
const sock = new SockJs(`/judge/consume-conn?token=${UiContext.token}`);
sock.onopen = () => {
const div = $('<div class="section visible">').appendTo('#messages');
$('<div class="section__header"><h1 class="section__title">Connection opened.</h1></div>')
.appendTo(div);
};
sock.onmessage = (message) => {
const msg = JSON.parse(message.data);
const div = $('<div class="section visible">').appendTo('#messages');
$('<div class="section__header"><h1 class="section__title">Record</h1></div>')
.appendTo(div);
const body = $('<div class="section__body">').text(message.data).appendTo(div);
const send = (key, packet) => {
const data = {
...packet,
key,
tag: msg.tag,
};
sock.send(JSON.stringify(data));
};
$('<button class="button rounded primary">').text('Compile')
.on('click', () => {
send('next', { status: 20 });
})
.appendTo(body);
$('<button class="button rounded primary">').text('Point0')
.on('click', () => {
send('next', {
case: {
status: recordEnum.STATUS_WRONG_ANSWER,
score: 0,
time_ms: 1,
memory_kb: 777,
judge_text: 'from playground',
},
progress: 51.123,
});
})
.appendTo(body);
$('<button class="button rounded primary">').text('Point10')
.on('click', () => {
send('next', {
case: {
status: recordEnum.STATUS_ACCEPTED,
score: 10,
time_ms: 1,
memory_kb: 233,
judge_text: 'from playground',
},
progress: 90.0,
});
})
.appendTo(body);
$('<button class="button rounded primary">').text('Accept')
.on('click', () => {
send('end', {
status: recordEnum.STATUS_ACCEPTED,
score: 100,
time_ms: 1,
memory_kb: 1,
});
$('button', div).detach();
})
.appendTo(body);
$('<button class="button rounded primary">').text('WA')
.on('click', () => {
send('end', {
status: recordEnum.STATUS_WRONG_ANSWER,
score: 88,
time_ms: 88,
memory_kb: 88,
});
$('button', div).detach();
})
.appendTo(body);
$('<button class="button rounded primary">').text('TLE')
.on('click', () => {
send('end', {
status: recordEnum.STATUS_TIME_LIMIT_EXCEEDED,
score: 10,
time_ms: 9999,
memory_kb: 88,
});
$('button', div).detach();
})
.appendTo(body);
};
sock.onclose = (message) => {
const div = $('<div class="section visible">').appendTo('#messages');
$(`
<div class="section__header">
<h1 class="section__title">
Connection closed,
reason=${JSON.stringify(message.reason)}
</h1>
</div>
`)
.appendTo(div);
};
});
export default page;