Skip to content

Commit

Permalink
Merge pull request #173 from boostcampwm2023/feature-be-#172
Browse files Browse the repository at this point in the history
[BE] feat#172 session log ์Šคํ‚ค๋งˆ ์ˆ˜์ •
  • Loading branch information
flydog98 authored Nov 29, 2023
2 parents e49e1c7 + 40b840e commit 8bbb474
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
20 changes: 9 additions & 11 deletions packages/backend/src/quizzes/quizzes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Inject,
Delete,
UseGuards,
NotFoundException,
} from '@nestjs/common';
import {
ApiTags,
Expand Down Expand Up @@ -136,10 +137,11 @@ export class QuizzesController {
execCommandDto.message,
));
} else if (execCommandDto.mode === MODE.EDITOR) {
const recentCommand = await this.sessionService.getRecentLog(
sessionId,
id,
);
const { mode: recentMode, message: recentMessage } =
await this.sessionService.getRecentLog(sessionId, id);
if (recentMode === MODE.EDITOR) {
throw new NotFoundException('ํŽธ์ง‘๊ธฐ ๋ช…๋ น ์ฐจ๋ก€๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค');
}

const bodyPreview =
execCommandDto.message.length > 15
Expand All @@ -148,12 +150,12 @@ export class QuizzesController {

this.logger.log(
'info',
`running editor command "${recentCommand}" for container ${containerId} with body starts with "${bodyPreview}"`,
`running editor command "${recentMessage}" for container ${containerId} with body starts with "${bodyPreview}"`,
);

({ message, result } = await this.containerService.runEditorCommand(
containerId,
recentCommand,
recentMessage,
execCommandDto.message,
));
} else {
Expand All @@ -163,11 +165,7 @@ export class QuizzesController {
}

// ์ผ๋‹จ editor์ผ ๋•Œ๋„ message๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
this.sessionService.pushLogBySessionId(
execCommandDto.message,
sessionId,
id,
);
this.sessionService.pushLogBySessionId(execCommandDto, sessionId, id);

response.status(HttpStatus.OK).send({
message,
Expand Down
28 changes: 20 additions & 8 deletions packages/backend/src/session/schema/session.schema.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

const Action = {
Command: 'command',
Editor: 'editor',
} as const;

export type ActionType = (typeof Action)[keyof typeof Action];

@Schema({ timestamps: true })
export class Session extends Document {
// @Prop({ required: true })
// createdAt: Date;
//
// @Prop({ required: true })
// updatedAt: Date;

@Prop()
deletedAt: Date | null;

Expand All @@ -17,15 +18,26 @@ export class Session extends Document {
type: Map,
of: {
status: { type: String, required: true },
logs: { type: [String], required: true },
logs: {
type: [
{
mode: { type: String, enum: Object.values(Action), required: true },
message: { type: String, required: true },
},
],
required: true,
},
containerId: { type: String, default: '' },
},
})
problems: Map<
number,
{
status: string;
logs: string[];
logs: {
mode: ActionType;
message: string;
}[];
containerId: string;
}
>;
Expand Down
11 changes: 7 additions & 4 deletions packages/backend/src/session/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Inject, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Logger } from 'winston';
import { Model } from 'mongoose';
import { Session } from './schema/session.schema';
import { ActionType, Session } from './schema/session.schema';
import { ObjectId } from 'typeorm';

@Injectable()
Expand Down Expand Up @@ -71,7 +71,10 @@ export class SessionService {
session.save();
}

async getRecentLog(sessionId: string, problemId: number): Promise<string> {
async getRecentLog(
sessionId: string,
problemId: number,
): Promise<{ mode: string; message: string }> {
const session = await this.getSessionById(sessionId);

const problemLogs = session?.problems.get(problemId)?.logs;
Expand All @@ -83,15 +86,15 @@ export class SessionService {
}

async pushLogBySessionId(
command: string,
log: { mode: ActionType; message: string },
sessionId: string,
problemId: number,
): Promise<void> {
const session = await this.getSessionById(sessionId);
if (!session.problems.get(problemId)) {
throw new Error('problem not found');
}
session.problems.get(problemId).logs.push(command);
session.problems.get(problemId).logs.push(log);
session.save();
}

Expand Down

0 comments on commit 8bbb474

Please sign in to comment.