feat(copyWithProgress): report copy progress every second instead of every 1%
Build / Lint & Test (push) Successful in 21s
Build / Lint & Test (push) Successful in 21s
This commit is contained in:
1 parent
70f065a3a2
commit
5fdc5a6781
2 files changed
+12
-19
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user