chore: better migration script logs

This commit is contained in:
Miodec 2024-05-17 16:57:27 +02:00
parent 4d76537871
commit c164b84291

View file

@ -55,7 +55,9 @@ export async function migrate(): Promise<void> {
resultCollection = DB.collection("results");
console.log("Creating index on users collection...");
const t1 = Date.now();
await userCollection.createIndex({ uid: 1 }, { unique: true });
console.log("Index created in", Date.now() - t1, "ms");
await migrateResults();
}
@ -74,18 +76,27 @@ async function migrateResults(batchSize = 50): Promise<void> {
const start = new Date().valueOf();
let uids: string[] = [];
do {
const t0 = Date.now();
console.log("Fetching users to migrate...");
const t1 = Date.now();
uids = await getUsersToMigrate(batchSize);
console.log("Fetched", uids.length, "users in", Date.now() - t1, "ms");
console.log("Users to migrate:", uids.join(","));
//migrate
const t2 = Date.now();
await migrateUsers(uids);
console.log("Migrated", uids.length, "users in", Date.now() - t2, "ms");
const t3 = Date.now();
await handleUsersWithNoResults(uids);
console.log("Handled users with no results in", Date.now() - t3, "ms");
//progress tracker
count += uids.length;
updateProgress(allUsersCount, count, start);
updateProgress(allUsersCount, count, start, Date.now() - t0);
} while (uids.length > 0 && appRunning);
if (appRunning) updateProgress(100, 100, start);
if (appRunning) updateProgress(100, 100, start, 0);
}
async function getUsersToMigrate(limit: number): Promise<string[]> {
@ -98,7 +109,6 @@ async function getUsersToMigrate(limit: number): Promise<string[]> {
}
async function migrateUsers(uids: string[]): Promise<void> {
console.log("migrateUsers:", uids.join(","));
await resultCollection
.aggregate(
[
@ -214,7 +224,6 @@ async function migrateUsers(uids: string[]): Promise<void> {
}
async function handleUsersWithNoResults(uids: string[]): Promise<void> {
console.log("handleUsersWithNoResults:", uids.join(","));
await userCollection.updateMany(
{
$and: [{ uid: { $in: uids } }, filter],
@ -223,7 +232,12 @@ async function handleUsersWithNoResults(uids: string[]): Promise<void> {
);
}
function updateProgress(all: number, current: number, start: number): void {
function updateProgress(
all: number,
current: number,
start: number,
previousBatchSizeTime: number
): void {
const percentage = (current / all) * 100;
const timeLeft = Math.round(
(((new Date().valueOf() - start) / percentage) * (100 - percentage)) / 1000
@ -232,6 +246,8 @@ function updateProgress(all: number, current: number, start: number): void {
process.stdout.clearLine?.(0);
process.stdout.cursorTo?.(0);
process.stdout.write(
`${Math.round(percentage)}% done, estimated time left ${timeLeft} seconds.`
`Previous batch took ${Math.round(previousBatchSizeTime)}ms ${Math.round(
percentage
)}% done, estimated time left ${timeLeft} seconds.`
);
}