Skip to content

Commit

Permalink
Improve commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bortoz committed Dec 14, 2024
1 parent 99319b0 commit 5642f96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
22 changes: 14 additions & 8 deletions src/cli/firebase/definalize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { info, success } from "~/utils/logs";

import { sumBy } from "lodash-es";
import { participationConverter } from "./utils/converters-admin";
import { initializeFirebase } from "./utils/initialize";

Expand All @@ -8,28 +9,33 @@ export default async function definalize() {

info("Definalizing all participations.");

const ref = db.collection("participations").withConverter(participationConverter);
const ref = db
.collection("participations")
.where("finalized", "==", false)
.withConverter(participationConverter)
.limit(1000);

const chunkSize = 25_000;
let snapshot = await ref.limit(chunkSize).get();
let snapshot = await ref.get();

let sum = 0;
while (!snapshot.empty) {
await Promise.all(
const finalized = await Promise.all(
snapshot.docs.map(async (doc) => {
const participation = doc.data();
if (participation.finalized) {
await ref.doc(doc.id).update({ finalized: false });
await db.doc(doc.ref.path).update({ finalized: false });
return 1;
}
return 0;
}),
);

sum += snapshot.size;
sum += sumBy(finalized);
info(`Definalized ${sum} participations.`);

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

success("All participations are definalized.");
success(`${sum} participations were successfully definalized.`);
}
11 changes: 7 additions & 4 deletions src/cli/firebase/update-scores.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { calcScore } from "~/models";
import { info, success } from "~/utils/logs";

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

Expand All @@ -18,24 +19,26 @@ export default async function updateScores() {

let sum = 0;
while (!snapshot.empty) {
await Promise.all(
const updated = 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;
if (!student?.variant) return 0;
const score = calcScore(student, variants[student.variant]?.schema);
if (score === student.score) return 0;
t.update(doc.ref, { score });
return 1;
}),
),
);

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

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

success("All students updated.");
success(`${sum} students updated.`);
}

0 comments on commit 5642f96

Please sign in to comment.