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
13 changes: 6 additions & 7 deletions packages/alea-frontend/components/QuizPerformanceTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ function QuizPerformanceTable({
<TableCell sx={{ wordBreak: 'break-word' }}>
<b>{t.maxPoints}</b>
</TableCell>
{/* Showing my score contributed to performance issues during the quiz.
<TableCell sx={{ wordBreak: 'break-word' }}>
<b>{t.myScore}</b>
</TableCell> */}
<TableCell sx={{ wordBreak: 'break-word' }}>
<b>{t.averageScore}</b>
</TableCell>
<TableCell sx={{ wordBreak: 'break-word' }}>
<b>{t.myScore}</b>
</TableCell>
</TableRow>
</TableHead>
<TableBody>
Expand Down Expand Up @@ -145,12 +144,12 @@ function QuizPerformanceTable({
</Tooltip>
</TableCell>
<TableCell>{previousQuizData?.quizInfo[quiz.quizId]?.maxPoints}</TableCell>
{/* <TableCell>
{previousQuizData?.quizInfo[quiz.quizId]?.score?.toFixed(2)}
</TableCell> */}
<TableCell>
{previousQuizData?.quizInfo[quiz.quizId]?.averageScore?.toFixed(2)}
</TableCell>
<TableCell>
{previousQuizData?.quizInfo[quiz.quizId]?.score?.toFixed(2)}
</TableCell>
</TableRow>
))}
</TableBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,33 @@ import { getCurrentTermForCourseId } from '../../get-current-term';
import { queryGradingDbAndEndSet500OnError } from '../../grading-db-utils';
import { getAllQuizzes } from '../quiz-utils';

const USER_TO_QUIZ_SCORES_CACHE = new Map<string, { [quizId: string]: number }>();

// Quiz Id to (cacheTimestampMs, avgScore)
const QUIZ_AVG_SCORES_CACHE = new Map<string, { cacheTimestampMs: number; avgScore: number }>();

async function getUserScoresOrSet500Error(
userId: string,
res: NextApiResponse
): Promise<{ [quizId: string]: number } | undefined> {
if (USER_TO_QUIZ_SCORES_CACHE.size === 0) {
const result: Array<any> = await queryGradingDbAndEndSet500OnError(
`SELECT userId, quizId, sum(points) as score
FROM grading
WHERE (quizId, userId, problemId, browserTimestamp_ms) IN (
SELECT quizId, userId,problemId, MAX(browserTimestamp_ms) AS browserTimestamp_ms
const result: Array<any> = await queryGradingDbAndEndSet500OnError(
`SELECT g.quizId, SUM(g.points) as score
FROM grading g
WHERE g.userId = ?
AND (g.quizId, g.userId, g.problemId, g.browserTimestamp_ms) IN (
SELECT quizId, userId, problemId, MAX(browserTimestamp_ms) AS browserTimestamp_ms
FROM grading
GROUP BY quizId, userId,problemId
WHERE userId = ?
GROUP BY quizId, userId, problemId
)
GROUP BY userId,quizId;`,
[],
res
);
if (!result) return;
result.forEach((quiz) => {
const score = quiz['score'];
const quizId = quiz['quizId'];
const userId = quiz['userId'];
if (!USER_TO_QUIZ_SCORES_CACHE.has(userId)) USER_TO_QUIZ_SCORES_CACHE.set(userId, {});
USER_TO_QUIZ_SCORES_CACHE.get(userId)[quizId] = score;
});
}
return USER_TO_QUIZ_SCORES_CACHE.get(userId) ?? {};
GROUP BY g.quizId;`,
[userId, userId],
res
);
if (!result) return;
const userScores: { [quizId: string]: number } = {};
result.forEach((quiz) => {
userScores[quiz['quizId']] = quiz['score'];
});
return userScores;
}

async function getQuizAveragesOrSet500Error(
Expand Down Expand Up @@ -101,9 +96,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const courseId = req.query.courseId as string;
let instanceId = req.query.instanceId as string;
if (!instanceId) instanceId = await getCurrentTermForCourseId(courseId);
const userScores = {}; // await getUserScoresOrSet500Error(userId, res); Disable to avoid performance issues
const userScores = await getUserScoresOrSet500Error(userId, res); // Disable to avoid performance issues
if (!userScores) return;

const relevantQuizzes = getAllQuizzes()
.filter((q) => q.courseId === courseId && q.courseTerm === instanceId)
.filter((q) => getQuizPhase(q) === Phase.FEEDBACK_RELEASED);
Expand All @@ -130,7 +124,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
const quizId = quiz.id;
quizInfo[quizId] = {
score: userScores[quizId],
score: userScores[quizId] ?? 0,
averageScore: quizAverages.get(quizId),
maxPoints,
recorrectionInfo,
Expand Down
3 changes: 3 additions & 0 deletions sql/grading_database_setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ SELECT * FROM grading;

-- For optimizing the get-quiz API
CREATE INDEX idx_grading_optimus ON grading (quizId, userId, problemId, browserTimestamp_ms);

-- For optimizing user-specific score lookups in get-previous-quiz-info
CREATE INDEX idx_grading_user_quiz_problem_time ON grading(userId, quizId, problemId, browserTimestamp_ms);
Loading