Skip to content

Commit

Permalink
add database import progress (#986)
Browse files Browse the repository at this point in the history
Adds a class called `ProgressWriter` used for creating progress bars in the server terminal output. Made updates to use the new class to display a progress bar for when JSON files are being validated and loaded in the database.
  • Loading branch information
waffle-lord authored Dec 19, 2024
1 parent 7caec6e commit 8e0f57a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion project/src/utils/ImporterUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JsonUtil } from "@spt/utils/JsonUtil";
import { ProgressWriter } from "@spt/utils/ProgressWriter";
import { VFS } from "@spt/utils/VFS";
import { Queue } from "@spt/utils/collections/queue/Queue";
import { inject, injectable } from "tsyringe";
Expand Down Expand Up @@ -119,9 +120,12 @@ export class ImporterUtil {
directoriesToRead.enqueueAll(this.vfs.getDirs(directory).map((d) => `${directory}/${d}`));
}

const progressWriter = new ProgressWriter(filesToProcess.length);

while (filesToProcess.length !== 0) {
const fileNode = filesToProcess.dequeue();
if (!fileNode) continue;

if (this.vfs.getFileExtension(fileNode.fileName) === "json") {
const filePathAndName = `${fileNode.filePath}${fileNode.fileName}`;
promises.push(
Expand All @@ -135,7 +139,8 @@ export class ImporterUtil {
onObjectDeserialized(filePathAndName, fileDeserialized);
const strippedFilePath = this.vfs.stripExtension(filePathAndName).replace(filepath, "");
this.placeObject(fileDeserialized, strippedFilePath, result, strippablePath);
}),
})
.then(() => progressWriter.increment()),
);
}
}
Expand Down
35 changes: 35 additions & 0 deletions project/src/utils/ProgressWriter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export class ProgressWriter {
count = 0;
total: number;
done = false;

constructor(total: number) {
this.total = total;
}

public increment(): void {
if (this.done) {
return;
}

this.count++;

const progress = Math.floor((this.count / this.total) * 100);

// reduce bar fill max to 50 characters to save space
const progressHalved = Math.floor(progress / 4);

const barFill = "=".repeat(progressHalved);
const barEmptySpace = " ".repeat(Math.floor(25 - progressHalved));

const progressBar = ` -> ${this.count} / ${this.total} [${barFill}${barEmptySpace}] ${progress}%`;

process.stdout.write(progressBar);
process.stdout.cursorTo(0);

if (progress === 100) {
process.stdout.write("\n");
this.done = true;
}
}
}

0 comments on commit 8e0f57a

Please sign in to comment.