Skip to content

Commit

Permalink
add question case code
Browse files Browse the repository at this point in the history
  • Loading branch information
greywen committed Aug 9, 2022
1 parent ef168de commit e9a5250
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 59 deletions.
25 changes: 4 additions & 21 deletions src/controllers/code.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,12 @@ export class CodeController {
}

async runByCaseImplement(body: ICodeRunBody) {
const { code, questionId, languageId, once } = body;

const question = await this.codeService.getQuestion(questionId);
const language = await this.codeService.getLanguage(languageId);
const result = [];

for (const testcase of question.cases) {
const codeCommand = await this.codeService.prepareRunCommand(
language,
code,
question.entry,
testcase,
);

const _result = await this.codeService.run(languageId, codeCommand);
result.push(_result);

if (once || !_result.isSuccess) {
break;
}
const { code, questionId, languageId } = body;
if (!languageId || !questionId || !code) {
return '';
}

return result;
return await this.codeService.runCodeByCase(body);
}

@Post('run')
Expand Down
11 changes: 10 additions & 1 deletion src/dtos/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ export interface IQuestionDto {
code: string;
}

export interface ICodeResult {
export interface ICodeRunResult {
isSuccess: boolean;
data: any;
}

export interface IRunCaseResult {
comments: string;
input: string;
output: string;
codeOutput?: string;
elapsedTime?: string;
logs?: string;
}
5 changes: 3 additions & 2 deletions src/entities/questionBank.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class QuestionBank {
}

export class IQuestionCase {
case: any;
assert: any;
comments: string;
input: any;
output: any;
}
89 changes: 54 additions & 35 deletions src/services/code.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import config from '@config/config';
import { ICodeResult } from '@dtos/code';
import { ICodeRunResult, ICodeRunBody, IRunCaseResult } from '@dtos/code';
import { IQuestionCase, QuestionBank } from '@entities/questionBank.entity';
import { LanguageModel } from '@models/language.model';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { httpGet, httpPost } from '@utils/httpRequest';
import { matchCaseResult } from '@utils/utils';
import { Repository } from 'typeorm';

@Injectable()
Expand All @@ -31,47 +32,65 @@ export class CodeService {
);
}

prepareCodeCommands(language: LanguageModel, code: string) {
const commands = `${language.beforInjectionCodeCmd}
code="${code.replace(/"/g, '\\"')}"
cat <<< "$code" > ${language.fileName}
${language.afterInjectionCodeCmd}
`;
return commands;
}

async prepareTestCaseCode(
entry: string,
cases: IQuestionCase,
captureCode: string,
) {
const testCaseCode = captureCode
.replace('${entry}', entry)
.replace('${case}', cases.case);
return testCaseCode;
}

async prepareRunCommand(
async prepareCodeCase(
language: LanguageModel,
code: string,
entry: string,
cases: IQuestionCase,
) {
const testCaseCode = await this.prepareTestCaseCode(
entry,
cases,
language.captureCode,
);
const testCaseCode = language.captureCode
.replace('${entry}', entry)
.replace('${input}', cases.input);
code += testCaseCode;
return await this.prepareCodeCommands(language, code);
return code;
}

async run(languageId: number, code: string): Promise<any> {
return await httpPost<ICodeResult>(`${config.services.codeService}/run`, {
body: JSON.stringify({
languageId: languageId,
code: code,
}),
});
extractCaseResult(value: string) {
value = value ?? '';
return {
elapsedTime: matchCaseResult('ElapsedTime', value),
codeOutput: matchCaseResult('Output', value),
};
}

async runCodeByCase(params: ICodeRunBody) {
const { code, questionId, languageId, once } = params;

const question = await this.getQuestion(questionId);
const language = await this.getLanguage(languageId);

const result = [] as IRunCaseResult[];
for (const testcase of question.cases) {
const codeCommand = await this.prepareCodeCase(
language,
code,
question.entry,
testcase,
);

const _result = await this.run(languageId, codeCommand);
result.push({
...testcase,
...this.extractCaseResult(_result.data),
logs: _result.data,
});

if (once || !_result.isSuccess) {
break;
}
}
return result;
}

async run(languageId: number, code: string): Promise<ICodeRunResult> {
return await httpPost<ICodeRunResult>(
`${config.services.codeService}/run`,
{
body: JSON.stringify({
languageId: languageId,
code: code,
}),
},
);
}
}
6 changes: 6 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ const timeSleep = (time: number): Promise<null> => {
export const sleep = (second: number) => {
return timeSleep(second * 1000);
};

export const matchCaseResult = (key: string, value: string) => {
const matchList = value.match(new RegExp(`${key}: (.+?)\n`));
if (matchList?.length > 1) return matchList[1];
return null;
};

0 comments on commit e9a5250

Please sign in to comment.