From 5fdc5a6781766bbb4bfaaecfa3ca2bbb7c842998 Mon Sep 17 00:00:00 2001 From: PickleRick Date: Thu, 1 Oct 2026 00:00:08 +0200 Subject: [PATCH] feat(copyWithProgress): report copy progress every second instead of every 1% --- FlowHelpers/1.0.0/copyWithProgress.js | 30 ++++++++++----------------- tests/quick-logic-test.js | 1 + 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/FlowHelpers/1.0.0/copyWithProgress.js b/FlowHelpers/1.0.0/copyWithProgress.js index 8ecb839..bfa1ad0 100644 --- a/FlowHelpers/1.0.0/copyWithProgress.js +++ b/FlowHelpers/1.0.0/copyWithProgress.js @@ -5,7 +5,7 @@ const path = require('path'); const MAX_RETRIES = 2; const RETRY_DELAY_MS = 1000; -const LOG_INTERVAL_MS = 3000; +const PROGRESS_INTERVAL_MS = 1000; const sleep = (milliseconds) => new Promise((resolve) => { setTimeout(resolve, milliseconds); @@ -85,9 +85,7 @@ const copyWithProgress = async ({ const startedAt = Date.now(); let copiedBytes = 0; - let lastReportedPercent = -1; - let lastLoggedPercent = -10; - let lastLoggedTime = startedAt; + let lastProgressTime = 0; let intervalBytes = 0; let intervalTime = startedAt; @@ -114,15 +112,15 @@ const copyWithProgress = async ({ const now = Date.now(); const percent = Math.min(Math.floor((copiedBytes / sourceSize) * 100), 99); - // Calculate ETA and speed - const totalElapsedSec = Math.max((now - startedAt) / 1000, 0.001); - const avgSpeedBps = copiedBytes / totalElapsedSec; - const remainingBytes = Math.max(0, sourceSize - copiedBytes); - const remainingSec = avgSpeedBps > 0 ? remainingBytes / avgSpeedBps : 0; - const eta = formatEta(remainingSec); + // Update UI and jobLog every second (PROGRESS_INTERVAL_MS) or on initial data chunk + const timeSinceLastProgress = now - lastProgressTime; + if (lastProgressTime === 0 || timeSinceLastProgress >= PROGRESS_INTERVAL_MS) { + const totalElapsedSec = Math.max((now - startedAt) / 1000, 0.001); + const avgSpeedBps = copiedBytes / totalElapsedSec; + const remainingBytes = Math.max(0, sourceSize - copiedBytes); + const remainingSec = avgSpeedBps > 0 ? remainingBytes / avgSpeedBps : 0; + const eta = formatEta(remainingSec); - // Update Tdarr UI on every percentage increase - if (percent > lastReportedPercent) { if (typeof updateWorker === 'function') { updateWorker({ percentage: percent, @@ -132,12 +130,7 @@ const copyWithProgress = async ({ if (typeof updateProgress === 'function') { updateProgress({ progress: percent }); } - lastReportedPercent = percent; - } - // Throttle jobLog output: every 10% or every LOG_INTERVAL_MS - const timeSinceLastLog = now - lastLoggedTime; - if (percent >= lastLoggedPercent + 10 || (timeSinceLastLog >= LOG_INTERVAL_MS && percent !== lastLoggedPercent)) { const intSec = Math.max((now - intervalTime) / 1000, 0.001); const currentSpeedMb = (intervalBytes / 1024 / 1024 / intSec).toFixed(1); const copiedMb = (copiedBytes / 1024 / 1024).toFixed(1); @@ -150,8 +143,7 @@ const copyWithProgress = async ({ ); } - lastLoggedPercent = percent; - lastLoggedTime = now; + lastProgressTime = now; intervalBytes = 0; intervalTime = now; } diff --git a/tests/quick-logic-test.js b/tests/quick-logic-test.js index d67c79a..98cab1a 100644 --- a/tests/quick-logic-test.js +++ b/tests/quick-logic-test.js @@ -266,6 +266,7 @@ const AUDIO_INPUTS = { ok(copyHelperCode.includes('fs.unlinkSync(destination)'), 'copy helper removes partial copies before retrying'); ok(copyHelperCode.includes('formatEta'), 'copy helper formats ETA'); ok(copyHelperCode.includes('updateWorker'), 'copy helper updates Tdarr worker progress'); + ok(copyHelperCode.includes('PROGRESS_INTERVAL_MS = 1000'), 'copy helper reports progress every 1 second (1000ms)'); // ── Subtitle default naming checks ───────────────────────────────────────── console.log('\n── Subtitle default naming rules ────────────────────');