Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

スコア算出のリファクタリング #64

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/app/api/minio/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function POST(req: NextRequest) {
userId: user?.id || 0,
};

const score = await scoreRegister(scoreData);
const score = await scoreRegister(scoreData, assignmentId);

const response: ScoreResponse = {
text: caption || "",
Expand Down
14 changes: 10 additions & 4 deletions app/src/functions/scoreRegister.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { prisma } from "@/lib/prisma";
import type { Score, ScoreData } from "@/types";

const assignmentDate = async function GET() {
const assignmentDate = async function GET(assignmentId: number) {
const assignment = await prisma.assignment.findFirst({
where: { id: assignmentId },
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで取ってくる課題が違ったので修正

select: { date: true },
});
const date = assignment?.date ?? null;
Expand All @@ -13,7 +14,10 @@ const assignmentDate = async function GET() {
});
};

export const scoreRegister = async (scoreData: ScoreData) => {
export const scoreRegister = async (
scoreData: ScoreData,
assignmentId: number,
) => {
// 必須データの存在確認
if (
scoreData.similarity == null ||
Expand All @@ -22,23 +26,25 @@ export const scoreRegister = async (scoreData: ScoreData) => {
scoreData.assignmentId == null ||
scoreData.userId == null
) {
return
return;
}

// ポイントの計算
const minTime = 60;
const maxTime = 3600;

const response = await assignmentDate();
const response = await assignmentDate(assignmentId);
const assignmentDateValue = await response.json();

const answerIntervalTime =
new Date(scoreData.answerTime).getTime() -
new Date(assignmentDateValue.date).getTime();

const normalizedTime = Math.max(
0,
Math.min(1, (answerIntervalTime / 1000 - minTime) / (maxTime - minTime)),
);

const point = scoreData.similarity * 70 + (1 - normalizedTime) * 30;

const score: Score = await prisma.score.create({
Expand Down
Loading