Skip to content

Commit

Permalink
feat: append competition event before send to judge; allow batch call…
Browse files Browse the repository at this point in the history
…back judge
  • Loading branch information
dreamerblue committed Dec 10, 2024
1 parent bdfd98b commit 665e250
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 76 deletions.
51 changes: 50 additions & 1 deletion src/app/solution/solution.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,58 @@ const solutionContract = {
],
},
eventTimestampUs: { type: 'number' },
batchData: {
type: 'array',
items: {
type: 'object',
properties: {
data: {
anyOf: [
{
type: 'object',
properties: {
type: { type: 'string', enum: ['start'] },
},
additionalProperties: false,
required: ['type'],
},
{
type: 'object',
properties: {
type: { type: 'string', enum: ['progress'] },
current: { type: 'number' },
total: { type: 'number' },
},
additionalProperties: false,
required: ['type', 'current', 'total'],
},
{
type: 'object',
properties: {
type: { type: 'string', enum: ['finish'] },
resultType: {
type: 'string',
enum: ['CompileError', 'SystemError', 'Done'],
},
detail: {
type: 'object',
allowAdditionalProperties: true,
},
},
additionalProperties: false,
required: ['type', 'resultType'],
},
],
},
eventTimestampUs: { type: 'number' },
},
additionalProperties: false,
required: ['data', 'eventTimestampUs'],
},
},
},
additionalProperties: true,
required: ['judgeInfoId', 'solutionId', 'judgerId', 'data', 'eventTimestampUs'],
required: ['judgeInfoId', 'solutionId', 'judgerId'],
} as defContract.ContractSchema,
};

Expand Down
157 changes: 83 additions & 74 deletions src/app/solution/solution.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,29 @@ export default class SolutionController {
result: ESolutionResult.RPD,
});
await this.service.update(newId, { judgeInfoId });
try {
if (competitionId) {
const newSolutionCreateTime = await this.service.getSolutionCreateTime(newId);
await Promise.all([
this.competitionLogService.log(competitionId, ECompetitionLogAction.SubmitSolution, {
solutionId: newId,
problemId,
userId: sess.userId,
}),
this.competitionEventService.event(competitionId, ECompetitionEvent.SubmitSolution, {
solutionId: newId,
problemId,
userId: sess.userId,
judgeInfoId,
detail: {
time: newSolutionCreateTime,
},
}),
]);
}
} catch (e) {
ctx.logger.error('[submitSolution] append competition log or event error:', e);
}
this.service.sendToJudgeQueue({
judgeInfoId,
solutionId: newId,
Expand Down Expand Up @@ -522,27 +545,7 @@ export default class SolutionController {
// code,
// spj: problem.spj,
// });
try {
if (competitionId) {
this.competitionLogService.log(competitionId, ECompetitionLogAction.SubmitSolution, {
solutionId: newId,
problemId,
userId: sess.userId,
});
const newSolutionCreateTime = await this.service.getSolutionCreateTime(newId);
this.competitionEventService.event(competitionId, ECompetitionEvent.SubmitSolution, {
solutionId: newId,
problemId,
userId: sess.userId,
judgeInfoId,
detail: {
time: newSolutionCreateTime,
},
});
}
} catch (e) {
console.error(e);
}

// const REDIS_QUEUE_NAME = 'judge:queue';
// const task = {
// solution_id: `${newId}`,
Expand Down Expand Up @@ -626,6 +629,22 @@ export default class SolutionController {
// }
}
for (const chunk of chunks) {
await Promise.all(
chunk.map((s) => {
if (s.competitionId) {
return this.competitionEventService
.event(s.competitionId, ECompetitionEvent.RejudgeSolution, {
solutionId: s.solutionId,
problemId: s.problemId,
userId: s.userId,
judgeInfoId: judgeInfoIdMap.get(s.solutionId),
})
.then(() => {});
}
return Promise.resolve();
}),
);

await Promise.all(
chunk.map((s) =>
this.service.sendToJudgeQueue({
Expand All @@ -647,22 +666,6 @@ export default class SolutionController {
}),
),
);

await Promise.all(
chunk.map((s) => {
if (s.competitionId) {
return this.competitionEventService
.event(s.competitionId, ECompetitionEvent.RejudgeSolution, {
solutionId: s.solutionId,
problemId: s.problemId,
userId: s.userId,
judgeInfoId: judgeInfoIdMap.get(s.solutionId),
})
.then(() => {});
}
return Promise.resolve();
}),
);
}

return {
Expand All @@ -674,45 +677,51 @@ export default class SolutionController {
@authSystemRequest()
async [routesBe.callbackJudge.i](ctx: Context): Promise<void> {
const req = ctx.request.body as ICallbackJudgeReq;
const { judgeInfoId, solutionId, judgerId, data, eventTimestampUs } = req;
const { judgeInfoId, solutionId, judgerId, batchData } = req;
const redundant = this.lodash.pick(req, ['userId', 'problemId', 'contestId', 'competitionId']);
switch (data.type) {
case 'start': {
await this.service.updateJudgeStart(
judgeInfoId,
solutionId,
judgerId,
eventTimestampUs,
redundant,
);
break;
}
case 'progress': {
await this.service.updateJudgeProgress(
judgeInfoId,
solutionId,
judgerId,
eventTimestampUs,
redundant,
data.current,
data.total,
);
break;
}
case 'finish': {
await this.service.updateJudgeFinish(
judgeInfoId,
solutionId,
judgerId,
eventTimestampUs,
redundant,
data.resultType,
data.detail,
);
break;
const batchDataList = batchData || [
{ data: req.data!, eventTimestampUs: req.eventTimestampUs! },
];
for (const item of batchDataList) {
const { data, eventTimestampUs } = item;
switch (data.type) {
case 'start': {
await this.service.updateJudgeStart(
judgeInfoId,
solutionId,
judgerId,
eventTimestampUs,
redundant,
);
break;
}
case 'progress': {
await this.service.updateJudgeProgress(
judgeInfoId,
solutionId,
judgerId,
eventTimestampUs,
redundant,
data.current,
data.total,
);
break;
}
case 'finish': {
await this.service.updateJudgeFinish(
judgeInfoId,
solutionId,
judgerId,
eventTimestampUs,
redundant,
data.resultType,
data.detail,
);
break;
}
default:
throw new ReqError(Codes.GENERAL_ILLEGAL_REQUEST);
}
default:
throw new ReqError(Codes.GENERAL_ILLEGAL_REQUEST);
}
}
}
2 changes: 1 addition & 1 deletion src/common
Submodule common updated 1 files
+21 −2 contracts/solution.ts

0 comments on commit 665e250

Please sign in to comment.