Browse Source

Add migration script to build statistics table

pull/6/head
Menci 6 years ago
parent
commit
e7690a1065
  1. 6
      app.js
  2. 37
      migrates/build-statistics.js

6
app.js

@ -7,7 +7,7 @@ const fs = require('fs'),
commandLineArgs = require('command-line-args');
const optionDefinitions = [
{ name: 'config', alias: 'c', type: String, defaultValue: './config.json' },
{ name: 'config', alias: 'c', type: String, defaultValue: __dirname + '/config.json' },
];
const options = commandLineArgs(optionDefinitions);
@ -174,7 +174,7 @@ global.syzoj = {
});
},
loadModules() {
fs.readdir('./modules/', (err, files) => {
fs.readdir(__dirname + '/modules/', (err, files) => {
if (err) {
this.log(err);
return;
@ -268,4 +268,4 @@ global.syzoj = {
}
};
syzoj.run();
syzoj.untilStarted = syzoj.run();

37
migrates/build-statistics.js

@ -0,0 +1,37 @@
/*
* This script will help you build submission_statistics table. SYZOJ changed previous
* way of querying every time to cache statistics in database and update it for every
* judged submission. Without running this script after migrating will cause old submissions
* disappear from statistics.
*
*/
const fn = async () => {
require('..');
await syzoj.untilStarted;
const User = syzoj.model('user');
const Problem = syzoj.model('problem');
const JudgeState = syzoj.model('judge_state');
const userIDs = (await User.createQueryBuilder().select('id').getRawMany()).map(record => record.id);
for (const id of userIDs) {
const problemIDs = (await JudgeState.createQueryBuilder()
.select('DISTINCT(problem_id)', 'problem_id')
.where('status = :status', { status: 'Accepted' })
.andWhere('user_id = :user_id', { user_id: id })
.andWhere('type = 0')
.getRawMany()).map(record => record.problem_id);
for (const problemID of problemIDs) {
const problem = await Problem.findById(problemID);
await problem.updateStatistics(id);
console.log(`userID = ${id}, problemID = ${problemID}`);
}
}
process.exit();
};
// NOTE: Uncomment to run.
fn();
Loading…
Cancel
Save