Skip to content
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
50 changes: 50 additions & 0 deletions src/features/votes/services/vote_statistics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TaskGrade } from '@prisma/client';

import {
getVoteGradeStatistics,
getVoteGradeStatisticsForTaskIds,
getAllTasksWithVoteInfo,
getVoteCountersByTaskId,
getVoteStatsByTaskId,
Expand Down Expand Up @@ -290,3 +291,52 @@ describe('getAllVoteCounters', () => {
expect(prisma.votedGradeCounter.findMany).toHaveBeenCalledWith();
});
});

describe('getVoteGradeStatisticsForTaskIds', () => {
test('returns an empty Map without querying the DB when taskIds is empty', async () => {
const result = await getVoteGradeStatisticsForTaskIds([]);

expect(result.size).toBe(0);
expect(prisma.votedGradeStatistics.findMany).not.toHaveBeenCalled();
});

test('queries with a WHERE IN filter for the given taskIds', async () => {
mockVotedGradeStatisticsFindMany([]);

await getVoteGradeStatisticsForTaskIds(['abc001_a', 'abc002_a']);

expect(prisma.votedGradeStatistics.findMany).toHaveBeenCalledWith({
where: { taskId: { in: ['abc001_a', 'abc002_a'] } },
});
});

test('returns an empty Map when no matching statistics exist', async () => {
mockVotedGradeStatisticsFindMany([]);

const result = await getVoteGradeStatisticsForTaskIds(['abc001_a']);

expect(result.size).toBe(0);
});

test('maps each taskId to its statistics record', async () => {
const stat = makeStatisticsRecord({ taskId: 'abc001_a', grade: TaskGrade.Q5 });
mockVotedGradeStatisticsFindMany([stat]);

const result = await getVoteGradeStatisticsForTaskIds(['abc001_a']);

expect(result.get('abc001_a')?.grade).toBe(TaskGrade.Q5);
});

test('returns only statistics for the specified taskIds', async () => {
const records = [
makeStatisticsRecord({ taskId: 'abc001_a', grade: TaskGrade.Q5 }),
makeStatisticsRecord({ id: 'stats-abc002_a', taskId: 'abc002_a', grade: TaskGrade.D1 }),
];
mockVotedGradeStatisticsFindMany(records);

const result = await getVoteGradeStatisticsForTaskIds(['abc001_a', 'abc002_a']);

expect(result.size).toBe(2);
expect(result.get('abc002_a')?.grade).toBe(TaskGrade.D1);
});
});
13 changes: 13 additions & 0 deletions src/features/votes/services/vote_statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ export async function getVoteGradeStatistics(): Promise<Map<string, VotedGradeSt
return gradesMap;
}

export async function getVoteGradeStatisticsForTaskIds(
taskIds: string[],
): Promise<Map<string, VotedGradeStatistics>> {
if (taskIds.length === 0) {
return new Map();
}

const stats = await prisma.votedGradeStatistics.findMany({
where: { taskId: { in: taskIds } },
});
return new Map(stats.map((s) => [s.taskId, s]));
Comment thread
river0525 marked this conversation as resolved.
}

export async function getAllTasksWithVoteInfo(): Promise<TaskWithVoteInfo[]> {
const [allTasks, stats, counters] = await Promise.all([
prisma.task.findMany({ orderBy: { task_id: 'desc' } }),
Expand Down
12 changes: 7 additions & 5 deletions src/routes/workbooks/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { error, type Actions } from '@sveltejs/kit';

import { Roles } from '$lib/types/user';
import type { TaskResult } from '$lib/types/task';

import * as taskResultsCrud from '$lib/services/task_results';
import { getWorkbookWithAuthor } from '$features/workbooks/services/workbooks';
import * as action from '$lib/actions/update_task_result';
import { getVoteGradeStatisticsForTaskIds } from '$features/votes/services/vote_statistics';

import { isAdmin, canRead } from '$lib/utils/authorship';
import { getLoggedInUser } from '$features/auth/services/session';
Expand Down Expand Up @@ -36,16 +36,18 @@ export async function load({ locals, params, url }) {
error(FORBIDDEN, `問題集id: ${slug} にアクセスする権限がありません。`);
}

const taskResults: Map<string, TaskResult> = await taskResultsCrud.getTaskResultsByTaskId(
workBook.workBookTasks,
loggedInUser?.id as string,
);
const taskIds = workBook.workBookTasks.map((t) => t.taskId);
const [taskResults, voteStatisticsMap] = await Promise.all([
taskResultsCrud.getTaskResultsByTaskId(workBook.workBookTasks, loggedInUser?.id as string),
getVoteGradeStatisticsForTaskIds(taskIds),
]);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

return {
isLoggedIn: loggedInUser !== null,
loggedInAsAdmin: loggedInAsAdmin,
...workbookWithAuthor,
taskResults: taskResults,
voteStatisticsMap: voteStatisticsMap,
};
}

Expand Down
15 changes: 14 additions & 1 deletion src/routes/workbooks/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ExternalLinkWrapper from '$lib/components/ExternalLinkWrapper.svelte';
import PublicationStatusLabel from '$features/workbooks/components/shared/PublicationStatusLabel.svelte';
import CommentAndHint from '$features/workbooks/components/detail/CommentAndHint.svelte';
import RelativeEvaluationBadge from '$features/votes/components/RelativeEvaluationBadge.svelte';

import { getBackgroundColorFrom } from '$lib/services/submission_status';

Expand All @@ -33,6 +34,7 @@
let workBook = data.workBook;
let workBookTasks: WorkBookTaskBase[] = $state([]);
let taskResults: Map<string, TaskResult> = $derived(data.taskResults);
let voteStatisticsMap = $derived(data.voteStatisticsMap);

let isLoggedIn = data.isLoggedIn;

Expand Down Expand Up @@ -157,14 +159,25 @@
</TableHead>
<TableBody class="divide-y divide-gray-200 dark:divide-gray-700">
{#each workBookTasks as workBookTask (workBookTask.taskId)}
{@const taskGrade = getTaskGrade(workBookTask.taskId)}
{@const statsEntry = voteStatisticsMap?.get(workBookTask.taskId)}
<TableBodyRow
id={getUniqueIdUsing(workBookTask.taskId)}
class={getBackgroundColorFrom(getTaskResult(workBookTask.taskId).status_name)}
>
<!-- 問題のグレード -->
<TableBodyCell class="justify-center w-16 px-0.5 xs:px-3">
<div class="flex items-center justify-center min-w-[54px] max-w-[54px]">
<GradeLabel taskGrade={getTaskGrade(workBookTask.taskId)} />
<div class="relative">
<GradeLabel {taskGrade} />
{#if taskGrade !== TaskGrade.PENDING && statsEntry}
<RelativeEvaluationBadge
officialGrade={taskGrade}
medianGrade={statsEntry.grade}
badgeId="rel-eval-{getUniqueIdUsing(workBookTask.taskId)}"
/>
{/if}
</div>
</div>
</TableBodyCell>

Expand Down
Loading