From 42cbd020d59b9bcdfc9ebab1434d02ffa79cf254 Mon Sep 17 00:00:00 2001 From: masnn Date: Fri, 15 May 2020 20:21:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E5=B7=A5=E5=85=B7=EF=BC=9A?= =?UTF-8?q?=20+user=20+solution=20+discussion=20+training?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hydro/loader.js | 2 + module/migrate-vijos/script.js | 147 +++++++++++++++++++++++++++------ ui/constant/record.js | 9 -- 3 files changed, 125 insertions(+), 33 deletions(-) diff --git a/hydro/loader.js b/hydro/loader.js index 3b8fca412..5b016f4bb 100644 --- a/hydro/loader.js +++ b/hydro/loader.js @@ -209,6 +209,8 @@ async function load() { script: {}, lib: {}, nodeModules: { + bson: require('bson'), + 'js-yaml': require('js-yaml'), mongodb: require('mongodb'), }, template: {}, diff --git a/module/migrate-vijos/script.js b/module/migrate-vijos/script.js index b6feae7ee..bea48f382 100644 --- a/module/migrate-vijos/script.js +++ b/module/migrate-vijos/script.js @@ -1,11 +1,8 @@ /* eslint-disable no-await-in-loop */ -const Mongo = global.Hydro.nodeModules.mongodb; -const { ObjectID } = global.Hydro.nodeModules.bson; +const { mongodb, bson } = global.Hydro.nodeModules; const dst = global.Hydro.service.db; const { problem } = global.Hydro.model.problem; -const RULES = ['', 'oi', 'acm']; - const tasks = { user: async (docs) => docs.map((doc) => ({ _id: doc._id, @@ -17,11 +14,21 @@ const tasks = { gravatar: doc.gravatar, email: doc.mail, emailLower: doc.mail_lower, - + regat: doc.regat, + regip: doc.regip, + loginat: doc.loginat, + loginip: doc.loginip, + codeLang: doc.code_lang, + codeTemplate: doc.code_template, + timezone: doc.timezone, + viewLang: doc.viewLang, + bio: doc.bio, + gender: doc.gender, + qq: doc.qq, })), problem: async (docs) => docs.map((doc) => ({ _id: doc._id, - pid: doc.doc_id || new ObjectID(), + pid: doc.pname || doc.doc_id.toString() || new bson.ObjectID(), title: doc.title, content: doc.content, owner: doc.owner_uid, @@ -43,30 +50,120 @@ const tasks = { } return problems; }, - contest: async (docs) => docs.map((doc) => ({ + solution: async (docs) => { + const solutions = []; + for (const doc of docs) { + const reply = doc.reply.map((r) => ({ + owner: r.owner_uid, + content: r.content, + _id: r._id, + })); + const pdoc = await problem.get(doc.parent_doc_id.toString()); + solutions.push({ + _id: doc._id, + owner: doc.owner_uid, + content: doc.content, + vote: doc.vote, + reply, + pid: pdoc._id, + }); + } + return solutions; + }, + discussion: async (docs) => { + const TYPE = { + 10: 'problem', + 20: 'node', + 30: 'contest', + }; + const discussions = []; + for (const doc of docs) { + discussions.push({ + _id: doc._id, + title: doc.title, + content: doc.content, + owner: doc.owner_uid, + views: doc.views, + highlight: doc.highlight, + nReply: doc.num_replies, + ip: doc.ip, + updateAt: doc.update_at, + parentType: TYPE[doc.parentType], + parentId: doc.parentId, + }); + } + return discussions; + }, + 'discussion.reply': async (docs) => docs.map((doc) => ({ _id: doc._id, - title: doc.title, - content: doc.content, owner: doc.owner_uid, - rule: RULES[doc.rule], - beginAt: doc.begin_at, - endAt: doc.end_at, - pids: doc.pids, - attend: doc.attend, - })), - solution: async (docs) => docs.map((doc) => ({ - _id: doc._id, - title: doc.title, + ip: doc.ip, content: doc.content, - vote: doc.vote, + parentId: doc.parent_doc_id, })), + contest: async (docs) => { + const RULES = ['oi', 'acm', 'homework']; + const contests = []; + for (const doc of docs) { + const pids = []; + for (const pid of doc.pids) { + const pdoc = await problem.get(pid.toString()); + pids.push(pdoc._id); + } + contests.push({ + _id: doc._id, + title: doc.title, + content: doc.content, + owner: doc.owner_uid, + rule: RULES[doc.rule], + beginAt: doc.begin_at, + endAt: doc.end_at, + pids, + attend: doc.attend, + }); + } + return contests; + }, + training: async (docs) => { + const trainings = []; + for (const doc of docs) { + const dag = []; + for (const t of doc.dag) { + const pids = []; + for (const pid of t.pids) { + const pdoc = await problem.get(pid.toString()); + pids.push(pdoc._id); + } + dag.push({ + _id: t._id, + title: t.title, + requireNids: t.require_nids, + pids, + }); + } + trainings.push({ + _id: doc._id, + title: doc.title, + content: doc.content, + description: doc.desc, + enroll: doc.enroll, + owner: doc.owner_uid, + dag, + }); + } + return trainings; + }, }; const cursor = { + user: (s) => s.collection('user').find(), problem: (s) => s.collection('document').find({ doc_type: 10 }), 'problem.status': (s) => s.collection('document.status').find({ doc_type: 10 }), + solution: (s) => s.collection('document').find({ doc_type: 11 }), + discussion: (s) => s.collection('document').find({ doc_type: 21 }), + 'discussion.reply': (s) => s.collection('document').find({ doc_type: 22 }), contest: (s) => s.collection('document').find({ doc_type: 30 }), - user: (s) => s.collection('user').find(), + training: (s) => s.collection('document').find({ doc_type: 40 }), }; async function task(name, src, report) { @@ -86,7 +183,7 @@ async function migrateVijos({ let mongourl = 'mongodb://'; if (username) mongourl += `${username}:${password}@`; mongourl += `${host}:${port}/${name}`; - const Database = await Mongo.MongoClient.connect(mongourl, { + const Database = await mongodb.MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true, }); const src = Database.db(name); @@ -96,9 +193,11 @@ async function migrateVijos({ value: (await src.collection('system').findOne({ _id: 'user_counter' })).value, }); await report({ progress: 1, message: 'Collection:system done.' }); - await task('problem', src, report); - await task('problem.status', src, report); - await task('contest', src, report); + const t = [ + 'user', 'problem', 'problem.status', 'solution', 'discussion', + 'discussion.reply', 'contest', 'training', + ]; + for (const i of t) await task(i, src, report); } global.Hydro.script.migrateVijos = module.exports = migrateVijos; diff --git a/ui/constant/record.js b/ui/constant/record.js index 4091ffe41..867b035f7 100644 --- a/ui/constant/record.js +++ b/ui/constant/record.js @@ -17,9 +17,6 @@ export const STATUS_COMPILING = 21; export const STATUS_FETCHED = 22; export const STATUS_IGNORED = 30; -export const TYPE_SUBMISSION = 0; -export const TYPE_PRETEST = 1; - export const STATUS_TEXTS = { [STATUS_WAITING]: 'Waiting', [STATUS_ACCEPTED]: 'Accepted', @@ -90,9 +87,3 @@ export const STATUS_SCRATCHPAD_SHORT_TEXTS = { [STATUS_RUNTIME_ERROR]: 'RTE', }; attachObjectMeta(STATUS_SCRATCHPAD_SHORT_TEXTS, 'exportToPython', false); - -export const TYPE_TEXTS = { - [TYPE_SUBMISSION]: 'Submission', - [TYPE_PRETEST]: 'Pretest', -}; -attachObjectMeta(TYPE_TEXTS, 'intKey', true);