Commit bed475a3 authored by Yechang's avatar Yechang
Browse files

fix(feedback): fix penalty error

parent 3102797f
Loading
Loading
Loading
Loading
Loading
+20 −32
Original line number Diff line number Diff line
@@ -7,18 +7,18 @@ vi.mock('$lib/server/db')
describe('create feedback', async () => {
    test('calculate penalty', async () => {
        const userId = 233
        const problemId = 450
        const problem = {
            id: 450,
            penalty: 5,
            penaltyAfter: 2

        }
        const submissions = [{
            id: 1,
            createdAt: new Date(),
            name: "x",
            problemId,
            problem: [
                {
                    penalty: 5,
                    penaltyAfter: 2
                }
            ],
            problemId: problem.id,
            problem,
            submitterId: userId,
            volumeId: "x",
            feedback: {
@@ -29,13 +29,8 @@ describe('create feedback', async () => {
            id: 2,
            createdAt: new Date(),
            name: "x",
            problemId,
            problem: [
                {
                    penalty: 5,
                    penaltyAfter: 2
                }
            ],
            problemId: problem.id,
            problem,
            submitterId: userId,
            volumeId: "x",
            feedback: {
@@ -46,29 +41,20 @@ describe('create feedback', async () => {
            id: 3,
            createdAt: new Date(),
            name: "x",
            problemId,
            problem: [
                {
                    penalty: 5,
                    penaltyAfter: 2
                }
            ],
            problemId: problem.id,
            problem,
            submitterId: userId,
            volumeId: "x",
            feedback: {

                isValid: true, score: 80
                isValid: true,
                score: 80
            }
        }, {
            id: 4,
            createdAt: new Date(),
            name: "x",
            problemId,
            problem: {
                id: problemId,
                penalty: 5,
                penaltyAfter: 2
            },
            problemId: problem.id,
            problem,
            submitterId: userId,
            volumeId: "x",
            feedback: {
@@ -77,7 +63,9 @@ describe('create feedback', async () => {
        }]
        db.submission.findUnique.mockResolvedValue(submissions[3])
        db.submission.findMany.mockResolvedValue(submissions)
        const penalty = await fixAndCalculatePenalty(4)
        expect(penalty).toStrictEqual(10)
        expect(await fixAndCalculatePenalty(1)).toStrictEqual(0)
        expect(await fixAndCalculatePenalty(2)).toStrictEqual(0)
        expect(await fixAndCalculatePenalty(3)).toStrictEqual(5)
        expect(await fixAndCalculatePenalty(4)).toStrictEqual(10)
    })
})
 No newline at end of file
+36 −25
Original line number Diff line number Diff line
@@ -40,9 +40,6 @@ export const fixAndCalculatePenalty = async (currentSubmissionId: number): Promi

    const submissions = await db.submission.findMany({
        where: {
            id: {
                lte: submission.id
            },
            submitterId: submission.id,
            problemId: problem.id
        },
@@ -54,24 +51,35 @@ export const fixAndCalculatePenalty = async (currentSubmissionId: number): Promi
        }
    })

    let num = 0;
    const cal = (n: number) => (_.max([n - penaltyAfter, 0]) || 0) * penalty;
    let num = 0, res = null;
    const cal = (n: number) => (_.max([n - penaltyAfter + 1, 0]) || 0) * penalty;
    for (const s of submissions) {
        if (_.isNil(s.feedback)) {
            // no feedback
            return null
        const penaltyScore = cal(num)
        if (s.id == currentSubmissionId) {
            res = penaltyScore
            continue
        }

        const { feedback } = s
        num += feedback.isValid ? 1 : 0;
        if (feedback.score >= 0) {
            // already had score
            continue
        if (_.isNil(feedback)) {
            break
        }
        const penaltyScore = cal(num)
        const originalScore = await getFeedbackScore(s.feedback)
        if (feedback.score === -1) {
            const originalScore = await getFeedbackScore(feedback)
            const finalScore = _.max([originalScore - penaltyScore, 0]) || 0;

        await db.submissionFeedback.update({
            await db.$transaction(async (tx) => {
                await tx.gradeRecord.create({
                    data: {
                        grade: finalScore,
                        source: {
                            type: "submission",
                            id: submission.id
                        },
                        studentId: submission.submitterId,
                        groupId: submission.problem.gradeGroupId,
                    }
                })
                await tx.submissionFeedback.update({
                    where: {
                        id: feedback.id
                    },
@@ -79,6 +87,9 @@ export const fixAndCalculatePenalty = async (currentSubmissionId: number): Promi
                        score: finalScore
                    }
                })
            })
        }
        num += feedback.isValid ? 1 : 0;
    }
    return cal(num + 1)
    return res
}
 No newline at end of file
+0 −1
Original line number Diff line number Diff line
import { db } from '$lib/server/db'
import { fixAndCalculatePenalty } from '$lib/server/db/feedback.js'
import { log } from '$lib/server/log'
import { SlotType, type SlotValue } from '$lib/server/oj'
import { redis } from '$lib/server/redis'
+24 −41
Original line number Diff line number Diff line
@@ -158,25 +158,9 @@ export const PUT = async ({ request, params }) => {
            return new Response(null, { status: 201 })
        }

        const penalty = await fixAndCalculatePenalty(task.submissionId)
        const score = _.isNil(penalty) ? -1 : (_.max([res.score - penalty, 0]) || 0)
        const feedback = await db.$transaction(async (tx) => {
            if (score >= 0) {
                await tx.gradeRecord.create({
                    data: {
                        grade: score,
                        source: {
                            type: "submission",
                            id: task.submissionId
                        },
                        studentId: task.submission.submitterId,
                        groupId: task.submission.problem.gradeGroupId,
                    }
                })
            }
            return await tx.submissionFeedback.create({
        const feedback = await db.submissionFeedback.create({
            data: {
                    score,
                score: -1,
                description: res.status,
                descriptionHTML: "",
                content: [
@@ -200,8 +184,7 @@ export const PUT = async ({ request, params }) => {
                isValid: true
            }
        })
        })

        await fixAndCalculatePenalty(task.submissionId)
    }
    return new Response(null, { status: 201 })
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -57,7 +57,7 @@ export const load = (async ({ parent }) => {
        }))
        return {
            problemId: p.id,
            results
            results: results.filter(x => (x.result?.length || 0) > 0)
        }
    }))