From 8cc93d5745602dc621c2005b26239228c184f85b Mon Sep 17 00:00:00 2001 From: Menci Date: Mon, 20 May 2019 09:24:53 +0800 Subject: [PATCH] Fix statistics bugs --- models/problem.ts | 41 +++++++++++++++++++++++++++++------------ views/problem.ejs | 2 +- views/statistics.ejs | 8 ++++---- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/models/problem.ts b/models/problem.ts index ebe8e5c..61e4e46 100644 --- a/models/problem.ts +++ b/models/problem.ts @@ -356,7 +356,10 @@ export default class Problem extends Model { .getRawMany(); const resultRow = result[0]; - if (!resultRow || resultRow[column] == null) return; + let toDelete = false; + if (!resultRow || resultRow[column] == null) { + toDelete = true; + } const baseColumns = { user_id, @@ -365,6 +368,15 @@ export default class Problem extends Model { }; let record = await SubmissionStatistics.findOne(baseColumns); + + if (toDelete) { + if (record) { + await record.destroy(); + } + + return; + } + if (!record) { record = SubmissionStatistics.create(baseColumns); } @@ -378,6 +390,10 @@ export default class Problem extends Model { } async countStatistics(type) { + if (!statisticsTypes[type] || this.type === ProblemType.SubmitAnswer && statisticsCodeOnly.includes(type)) { + return null; + } + return await SubmissionStatistics.count({ problem_id: this.id, type: type @@ -385,9 +401,11 @@ export default class Problem extends Model { } async getStatistics(type, paginate) { - const entityManager = TypeORM.getManager(); + if (!statisticsTypes[type] || this.type === ProblemType.SubmitAnswer && statisticsCodeOnly.includes(type)) { + return null; + } - let statistics = { + const statistics = { type: type, judge_state: null, scoreDistribution: null, @@ -409,15 +427,14 @@ export default class Problem extends Model { .getMany() : []; - JudgeState.createQueryBuilder() - .select('score') - .addSelect('COUNT(*)', 'count') - .where('problem_id = :problem_id', { problem_id: this.id }) - .andWhere('type = 0') - .andWhere('pending = false') - .groupBy('score') - .getRawMany() - let a = (await entityManager.query('SELECT `score`, COUNT(*) AS `count` FROM `judge_state` WHERE `problem_id` = __PROBLEM_ID__ AND `type` = 0 AND `pending` = 0 GROUP BY `score`'.replace('__PROBLEM_ID__', this.id.toString()))); + const a = await JudgeState.createQueryBuilder() + .select('score') + .addSelect('COUNT(*)', 'count') + .where('problem_id = :problem_id', { problem_id: this.id }) + .andWhere('type = 0') + .andWhere('pending = false') + .groupBy('score') + .getRawMany(); let scoreCount = []; for (let score of a) { diff --git a/views/problem.ejs b/views/problem.ejs index 6f4ea60..1b29904 100644 --- a/views/problem.ejs +++ b/views/problem.ejs @@ -92,7 +92,7 @@ if (contest) { 提交 <% } %> 提交记录 - 统计 + 统计 测试数据 <% if (problem.additional_file) { %> 附加文件 diff --git a/views/statistics.ejs b/views/statistics.ejs index 8166f12..331a84d 100644 --- a/views/statistics.ejs +++ b/views/statistics.ejs @@ -3,11 +3,11 @@ this.title = '统计'; let types = { fastest: problem.type === 'submit-answer' ? null : '最快', slowest: problem.type === 'submit-answer' ? null : '最慢', - shortest: problem.type === 'submit-answer' ? null : '最短', - longest: problem.type === 'submit-answer' ? null : '最长', + shortest: problem.type === 'submit-answer' ? '最小' : '最短', + longest: problem.type === 'submit-answer' ? '最大' : '最长', earliest: '最早', - min: problem.type === 'submit-answer' ? '最小' : '最小内存', - max: problem.type === 'submit-answer' ? '最大' : '最大内存' + min: problem.type === 'submit-answer' ? null : '最小内存', + max: problem.type === 'submit-answer' ? null : '最大内存' }; %> <% include header %>