diff --git a/hydro/lib/markdown.js b/hydro/lib/markdown.js index cb67876df..5d25a2f16 100644 --- a/hydro/lib/markdown.js +++ b/hydro/lib/markdown.js @@ -1,10 +1,7 @@ const MarkdownIt = require('markdown-it'); const katex = require('markdown-it-katex'); const Prism = require('prismjs'); -const loadLanguages = require('prismjs/components/'); - -// FIXME doesnt work in webpack -loadLanguages(['js', 'javascript', 'go', 'c', 'pascal', 'hs', 'java', 'cs', 'cpp', 'py', 'ruby']); +require('prismjs/components/index'); class Markdown extends MarkdownIt { constructor(safe) { diff --git a/hydro/model/message.js b/hydro/model/message.js index a719d2743..e6820fdcc 100644 --- a/hydro/model/message.js +++ b/hydro/model/message.js @@ -53,6 +53,12 @@ async function addReply(_id, from, content) { return await coll.findOne({ _id }); // eslint-disable-line no-return-await } +async function send(from, to, content) { + const sdoc = await coll.findOne({ $or: [{ from, to }, { from: to, to: from }] }); + await addReply(sdoc._id, from, content); + return sdoc._id; +} + function index() { return Promise.all([ coll.createIndex({ to: 1, _id: -1 }), @@ -68,5 +74,6 @@ global.Hydro.model.message = module.exports = { getMany, getMulti, addReply, + send, index, }; diff --git a/hydro/model/task.js b/hydro/model/task.js index 0769e0d9b..efff10215 100644 --- a/hydro/model/task.js +++ b/hydro/model/task.js @@ -21,10 +21,20 @@ function del(_id) { async function getFirst(query) { const res = await coll.find(query).sort('_id', 1).limit(1).toArray(); - await coll.deleteOne({ _id: res[0]._id }); - return res[0]; + if (res) { + await coll.deleteOne({ _id: res[0]._id }); + return res[0]; + } + return null; +} + +async function consume(query, cb) { + setInterval(async () => { + const res = await getFirst(query); + if (res) cb(res); + }, 100); } module.exports = { - add, get, del, count, getFirst, + add, get, del, count, getFirst, consume, }; diff --git a/module/judger/service.js b/module/judger/service.js index 86f905850..d1e40d60c 100644 --- a/module/judger/service.js +++ b/module/judger/service.js @@ -1,4 +1,3 @@ -// Webpack Required const fs = require('fs'); const path = require('path'); const child = require('child_process'); @@ -157,7 +156,7 @@ async function postInit() { await judger[this.config.type || 'default'].judge(this); } } - task.consume((t) => { + task.consume({ type: 'judge' }, (t) => { (new JudgeTask(t)).handle(); }); } diff --git a/module/migrate-vijos/handler.js b/module/migrate-vijos/handler.js index 8239cdcca..29f52087d 100644 --- a/module/migrate-vijos/handler.js +++ b/module/migrate-vijos/handler.js @@ -1,7 +1,6 @@ -const { - Route, Connection, Handler, ConnectionHandler, -} = global.Hydro.service.server; +const { Route, Handler } = global.Hydro.service.server; const { PERM_MANAGE } = global.Hydro.permission; +const { message } = global.Hydro.model; const migrate = global.Hydro.script.migrateVijos; class MigrateVijosHandler extends Handler { @@ -17,25 +16,21 @@ class MigrateVijosHandler extends Handler { this.response.body = { path }; this.response.template = 'migrate_vijos.html'; } -} -class MigrateVijosConnectionHandler extends ConnectionHandler { - async prepare() { - this.checkPerm(PERM_MANAGE); - } - - async message(msg) { - if (msg.key === 'start') { - migrate(msg, (data) => { this.send(data); }); - } + async post({ + host, port, name, username, password, + }) { + migrate({ + host, port, name, username, password, + }, (data) => message.send(0, 0, data)); + this.response.redirect = '/manage/log'; } } async function apply() { Route('/migrate/vijos', module.exports.MigrateVijosHandler); - Connection('/migrate/vijos-conn', module.exports.MigrateVijosConnectionHandler); } global.Hydro.handler.pastebin = module.exports = { - MigrateVijosHandler, MigrateVijosConnectionHandler, apply, + MigrateVijosHandler, apply, }; diff --git a/module/migrate-vijos/script.js b/module/migrate-vijos/script.js index 732ff5b10..12def5757 100644 --- a/module/migrate-vijos/script.js +++ b/module/migrate-vijos/script.js @@ -13,8 +13,8 @@ function parseSolution(doc) { } async function problem(src, report) { - const count = await src.collection('document').find({ doc_type: 10 }).count(); - report({ progress: 1, message: `Found ${count} problems.` }); + let count = await src.collection('document').find({ doc_type: 10 }).count(); + await report({ progress: 1, message: `Found ${count} problems.` }); for (let i = 0; i <= Math.floor(count / 50); i++) { const docs = await src.collection('document').find({ doc_type: 10 }) .skip(i * 50).limit(50) @@ -36,12 +36,27 @@ async function problem(src, report) { } await dst.collection('problem').insertMany(problems); } + count = await src.collection('document.status').find({ doc_type: 10 }).count(); + for (let i = 0; i <= Math.floor(count / 50); i++) { + const docs = await src.collection('document.status').find({ doc_type: 10 }) + .skip(i * 50).limit(50) + .toArray(); + const problems = []; + for (const doc of docs) { + problems.push({ + pid: doc.pid, + rid: doc.rid, + }); + } + await dst.collection('problem.status').insertMany(problems); + } + await report({ progress: 1, message: `Found ${count} problems.` }); } async function contest(src, report) { const RULES = ['', 'oi', 'acm']; const count = await src.collection('document').find({ doc_type: 30 }).count(); - report({ progress: 1, message: `Found ${count} contests.` }); + await report({ progress: 1, message: `Found ${count} contests.` }); for (let i = 0; i <= Math.floor(count / 50); i++) { const docs = await src.collection('document').find({ doc_type: 30 }) .skip(i * 50).limit(50) @@ -74,12 +89,12 @@ async function migrateVijos({ useNewUrlParser: true, useUnifiedTopology: true, }); const src = Database.db(name); - report({ progress: 0, message: 'Database connected.' }); - dst.collection('system').insertOne({ + await report({ progress: 0, message: 'Database connected.' }); + await dst.collection('system').insertOne({ _id: 'user', value: (await src.collection('system').findOne({ _id: 'user_counter' })).value, }); - report({ progress: 1, message: 'Collection:system done.' }); + await report({ progress: 1, message: 'Collection:system done.' }); await problem(src, report); await contest(src, report); } diff --git a/module/migrate-vijos/template/migrate_vijos.html b/module/migrate-vijos/template/migrate_vijos.html new file mode 100644 index 000000000..85ccc2a0d --- /dev/null +++ b/module/migrate-vijos/template/migrate_vijos.html @@ -0,0 +1,61 @@ +{% extends "layout/basic.html" %} +{% block content %} +