diff --git a/apps/backend/apps/client/src/submission/submission-sub.service.ts b/apps/backend/apps/client/src/submission/submission-sub.service.ts index 214818add5..4508c65a3c 100644 --- a/apps/backend/apps/client/src/submission/submission-sub.service.ts +++ b/apps/backend/apps/client/src/submission/submission-sub.service.ts @@ -10,7 +10,7 @@ import type { Cache } from 'cache-manager' import { plainToInstance } from 'class-transformer' import { validateOrReject, ValidationError } from 'class-validator' import { Span } from 'nestjs-otel' -import { testKey } from '@libs/cache' +import { testKey, userTestcasesKey } from '@libs/cache' import { CONSUME_CHANNEL, EXCHANGE, @@ -76,28 +76,36 @@ export class SubmissionSubscriptionService implements OnModuleInit { } async handleRunMessage(msg: JudgerResponse, userId: number): Promise { - const key = testKey(userId) const status = Status(msg.resultCode) const testcaseId = msg.judgeResult?.testcaseId const output = this.parseError(msg, status) - - const testcases = - (await this.cacheManager.get< - { - id: number - result: ResultStatus - output?: string - }[] - >(key)) ?? [] - - testcases.forEach((tc) => { - if (!testcaseId || tc.id === testcaseId) { - tc.result = status - tc.output = output + if (!testcaseId) { + const key = userTestcasesKey(userId) + const testcaseIds = (await this.cacheManager.get(key)) ?? [] + + for (const testcaseId of testcaseIds) { + await this.cacheManager.set( + testKey(userId, testcaseId), + { id: testcaseId, result: status, output }, + TEST_SUBMISSION_EXPIRE_TIME + ) } - }) + return + } + + const key = testKey(userId, testcaseId) + const testcase = await this.cacheManager.get<{ + id: number + result: ResultStatus + output?: string + }>(key) + if (testcase) { + testcase.id = testcaseId + testcase.result = status + testcase.output = output + } - await this.cacheManager.set(key, testcases, TEST_SUBMISSION_EXPIRE_TIME) + await this.cacheManager.set(key, testcase, TEST_SUBMISSION_EXPIRE_TIME) } parseError(msg: JudgerResponse, status: ResultStatus): string { diff --git a/apps/backend/apps/client/src/submission/submission.service.ts b/apps/backend/apps/client/src/submission/submission.service.ts index 3670858765..b6c0d88759 100644 --- a/apps/backend/apps/client/src/submission/submission.service.ts +++ b/apps/backend/apps/client/src/submission/submission.service.ts @@ -14,7 +14,7 @@ import { AxiosRequestConfig } from 'axios' import { Cache } from 'cache-manager' import { plainToInstance } from 'class-transformer' import { Span } from 'nestjs-otel' -import { testKey } from '@libs/cache' +import { testKey, userTestcasesKey } from '@libs/cache' import { MIN_DATE, OPEN_SPACE_ID, @@ -375,23 +375,16 @@ export class SubmissionService { } }) - const testcases: { - id: number - result: ResultStatus - }[] = [] - - for (const testcase of rawTestcases) { - testcases.push({ - id: testcase.id, - result: 'Judging' - }) + const testcaseIds: number[] = [] + for (const rawTestcase of rawTestcases) { + await this.cacheManager.set( + testKey(userId, rawTestcase.id), + { id: rawTestcase.id, result: 'Judging' }, + TEST_SUBMISSION_EXPIRE_TIME + ) + testcaseIds.push(rawTestcase.id) } - - await this.cacheManager.set( - testKey(userId), - testcases, - TEST_SUBMISSION_EXPIRE_TIME - ) + await this.cacheManager.set(userTestcasesKey(userId), testcaseIds) await this.publish.publishJudgeRequestMessage( submissionDto.code, @@ -401,14 +394,23 @@ export class SubmissionService { } async getTestResult(userId: number) { - const key = testKey(userId) - return await this.cacheManager.get< - { + const testCasesKey = userTestcasesKey(userId) + const testcases = + (await this.cacheManager.get(testCasesKey)) ?? [] + + const results: { id: number; result: ResultStatus; output?: string }[] = [] + for (const testcaseId of testcases) { + const key = testKey(userId, testcaseId) + const testcase = await this.cacheManager.get<{ id: number result: ResultStatus output?: string - }[] - >(key) + }>(key) + if (testcase) { + results.push(testcase) + } + } + return results } @Span() diff --git a/apps/backend/libs/cache/src/keys.ts b/apps/backend/libs/cache/src/keys.ts index 16c0416b54..c067771bdf 100644 --- a/apps/backend/libs/cache/src/keys.ts +++ b/apps/backend/libs/cache/src/keys.ts @@ -9,4 +9,6 @@ export const joinGroupCacheKey = (groupId: number) => `group:${groupId}` export const invitationCodeKey = (code: string) => `invite:${code}` export const invitationGroupKey = (groupId: number) => `invite:to:${groupId}` -export const testKey = (userId: number) => `test:user:${userId}` +export const testKey = (userId: number, testcaseId: number) => + `test:user:${userId}:testcase:${testcaseId}` +export const userTestcasesKey = (userId: number) => `test:user:${userId}`