Skip to content

Commit

Permalink
Change speed logging to be more responsive
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Apr 4, 2024
1 parent eb42d3e commit 1111464
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/lib/Downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class VideoDownloader {
private static async processVideo(video: Video) {
const logger = new this.ProgressLogger(video.title);

for (let retries = 1; retries < VideoDownloader.MaxRetries + 1; retries++) {
for (let retries = 1; retries < this.MaxRetries + 1; retries++) {
try {
if (settings.extras.saveNfo) {
logger.log("Saving .nfo");
Expand Down Expand Up @@ -109,8 +109,8 @@ export class VideoDownloader {
}
// eslint-disable-next-line no-fallthrough
case Video.State.Muxed: {
this.ProgressLogger.CompletedVideos++;
logger.done("Download & Muxing complete!");
VideoDownloader.ProgressLogger.CompletedVideos++;
promDownloadedTotal.inc();
}
}
Expand All @@ -125,7 +125,7 @@ export class VideoDownloader {
}
promErrors.labels({ message: message }).inc();

if (retries < VideoDownloader.MaxRetries) {
if (retries < this.MaxRetries) {
logger.error(`${message} - Retrying in ${retries}s [${retries}/${this.MaxRetries}]`);
// Wait between retries
await sleep(1000 * retries);
Expand Down
33 changes: 21 additions & 12 deletions src/lib/logging/ProgressBars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ export class ProgressBars extends ProgressLogger implements IProgressLogger {
public static DownloadSpeed = 0;
public static Errors = 0;

private _startTime: undefined | number = undefined;
private _lastTick = 0;

private _downloadSpeed = 0;
private reset() {
ProgressBars.DownloadSpeed -= this._downloadSpeed;
this._downloadSpeed = 0;
this.downloadedBytes = 0;
}

readonly title: string;
constructor(title: string) {
Expand All @@ -33,6 +28,13 @@ export class ProgressBars extends ProgressLogger implements IProgressLogger {
public log(message: string) {
ProgressBars._Bars.updateTask(this.title, { message });
}
private reset() {
ProgressBars.DownloadSpeed -= this._downloadSpeed;
ProgressBars.DownloadSpeed = Math.abs(ProgressBars.DownloadSpeed);
this._downloadSpeed = 0;
this.downloadedBytes = 0;
this.updateSummaryBar();
}
public done() {
ProgressBars._Bars.done(this.title);
this.reset();
Expand All @@ -52,23 +54,26 @@ export class ProgressBars extends ProgressLogger implements IProgressLogger {

public onDownloadProgress(progress: Progress): void {
if (progress.total === undefined) return;
ProgressBars.DownloadedBytes += progress.transferred - this.downloadedBytes;

const bytesSinceLastTick = progress.transferred - this.downloadedBytes;
ProgressBars.DownloadedBytes += bytesSinceLastTick;
super.onDownloadProgress(progress);

if (this._startTime === undefined) {
this._startTime ??= Date.now();
if (this._lastTick === 0) {
ProgressBars.TotalBytes += progress.total;
this._lastTick = Date.now();
}

const elapsed = (Date.now() - this._startTime) / 1000;
const elapsedSinceLastTick = (Date.now() - this._lastTick) / 1000;
this._lastTick = Date.now();

const downloadSpeed = progress.transferred / elapsed;
const downloadSpeed = bytesSinceLastTick / elapsedSinceLastTick;
if (!isNaN(downloadSpeed)) {
ProgressBars.DownloadSpeed += downloadSpeed - this._downloadSpeed;
this._downloadSpeed = downloadSpeed;
}

const downloadETA = progress.total / this._downloadSpeed - elapsed;
const downloadETA = progress.total / this._downloadSpeed - elapsedSinceLastTick;

const downloaded = chalk`{cyan ${(progress.transferred / 1000000).toFixed(2)}}/{cyan ${(progress.total / 1000000).toFixed(2)}MB}`;
const speed = chalk`{green ${(this._downloadSpeed / 125000).toFixed(2)} mb/s}`;
Expand All @@ -79,6 +84,10 @@ export class ProgressBars extends ProgressLogger implements IProgressLogger {
message: `${downloaded} ${speed} ${eta}`,
});

this.updateSummaryBar();
}

private updateSummaryBar() {
const processed = chalk`Processed: {yellow ${ProgressBars.CompletedVideos}}/{yellow ${ProgressBars.TotalVideos}} Errors: {red ${ProgressBars.Errors}}`;
const downloadedTotal = chalk`Total Downloaded: {cyan ${(ProgressBars.DownloadedBytes / 1000000).toFixed(2)}}/{cyan ${(
ProgressBars.TotalBytes / 1000000
Expand Down

0 comments on commit 1111464

Please sign in to comment.