You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/packages/migrate/scripts/hustoj.ts

329 lines
14 KiB
TypeScript

/* eslint-disable no-tabs */
/* eslint-disable no-await-in-loop */
import path from 'path';
import mariadb from 'mariadb';
import TurndownService from 'turndown';
import {
_, buildContent, ContestModel, DomainModel, fs, noop, NotFoundError, ObjectId, postJudge, ProblemModel,
RecordDoc, RecordModel, SolutionModel, STATUS, StorageModel, SystemModel, Time, UserModel,
} from 'hydrooj';
const turndown = new TurndownService({
codeBlockStyle: 'fenced',
bulletListMarker: '-',
});
const statusMap = {
4: STATUS.STATUS_ACCEPTED,
5: STATUS.STATUS_WRONG_ANSWER,
6: STATUS.STATUS_WRONG_ANSWER,
7: STATUS.STATUS_TIME_LIMIT_EXCEEDED,
8: STATUS.STATUS_MEMORY_LIMIT_EXCEEDED,
9: STATUS.STATUS_OUTPUT_LIMIT_EXCEEDED,
10: STATUS.STATUS_RUNTIME_ERROR,
11: STATUS.STATUS_COMPILE_ERROR,
};
const langMap = {
0: 'c',
1: 'cc',
2: 'pas',
3: 'java',
4: 'rb',
5: 'bash',
6: 'py',
7: 'php',
8: 'perl',
9: 'cs',
10: 'oc',
11: 'fb',
12: 'sc',
13: 'cl',
14: 'cl++',
15: 'lua',
16: 'js',
17: 'go',
};
const nameMap: Record<string, string> = {
'sample.in': 'sample0.in',
'sample.out': 'sample0.out',
'test.in': 'test0.in',
'test.out': 'test0.out',
};
async function addContestFile(domainId: string, tid: ObjectId, filename: string, filepath: string) {
const tdoc = await ContestModel.get(domainId, tid);
await StorageModel.put(`contest/${domainId}/${tid}/${filename}`, filepath, 1);
const meta = await StorageModel.getMeta(`contest/${domainId}/${tid}/${filename}`);
const payload = { _id: filename, name: filename, ..._.pick(meta, ['size', 'lastModified', 'etag']) };
if (!meta) return false;
await ContestModel.edit(domainId, tid, { files: [...(tdoc.files || []), payload] });
return true;
}
export async function run({
host = 'localhost', port = 3306, name = 'jol',
username, password, domainId, contestType = 'oi',
dataDir, uploadDir = '/home/judge/src/web/upload/', rerun = true, randomMail = false,
}, report: Function) {
const src = await mariadb.createConnection({
host,
port,
user: username,
password,
database: name,
});
const query = (q: string) => new Promise<any[]>((res, rej) => {
src.query(q).then((r) => res(r)).catch((e) => rej(e));
});
const target = await DomainModel.get(domainId);
if (!target) throw new NotFoundError(domainId);
report({ message: 'Connected to database' });
/*
user_id varchar 20 N id
email varchar 100 Y E-mail
submit int 11 Y
solved int 11 Y
defunct char 1 N Y/N
ip varchar 20 N ip
accesstime datetime Y
volume int 11 N
language int 11 N
password varchar 32 Y
reg_time datetime Y
nick varchar 100 N
school varchar 100 N
*/
const uidMap: Record<string, number> = {};
const udocs = await query('SELECT * FROM `users`');
const precheck = await UserModel.getMulti({ unameLower: { $in: udocs.map((u) => u.user_id.toLowerCase()) } }).toArray();
if (precheck.length) throw new Error(`Conflict username: ${precheck.map((u) => u.unameLower).join(', ')}`);
for (const udoc of udocs) {
if (randomMail) delete udoc.email;
let current = await UserModel.getByEmail(domainId, udoc.email || `${udoc.user_id}@hustoj.local`);
current ||= await UserModel.getByUname(domainId, udoc.user_id);
if (current) {
report({ message: `duplicate user with email ${udoc.email}: ${current.uname},${udoc.user_id}` });
uidMap[udoc.user_id] = current._id;
} else {
const uid = await UserModel.create(
udoc.email || `${udoc.user_id}@hustoj.local`, udoc.user_id, '',
null, udoc.ip, udoc.defunct === 'Y' ? 0 : SystemModel.get('default.priv'),
);
uidMap[udoc.user_id] = uid;
await UserModel.setById(uid, {
loginat: udoc.accesstime,
regat: udoc.reg_time,
hash: udoc.password,
salt: udoc.password,
school: udoc.school || '',
hashType: 'hust',
});
await DomainModel.setUserInDomain(domainId, uid, {
displayName: udoc.nick || '',
school: udoc.school || '',
nSubmit: udoc.submit,
nAccept: 0,
});
}
}
const admins = await query("SELECT * FROM `privilege` WHERE `rightstr` = 'administrator'");
for (const admin of admins) await DomainModel.setUserRole(domainId, uidMap[admin.user_id], 'root');
const adminUids = admins.map((admin) => uidMap[admin.user_id]);
report({ message: 'user finished' });
/*
problem_id int 11 N
title varchar 200 N
description text Y
inupt text Y
output text Y
sample_input text Y
sample_output text Y
spj char 1 N
hint text Y
source varchar 100 Y
in_date datetime Y
time_limit int 11 N
memory_limit int 11 N (MByte)
defunct char 1 N Y/N
accepted int 11 Y ac
submit int 11 Y
solved int 11 Y
solution #optional
*/
const pidMap: Record<string, number> = {};
const [{ 'count(*)': pcount }] = await query('SELECT count(*) FROM `problem`');
const step = 50;
const pageCount = Math.ceil(Number(pcount) / step);
for (let pageId = 0; pageId < pageCount; pageId++) {
const pdocs = await query(`SELECT * FROM \`problem\` LIMIT ${pageId * step}, ${step}`);
for (const pdoc of pdocs) {
if (rerun) {
const opdoc = await ProblemModel.get(domainId, `P${pdoc.problem_id}`);
if (opdoc) pidMap[pdoc.problem_id] = opdoc.docId;
}
if (!pidMap[pdoc.problem_id]) {
const files = {};
let content = buildContent({
description: pdoc.description,
input: pdoc.input,
output: pdoc.output,
samples: [[pdoc.sample_input.trim(), pdoc.sample_output.trim()]],
hint: pdoc.hint,
source: pdoc.source,
}, 'html');
const uploadFiles = content.matchAll(/(?:src|href)="\/upload\/([^"]+\/([^"]+))"/g);
for (const file of uploadFiles) {
try {
files[file[2]] = await fs.readFile(path.join(uploadDir, file[1]));
content = content.replace(`/upload/${file[1]}`, `file://${file[2]}`);
} catch (e) {
report({ message: `failed to read file: ${path.join(uploadDir, file[1])}` });
}
}
const pid = await ProblemModel.add(
domainId, `P${pdoc.problem_id}`,
pdoc.title, content,
1, pdoc.source.split(' ').map((i) => i.trim()).filter((i) => i),
{ hidden: pdoc.defunct === 'Y' },
);
pidMap[pdoc.problem_id] = pid;
await Promise.all(Object.keys(files).map((filename) => ProblemModel.addAdditionalFile(domainId, pid, filename, files[filename])));
if (Object.keys(files).length) report({ message: `move ${Object.keys(files).length} file for problem ${pid}` });
}
const cdoc = await query(`SELECT * FROM \`privilege\` WHERE \`rightstr\` = 'p${pdoc.problem_id}'`);
const maintainer = [];
for (let i = 1; i < cdoc.length; i++) maintainer.push(uidMap[cdoc[i].user_id]);
await ProblemModel.edit(domainId, pidMap[pdoc.problem_id], {
nAccept: 0,
nSubmit: pdoc.submit,
4 years ago
config: `time: ${pdoc.time_limit}s\nmemory: ${pdoc.memory_limit}m`,
owner: uidMap[cdoc[0]?.user_id] || 1,
maintainer,
html: true,
});
if (pdoc.solution) {
const md = turndown.turndown(pdoc.solution);
await SolutionModel.add(domainId, pidMap[pdoc.problem_id], 1, md);
}
}
}
report({ message: 'problem finished' });
/*
contest_id int 11 N id
title varchar 255 Y
start_time datetime Y ()
end_time datatime Y ()
defunct char 1 N Y/N
description text Y
private tinyint 4 /0/1
langmask int 11
password char(16)
user_id char(48)
*/
const tidMap: Record<string, string> = {};
const tdocs = await query('SELECT * FROM `contest`');
for (const tdoc of tdocs) {
const pdocs = await query(`SELECT * FROM \`contest_problem\` WHERE \`contest_id\` = ${tdoc.contest_id}`);
const pids = pdocs.map((i) => pidMap[i.problem_id]).filter((i) => i);
const files = {};
let description = tdoc.description;
const uploadFiles = description.matchAll(/(?:src|href)="\/upload\/([^"]+\/([^"]+))"/g);
for (const file of uploadFiles) {
files[file[2]] = await fs.readFile(path.join(uploadDir, file[1]));
description = description.replace(`/upload/${file[1]}`, `file://${file[2]}`);
}
const tid = await ContestModel.add(
domainId, tdoc.title, tdoc.description || 'Description',
adminUids[0], contestType, tdoc.start_time, tdoc.end_time, pids, true,
{ _code: password },
);
tidMap[tdoc.contest_id] = tid.toHexString();
await Promise.all(Object.keys(files).map((filename) => addContestFile(domainId, tid, filename, files[filename])));
if (Object.keys(files).length) report({ message: `move ${Object.keys(files).length} file for contest ${tidMap[tdoc.contest_id]}` });
}
report({ message: 'contest finished' });
/*
solution
solution_id int 11 N id
problem_id int 11 N id
user_id char 20 N id
time int 11 N
memory int 11 N
in_date datetime N
result smallint 6 N 4AC
language tinyint 4 N
ip char 15 N ip
contest_id int 11 Y
valid tinyint 4 N
num tinyint 4 N
code_lenght int 11 N
judgetime datetime Y
pass_rate decimal 2 N OI
lint_error int N
judger char(16) N
*/
const [{ 'count(*)': rcount }] = await query('SELECT count(*) FROM `solution`');
const rpageCount = Math.ceil(Number(rcount) / step);
for (let pageId = 0; pageId < rpageCount; pageId++) {
const rdocs = await query(`SELECT * FROM \`solution\` LIMIT ${pageId * step}, ${step}`);
for (const rdoc of rdocs) {
const data: RecordDoc = {
status: statusMap[rdoc.result] || 0,
_id: Time.getObjectID(rdoc.in_date, false),
uid: uidMap[rdoc.user_id] || 0,
code: "HustOJ didn't provide user code",
lang: langMap[rdoc.language] || '',
pid: pidMap[rdoc.problem_id] || 0,
domainId,
score: rdoc.pass_rate ? Math.ceil(rdoc.pass_rate * 100) : rdoc.result === 4 ? 100 : 0,
time: rdoc.time || 0,
memory: rdoc.memory || 0,
judgeTexts: [],
compilerTexts: [],
testCases: [],
judgeAt: new Date(),
rejudged: false,
judger: 1,
};
const ceInfo = await query(`SELECT \`error\` FROM \`compileinfo\` WHERE \`solution_id\` = ${rdoc.solution_id}`);
if (ceInfo[0]?.error) data.judgeTexts.push(ceInfo[0].error);
const rtInfo = await query(`SELECT \`error\` FROM \`runtimeinfo\` WHERE \`solution_id\` = ${rdoc.solution_id}`);
if (rtInfo[0]?.error) data.judgeTexts.push(rtInfo[0].error);
const source = await query(`SELECT \`source\` FROM \`source_code\` WHERE \`solution_id\` = ${rdoc.solution_id}`);
if (source[0]?.source) data.code = source[0].source;
if (rdoc.contest_id) {
data.contest = new ObjectId(tidMap[rdoc.contest_id]);
await ContestModel.attend(domainId, data.contest, uidMap[rdoc.user_id]).catch(noop);
}
await RecordModel.coll.insertOne(data);
await postJudge(data).catch((err) => report({ message: err.message }));
}
}
report({ message: 'record finished' });
src.end();
if (!dataDir) return true;
if (dataDir.endsWith('/')) dataDir = dataDir.slice(0, -1);
const files = await fs.readdir(dataDir, { withFileTypes: true });
for (const file of files) {
if (!file.isDirectory()) continue;
const datas = await fs.readdir(`${dataDir}/${file.name}`, { withFileTypes: true });
const pdoc = await ProblemModel.get(domainId, `P${file.name}`, undefined, true);
if (!pdoc) continue;
report({ message: `Syncing testdata for ${file.name}` });
for (const data of datas) {
if (data.isDirectory()) continue;
const filename = nameMap[data.name] || data.name;
await ProblemModel.addTestdata(domainId, pdoc.docId, filename, `${dataDir}/${file.name}/${data.name}`);
}
await ProblemModel.addTestdata(domainId, pdoc.docId, 'config.yaml', Buffer.from(pdoc.config as string));
}
return true;
}