From 8e0f57ae0a726c363948b8225c6adf51fc86aeeb Mon Sep 17 00:00:00 2001 From: waffle-lord <76401815+waffle-lord@users.noreply.github.com> Date: Wed, 18 Dec 2024 22:36:39 -0500 Subject: [PATCH] add database import progress (#986) 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. --- project/src/utils/ImporterUtil.ts | 7 +++++- project/src/utils/ProgressWriter.ts | 35 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 project/src/utils/ProgressWriter.ts diff --git a/project/src/utils/ImporterUtil.ts b/project/src/utils/ImporterUtil.ts index 67c04b766..d9caa2d8d 100644 --- a/project/src/utils/ImporterUtil.ts +++ b/project/src/utils/ImporterUtil.ts @@ -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"; @@ -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( @@ -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()), ); } } diff --git a/project/src/utils/ProgressWriter.ts b/project/src/utils/ProgressWriter.ts new file mode 100644 index 000000000..d3ce90909 --- /dev/null +++ b/project/src/utils/ProgressWriter.ts @@ -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; + } + } +}