-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Display download progress for file downloads
Display a vanilla progress bar for file downloads that simply shows how much of the file has been downloaded so far. A new FileDownloadProgressBar widget replaces the existing animated spinner, and we inject a signal through to the SDK to pass the current progress back to the widget. TODO: * Visual alignment issue; need padding on the right side * tests? Fixes #1104.
- Loading branch information
Showing
10 changed files
with
167 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from PyQt5.QtCore import pyqtSignal | ||
from PyQt5.QtWidgets import QProgressBar | ||
|
||
from securedrop_client.utils import humanize_filesize | ||
|
||
|
||
class FileDownloadProgressBar(QProgressBar): | ||
""" | ||
A progress bar for file downloads. | ||
Use the signal to increment the progress; see the SDK's ProgressProxy type. | ||
""" | ||
|
||
# first value is bytes fetched, second is smoothed speed in bytes/second | ||
signal = pyqtSignal((int, int)) | ||
|
||
def __init__(self, file_size: int) -> None: | ||
super().__init__() | ||
self.setObjectName("FileDownloadProgressBar") | ||
self.setMaximum(file_size) | ||
self.signal.connect(self.update_progress) | ||
|
||
def update_progress(self, fetched: int, speed: int) -> None: | ||
self.setValue(fetched) | ||
percentage = int((fetched / self.maximum()) * 100) | ||
# TODO: does this remove too much precision? | ||
formatted_speed = humanize_filesize(speed) + "/s" | ||
# TODO: localize this? | ||
self.setFormat(f"{percentage}% | {formatted_speed}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import time | ||
|
||
from PyQt5.QtCore import pyqtBoundSignal | ||
|
||
SMOOTHING_FACTOR = 0.3 | ||
|
||
|
||
class ProgressProxy: | ||
""" | ||
Relay the current download progress over to the UI | ||
We smooth the speed over time to reduce noise by using | ||
an exponential moving average (motivated by https://stackoverflow.com/a/3841706). | ||
The reported speed is 30% of the current speed and 70% of the previous | ||
smoothed speed. | ||
""" | ||
|
||
def __init__(self, inner: pyqtBoundSignal | None) -> None: | ||
self.inner = inner | ||
self.ema_speed = 0 | ||
self.last_total_time: float | None = None | ||
self.last_total_bytes = 0 | ||
|
||
def update_speed(self, value: int) -> None: | ||
now = time.time() | ||
|
||
# If this is the first update we report the speed as 0 | ||
if self.last_total_time is None: | ||
self.last_total_time = value | ||
self.last_total_bytes = value | ||
self.ema_speed = 0 | ||
return | ||
|
||
time_diff = now - self.last_total_time | ||
bytes_diff = value - self.last_total_bytes | ||
if time_diff > 0: | ||
self.ema_speed = int( | ||
(1 - SMOOTHING_FACTOR) * self.ema_speed + SMOOTHING_FACTOR * bytes_diff / time_diff | ||
) | ||
|
||
self.last_total_time = now | ||
self.last_total_bytes = value | ||
|
||
def set_value(self, value: int) -> None: | ||
if not self.inner: | ||
return | ||
|
||
self.update_speed(value) | ||
self.inner.emit(value, self.ema_speed) |
Oops, something went wrong.