From ea47e80f144c62ef356012473c6388820b512215 Mon Sep 17 00:00:00 2001 From: Garrett Robinson Date: Wed, 23 Dec 2015 00:57:46 -0500 Subject: [PATCH 1/3] Add estimated time remaining to progress indicator Estimates the time remaining for each download and displays it in the progress bar. Waits for 10 seconds before showing the progress bar to allow the download rate to stabilize, which prevents the estimated time remaining from jumping all over the place at the start of the download (a.k.a the "Windows copy dialog experience"). If your download takes less than 10 seconds, you don't really need to see an ETA anyway. This commit also refactors the Downloads class, splitting out the download-specific functionality into a new Download class, providing better encapsulation. As a result, I was able to simplify the call to `update_download` because it was no longer necessary to pass the `total_bytes` (which don't change after the download has begun). Tested on Mac OS 10.9. --- onionshare/helpers.py | 40 ++++++++++++++ onionshare_gui/downloads.py | 90 ++++++++++++++++++++++---------- onionshare_gui/onionshare_gui.py | 2 +- 3 files changed, 103 insertions(+), 29 deletions(-) diff --git a/onionshare/helpers.py b/onionshare/helpers.py index 1a48147f..c2c4793f 100644 --- a/onionshare/helpers.py +++ b/onionshare/helpers.py @@ -19,6 +19,8 @@ along with this program. If not, see . """ import os, inspect, hashlib, base64, hmac, platform, zipfile, tempfile from itertools import izip +import math +import time # hack to make unicode filenames work (#141) import sys @@ -112,6 +114,44 @@ def human_readable_filesize(b): return '{0:.1f} {1:s}'.format(round(b, 1), units[u]) +def format_seconds(seconds): + """Return a human-readable string of the format 1d2h3m4s""" + seconds_in_a_minute = 60 + seconds_in_an_hour = seconds_in_a_minute * 60 + seconds_in_a_day = seconds_in_an_hour * 24 + + days = math.floor(seconds / seconds_in_a_day) + + hour_seconds = seconds % seconds_in_a_day + hours = math.floor(hour_seconds / seconds_in_an_hour) + + minute_seconds = hour_seconds % seconds_in_an_hour + minutes = math.floor(minute_seconds / seconds_in_a_minute) + + remaining_seconds = minute_seconds % seconds_in_a_minute + seconds = math.ceil(remaining_seconds) + + human_readable = [] + if days > 0: + human_readable.append("{}d".format(int(days))) + if hours > 0: + human_readable.append("{}h".format(int(hours))) + if minutes > 0: + human_readable.append("{}m".format(int(minutes))) + if seconds > 0: + human_readable.append("{}s".format(int(seconds))) + return ''.join(human_readable) + + +def estimated_time_remaining(bytes_downloaded, total_bytes, started): + now = time.time() + time_elapsed = now - started # in seconds + download_rate = bytes_downloaded / time_elapsed + remaining_bytes = total_bytes - bytes_downloaded + eta = remaining_bytes / download_rate + return format_seconds(eta) + + def is_root(): """ Returns if user is root. diff --git a/onionshare_gui/downloads.py b/onionshare_gui/downloads.py index 0ecb49f0..654bebb4 100644 --- a/onionshare_gui/downloads.py +++ b/onionshare_gui/downloads.py @@ -17,12 +17,67 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ +import time + from PyQt4 import QtCore, QtGui import common from onionshare import strings, helpers +class Download(object): + + def __init__(self, download_id, total_bytes): + self.download_id = download_id + self.started = time.time() + self.total_bytes = total_bytes + self.downloaded_bytes = 0 + + # make a new progress bar + self.progress_bar = QtGui.QProgressBar() + self.progress_bar.setTextVisible(True) + self.progress_bar.setAlignment(QtCore.Qt.AlignHCenter) + self.progress_bar.setMinimum(0) + self.progress_bar.setMaximum(total_bytes) + self.progress_bar.setValue(0) + self.progress_bar.setStyleSheet( + "QProgressBar::chunk { background-color: #05B8CC; }") + self.progress_bar.total_bytes = total_bytes + + # start at 0 + self.update(0) + + def update(self, downloaded_bytes): + self.downloaded_bytes = downloaded_bytes + + self.progress_bar.setValue(downloaded_bytes) + if downloaded_bytes == self.progress_bar.total_bytes: + pb_fmt = "%p%" + else: + elapsed = time.time() - self.started + if elapsed < 10: + # Wait a couple of seconds for the download rate to stabilize. + # This prevents an "Windows copy dialog"-esque experience at + # the beginning of the download. + pb_fmt = "{0:s}, %p% (Computing ETA)".format( + helpers.human_readable_filesize(downloaded_bytes)) + else: + pb_fmt = "{0:s}, ETA: {1:s}, %p%".format( + helpers.human_readable_filesize(downloaded_bytes), + self.estimated_time_remaining) + + self.progress_bar.setFormat(pb_fmt) + + def cancel(self): + self.progress_bar.setFormat(strings._('gui_canceled')) + + @property + def estimated_time_remaining(self): + return helpers.estimated_time_remaining(self.downloaded_bytes, + self.total_bytes, + self.started) + + class Downloads(QtGui.QVBoxLayout): """ The downloads chunk of the GUI. This lists all of the active download @@ -31,7 +86,7 @@ class Downloads(QtGui.QVBoxLayout): def __init__(self): super(Downloads, self).__init__() - self.progress_bars = {} + self.downloads = {} # downloads label self.downloads_label = QtGui.QLabel(strings._('gui_downloads', True)) @@ -46,40 +101,19 @@ class Downloads(QtGui.QVBoxLayout): """ self.downloads_label.show() - # make a new progress bar - pb = QtGui.QProgressBar() - pb.setTextVisible(True) - pb.setAlignment(QtCore.Qt.AlignHCenter) - pb.setMinimum(0) - pb.setMaximum(total_bytes) - pb.setValue(0) - pb.setStyleSheet("QProgressBar::chunk { background-color: #05B8CC; }") - pb.total_bytes = total_bytes - # add it to the list - self.progress_bars[download_id] = pb - self.addWidget(pb) + download = Download(download_id, total_bytes) + self.downloads[download_id] = download + self.addWidget(download.progress_bar) - # start at 0 - self.update_download(download_id, total_bytes, 0) - - def update_download(self, download_id, total_bytes, downloaded_bytes): + def update_download(self, download_id, downloaded_bytes): """ Update the progress of a download progress bar. """ - if download_id not in self.progress_bars: - self.add_download(download_id, total_bytes) - - pb = self.progress_bars[download_id] - pb.setValue(downloaded_bytes) - if downloaded_bytes == pb.total_bytes: - pb.setFormat("%p%") - else: - pb.setFormat("{0:s}, %p%".format(helpers.human_readable_filesize(downloaded_bytes))) + self.downloads[download_id].update(downloaded_bytes) def cancel_download(self, download_id): """ Update a download progress bar to show that it has been canceled. """ - pb = self.progress_bars[download_id] - pb.setFormat(strings._('gui_canceled')) + self.downloads[download_id].cancel() diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 2be92eae..f52ce905 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -218,7 +218,7 @@ class OnionShareGui(QtGui.QWidget): self.downloads.add_download(event["data"]["id"], web.zip_filesize) elif event["type"] == web.REQUEST_PROGRESS: - self.downloads.update_download(event["data"]["id"], web.zip_filesize, event["data"]["bytes"]) + self.downloads.update_download(event["data"]["id"], event["data"]["bytes"]) # is the download complete? if event["data"]["bytes"] == web.zip_filesize: From e9eed561d6d2b2ce8735f0a506bc4ebd7cbbf161 Mon Sep 17 00:00:00 2001 From: Garrett Robinson Date: Wed, 23 Dec 2015 01:21:40 -0500 Subject: [PATCH 2/3] Add time elapsed for completed downloads --- onionshare_gui/downloads.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/onionshare_gui/downloads.py b/onionshare_gui/downloads.py index 654bebb4..8b563d1c 100644 --- a/onionshare_gui/downloads.py +++ b/onionshare_gui/downloads.py @@ -52,7 +52,8 @@ class Download(object): self.progress_bar.setValue(downloaded_bytes) if downloaded_bytes == self.progress_bar.total_bytes: - pb_fmt = "%p%" + pb_fmt = "%p%, Time Elapsed: {0:s}".format( + helpers.format_seconds(time.time() - self.started)) else: elapsed = time.time() - self.started if elapsed < 10: From 41b3e6ab9d6c20d3de8e4ae09699492151d0631b Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Fri, 12 Feb 2016 08:58:29 -0800 Subject: [PATCH 3/3] Added estimated time remaining strings to English locale file --- locale/en.json | 5 ++++- onionshare_gui/downloads.py | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/locale/en.json b/locale/en.json index b6aa60a9..e45a2fa1 100644 --- a/locale/en.json +++ b/locale/en.json @@ -41,5 +41,8 @@ "gui_please_wait": "Please wait...", "error_hs_dir_cannot_create": "Cannot create hidden service dir {0:s}", "error_hs_dir_not_writable": "Hidden service dir {0:s} is not writable", - "using_ephemeral": "Staring ephemeral Tor hidden service and awaiting publication" + "using_ephemeral": "Staring ephemeral Tor hidden service and awaiting publication", + "gui_download_progress_complete": "%p%, Time Elapsed: {0:s}", + "gui_download_progress_starting": "{0:s}, %p% (Computing ETA)", + "gui_download_progress_eta": "{0:s}, ETA: {1:s}, %p%" } diff --git a/onionshare_gui/downloads.py b/onionshare_gui/downloads.py index 8b563d1c..054db3e7 100644 --- a/onionshare_gui/downloads.py +++ b/onionshare_gui/downloads.py @@ -52,7 +52,7 @@ class Download(object): self.progress_bar.setValue(downloaded_bytes) if downloaded_bytes == self.progress_bar.total_bytes: - pb_fmt = "%p%, Time Elapsed: {0:s}".format( + pb_fmt = strings._('gui_download_progress_complete').format( helpers.format_seconds(time.time() - self.started)) else: elapsed = time.time() - self.started @@ -60,10 +60,10 @@ class Download(object): # Wait a couple of seconds for the download rate to stabilize. # This prevents an "Windows copy dialog"-esque experience at # the beginning of the download. - pb_fmt = "{0:s}, %p% (Computing ETA)".format( + pb_fmt = strings._('gui_download_progress_starting').format( helpers.human_readable_filesize(downloaded_bytes)) else: - pb_fmt = "{0:s}, ETA: {1:s}, %p%".format( + pb_fmt = strings._('gui_download_progress_eta').format( helpers.human_readable_filesize(downloaded_bytes), self.estimated_time_remaining)