From e7690a10650a1bda41dd42ec70049b63ce52645e Mon Sep 17 00:00:00 2001 From: Menci Date: Fri, 26 Apr 2019 03:01:49 +0800 Subject: [PATCH] Add migration script to build statistics table --- app.js | 6 +++--- migrates/build-statistics.js | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 migrates/build-statistics.js diff --git a/app.js b/app.js index f99892f..a55da2b 100644 --- a/app.js +++ b/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(); diff --git a/migrates/build-statistics.js b/migrates/build-statistics.js new file mode 100644 index 0000000..34daac2 --- /dev/null +++ b/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();