Skip to content

Commit

Permalink
Fix download speed going negative on retry
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata committed Oct 21, 2024
1 parent f013eed commit eb27bf0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 8 additions & 5 deletions frontend/src/lib/utils/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ export function progressStats(progress: Readable<utils.Progress | null>, options
};

const series = timeSeries(finalOptions.statsInterval);

const stats = writable({ speed: 0, eta: 0 as number | undefined });
const lastStatsUpdate = { speed: 0, eta: 0 };

progress.subscribe(($progress) => {
if (!$progress) {
series.clear();
} else {
} else {
if ((series.getLast() ?? -1) > $progress.current) {
series.clear();
}
series.addValue($progress.current);

const speed = series.getDerivative() ?? 0;
const eta = speed !== 0 ? ($progress.total - $progress.current) / speed : undefined;

if (Date.now() - lastStatsUpdate.speed > finalOptions.updateInterval.speed) {
stats.set({ speed: speed, eta: get(stats).eta });
lastStatsUpdate.speed = Date.now();
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/lib/utils/timeSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface TimeSeries {
addValue: (value: number) => void;
getAverage: () => number;
getDerivative: () => number | undefined;
getLast: () => number | undefined;
clear: () => void;
}

Expand All @@ -20,6 +21,9 @@ export function timeSeries(millisecondsLifetime: number): TimeSeries {
getDerivative: () => {
return (items[items.length - 1].value - items[0].value) / ((items[items.length - 1].timestamp - items[0].timestamp) / 1000); // per second
},
getLast: () => {
return items.length > 0 ? items[items.length - 1]?.value : undefined;
},
clear: () => {
items.length = 0;
},
Expand Down

0 comments on commit eb27bf0

Please sign in to comment.