Skip to content

Commit

Permalink
Add command to update scores
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/web/teacher/table.tsx
  • Loading branch information
bortoz committed Dec 13, 2024
1 parent 871c321 commit 2205965
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/cli/firebase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import definalize from "./definalize";
import exportData from "./export";
import importData from "./import";
import init from "./init";
import updateScores from "./update-scores";

export default function firebaseCommand() {
const command = new Command("firebase");
Expand Down Expand Up @@ -51,6 +52,7 @@ export default function firebaseCommand() {
.action(importData);

command.command("definalize").description("Definalize all participations.").action(definalize);
command.command("update-scores").description("Update student scores.").action(updateScores);

return command;
}
41 changes: 41 additions & 0 deletions src/cli/firebase/update-scores.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { calcScore } from "~/models";
import { info, success } from "~/utils/logs";

import { studentConverter, variantConverter } from "./utils/converters-admin";
import { initializeFirebase } from "./utils/initialize";

export default async function updateScores() {
const { db } = await initializeFirebase();

info("Updating student scores.");

const variantRef = db.collection("variants").withConverter(variantConverter);
const variantSnap = await variantRef.get();
const variants = Object.fromEntries(variantSnap.docs.map((doc) => [doc.id, doc.data()]));

const ref = db.collectionGroup("students").withConverter(studentConverter).limit(1000);
let snapshot = await ref.get();

let sum = 0;
while (!snapshot.empty) {
await Promise.all(
snapshot.docs.map((doc) =>
db.runTransaction(async (t) => {
const studentSnap = await t.get(doc.ref);
const student = studentSnap.data();
if (!student?.variant) return;
const score = calcScore(student, variants[student.variant]?.schema);
t.update(doc.ref, { score });
}),
),
);

sum += snapshot.size;
info(`${sum} students updated.`);

const last = snapshot.docs.at(-1);
snapshot = await ref.startAfter(last).get();
}

success("All students updated.");
}
14 changes: 11 additions & 3 deletions src/web/teacher/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ function Table() {
participation.startingTime && contest.hasOnline
? addMinutes(participation.startingTime, contest.duration)
: undefined;
const isContestFinished = useIsAfter(endTime) || !endTime;
const isContestFinished = useIsAfter(endTime) ?? true;
const frozen = (contest.hasOnline && !isContestFinished) || participation.finalized;

const newStudentId = useRef(randomId());
Expand All @@ -313,7 +313,10 @@ function Table() {
} as Student);
}

const colDefs = useMemo(() => columnDefinition(contest, variants), [contest, variants]);
const colDefs = useMemo(
() => columnDefinition(contest, variants, isContestFinished),
[contest, variants, isContestFinished],
);

const onCellEditRequest = async (ev: CellEditRequestEvent) => {
let student = ev.data as Student;
Expand Down Expand Up @@ -423,7 +426,11 @@ function Table() {
);
}

function columnDefinition(contest: Contest, variants: Record<string, Variant>): ColDef[] {
function columnDefinition(
contest: Contest,
variants: Record<string, Variant>,
isContestFinished: boolean,
): ColDef[] {
const sampleVariant = Object.values(variants)[0];

const widths = {
Expand Down Expand Up @@ -524,6 +531,7 @@ function columnDefinition(contest: Contest, variants: Record<string, Variant>):
pinned: "right",
width: 100,
...defaultOptions,
hide: !isContestFinished,
},
{
field: "absent",
Expand Down

0 comments on commit 2205965

Please sign in to comment.