From bccca2f5d231cb2ff7f4944cccca1b6bc6006f71 Mon Sep 17 00:00:00 2001 From: undefined Date: Tue, 1 Oct 2024 09:09:01 +0800 Subject: [PATCH] core: improve daily task performance --- packages/hydrooj/src/handler/problem.ts | 1 - packages/hydrooj/src/interface.ts | 2 -- packages/hydrooj/src/script/problemStat.ts | 38 ++++++++++------------ 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/packages/hydrooj/src/handler/problem.ts b/packages/hydrooj/src/handler/problem.ts index 2a965dd07..cf9bb97df 100644 --- a/packages/hydrooj/src/handler/problem.ts +++ b/packages/hydrooj/src/handler/problem.ts @@ -506,7 +506,6 @@ export class ProblemSubmitHandler extends ProblemDetailHandler { if (!pretest) { await Promise.all([ problem.inc(domainId, this.pdoc.docId, 'nSubmit', 1), - problem.incStatus(domainId, this.pdoc.docId, this.user._id, 'nSubmit', 1), domain.incUserInDomain(domainId, this.user._id, 'nSubmit'), tid && contest.updateStatus(domainId, tid, this.user._id, rid, this.pdoc.docId), ]); diff --git a/packages/hydrooj/src/interface.ts b/packages/hydrooj/src/interface.ts index e151c9189..eec84a52d 100644 --- a/packages/hydrooj/src/interface.ts +++ b/packages/hydrooj/src/interface.ts @@ -294,8 +294,6 @@ export interface ProblemStatusDoc extends StatusDocBase { rid?: ObjectId; score?: number; status?: number; - nSubmit?: number; - nAccept?: number; star?: boolean; } diff --git a/packages/hydrooj/src/script/problemStat.ts b/packages/hydrooj/src/script/problemStat.ts index b47eed464..94c81313e 100644 --- a/packages/hydrooj/src/script/problemStat.ts +++ b/packages/hydrooj/src/script/problemStat.ts @@ -10,7 +10,13 @@ const sumStatus = (status) => ({ $sum: { $cond: [{ $eq: ['$status', status] }, 1 export async function udoc(report) { report({ message: 'Udoc' }); const pipeline = [ - { $match: { contest: { $nin: [RecordModel.RECORD_PRETEST, RecordModel.RECORD_GENERATE] } } }, + { + $match: { + contest: { $nin: [RecordModel.RECORD_PRETEST, RecordModel.RECORD_GENERATE] }, + status: { $ne: STATUS.STATUS_CANCELED }, + uid: { $gte: 0 }, + }, + }, { $group: { _id: { domainId: '$domainId', pid: '$pid', uid: '$uid' }, @@ -46,23 +52,6 @@ export async function udoc(report) { if (bulk.batches.length) await bulk.execute(); } -export async function psdoc(report) { - report({ message: 'Psdoc' }); - const pipeline = [ - { $match: { contest: { $nin: [RecordModel.RECORD_PRETEST, RecordModel.RECORD_GENERATE] } } }, - { - $group: { - _id: { domainId: '$domainId', pid: '$pid', uid: '$uid' }, - nSubmit: { $sum: 1 }, - }, - }, - ]; - const data = db.collection('record').aggregate(pipeline, { allowDiskUse: true }); - for await (const adoc of data) { - await document.setStatus(adoc._id.domainId, document.TYPE_PROBLEM, adoc._id.pid, adoc._id.uid, { nSubmit: adoc.nSubmit }); - } -} - export async function pdoc(report) { report({ message: 'Pdoc' }); const pipeline = [ @@ -157,9 +146,16 @@ export const apply = (ctx) => ctx.addScript( psdoc: Schema.boolean(), }), async (arg, report) => { - if (arg.pdoc === undefined || arg.pdoc) await pdoc(report); - if (arg.udoc === undefined || arg.udoc) await udoc(report); - if (arg.psdoc === undefined || arg.psdoc) await psdoc(report); + if (arg.pdoc === undefined || arg.pdoc) { + const start = Date.now(); + await pdoc(report); + report({ message: `pdoc finished in ${Date.now() - start}ms` }); + } + if (arg.udoc === undefined || arg.udoc) { + const start = Date.now(); + await udoc(report); + report({ message: `udoc finished in ${Date.now() - start}ms` }); + } return true; }, );