-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # src/web/teacher/table.tsx
- Loading branch information
Showing
3 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters