Smooth download speed and time remaining
This commit is contained in:
1 parent
68c7dd76f2
commit
596caf57ee
4 files changed
+65
-36
No files matched your search
+6
-27
@@ -1,10 +1,9 @@
|
||||
import { button, element } from "./dom.js";
|
||||
import { formatBytes, formatDuration, percentOf, statusLabels } from "./format.js";
|
||||
import { formatBytes, formatRemaining, percentOf, statusLabels } from "./format.js";
|
||||
import { speedOf } from "./speed.js";
|
||||
|
||||
const activeStatuses = new Set(["queued", "downloading", "extracting", "moving"]);
|
||||
const pausableStatuses = new Set(["queued", "downloading"]);
|
||||
const speedSmoothing = 0.3;
|
||||
const speedSamples = new Map();
|
||||
|
||||
export function isActive(job) {
|
||||
return activeStatuses.has(job.status);
|
||||
@@ -16,26 +15,6 @@ export function matchesFilter(job, filter) {
|
||||
return job.status === filter;
|
||||
}
|
||||
|
||||
export function updateSpeeds(jobs) {
|
||||
const now = performance.now();
|
||||
const seenIds = new Set();
|
||||
|
||||
for (const job of jobs) {
|
||||
seenIds.add(job.id);
|
||||
const previous = speedSamples.get(job.id);
|
||||
let speed = 0;
|
||||
if (previous && job.bytesDone >= previous.bytes && now > previous.time) {
|
||||
const instantSpeed = ((job.bytesDone - previous.bytes) * 1000) / (now - previous.time);
|
||||
speed = previous.speed > 0 ? previous.speed + speedSmoothing * (instantSpeed - previous.speed) : instantSpeed;
|
||||
}
|
||||
speedSamples.set(job.id, { bytes: job.bytesDone, time: now, speed });
|
||||
}
|
||||
|
||||
for (const id of speedSamples.keys()) {
|
||||
if (!seenIds.has(id)) speedSamples.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
function displayRank(job) {
|
||||
if (isActive(job) && !job.queuePosition) return 0;
|
||||
if (job.queuePosition) return 1;
|
||||
@@ -110,11 +89,11 @@ function progressText(job, percent) {
|
||||
}
|
||||
if (percent !== null) parts.push(`${percent.toFixed(1)}%`);
|
||||
|
||||
const sample = speedSamples.get(job.id);
|
||||
if (isActive(job) && sample && sample.speed > 0) {
|
||||
parts.push(`${formatBytes(sample.speed)}/s`);
|
||||
const speed = speedOf(job.id);
|
||||
if (isActive(job) && speed > 0) {
|
||||
parts.push(`${formatBytes(speed)}/s`);
|
||||
if (job.bytesTotal > job.bytesDone) {
|
||||
parts.push(`${formatDuration((job.bytesTotal - job.bytesDone) / sample.speed)} left`);
|
||||
parts.push(`${formatRemaining((job.bytesTotal - job.bytesDone) / speed)} left`);
|
||||
}
|
||||
}
|
||||
return parts.join(" | ");
|
||||
|
||||
Reference in new issue
Block a user