测试:从vj4的自动迁移脚本(未完成)

This commit is contained in:
undefined committed 2020-05-10 18:57:03 +08:00
1 parent cedb427dc9
commit 41125c6929
11 files changed
+185 -24

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
const {
Route, Connection, Handler, ConnectionHandler,
} = global.Hydro.service.server;
const { PERM_MANAGE } = global.Hydro.permission;
const migrate = global.Hydro.script.migrateVijos;
class MigrateVijosHandler extends Handler {
async prepare() {
this.checkPerm(PERM_MANAGE);
}
async get() {
const path = [
['Hydro', '/'],
['migrate_vijos', null],
];
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 function apply() {
Route('/migrate/vijos', module.exports.MigrateVijosHandler);
Connection('/migrate/vijos-conn', module.exports.MigrateVijosConnectionHandler);
}
global.Hydro.handler.pastebin = module.exports = {
MigrateVijosHandler, MigrateVijosConnectionHandler, apply,
};
+5
View File
@@ -0,0 +1,5 @@
{
"name": "migrate-vijos",
"version": "0.0.1",
"description": "Migrate From Vijos"
}
+87
View File
@@ -0,0 +1,87 @@
/* eslint-disable no-await-in-loop */
const Mongo = global.Hydro.nodeModules.mongodb;
const { ObjectID } = global.Hydro.nodeModules.bson;
const dst = global.Hydro.service.db;
function parseSolution(doc) {
return {
_id: doc._id,
title: doc.title,
content: doc.content,
vote: doc.vote,
};
}
async function problem(src, report) {
const count = await src.collection('document').find({ doc_type: 10 }).count();
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)
.toArray();
const problems = [];
for (const doc of docs) {
problems.push({
_id: doc._id,
pid: doc.doc_id || new ObjectID(),
title: doc.title,
content: doc.content,
owner: doc.owner_uid,
data: doc.data,
category: doc.category,
tag: doc.tag,
nSubmit: doc.num_submit,
nAccept: doc.num_accept,
});
}
await dst.collection('problem').insertMany(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.` });
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)
.toArray();
const contests = [];
for (const doc of docs) {
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: doc.pids,
attend: doc.attend,
});
}
await dst.collection('contests').insertMany(contests);
}
}
async function migrateVijos({
host, port, name, username, password,
}, report = () => { }) {
let mongourl = 'mongodb://';
if (username) mongourl += `${username}:${password}@`;
mongourl += `${host}:${port}/${name}`;
const Database = await Mongo.MongoClient.connect(mongourl, {
useNewUrlParser: true, useUnifiedTopology: true,
});
const src = Database.db(name);
report({ progress: 0, message: 'Database connected.' });
dst.collection('system').insertOne({
_id: 'user',
value: (await src.collection('system').findOne({ _id: 'user_counter' })).value,
});
report({ progress: 1, message: 'Collection:system done.' });
await problem(src, report);
await contest(src, report);
}
global.Hydro.script.migrateVijos = module.exports = migrateVijos;