Update the download speed every 2s. Speed is the average in the 2s.

Also update downloaded MB only if at least 1 MB has been downloaded
since last update.
This commit is contained in:
alessandro90
2019-06-29 00:24:45 +02:00
parent 88343af009
commit 408dd5e4ce
4 changed files with 60 additions and 23 deletions

View File

@@ -186,6 +186,8 @@ class Constants:
UNKNOWN = "N/A" UNKNOWN = "N/A"
EXTRACTING_MSG = "Extracting..." EXTRACTING_MSG = "Extracting..."
EXTRACTING_CODE = -1 EXTRACTING_CODE = -1
ZERO_INITIAL_SPEED = -1
ZERO_FINAL_SPEED = -2
NOT_AVAILABLE = "spectrumnotavailable.png" NOT_AVAILABLE = "spectrumnotavailable.png"
NOT_SELECTED = "nosignalselected.png" NOT_SELECTED = "nosignalselected.png"
FIELD_SEPARATOR = ";" FIELD_SEPARATOR = ";"

View File

@@ -21,9 +21,6 @@
<string notr="true"/> <string notr="true"/>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item> <item>
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="font"> <property name="font">
@@ -56,6 +53,21 @@ Please wait...
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLabel" name="speed_lbl">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>Speed</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item> <item>
<widget class="QProgressBar" name="progressBar"> <widget class="QProgressBar" name="progressBar">
<property name="minimum"> <property name="minimum">

View File

@@ -44,6 +44,7 @@ class DownloadWindow(QWidget, Ui_Download_window):
self._download_thread = DownloadThread() self._download_thread = DownloadThread()
self._download_thread.finished.connect(self._wait_close) self._download_thread.finished.connect(self._wait_close)
self._download_thread.progress.connect(self._display_progress) self._download_thread.progress.connect(self._display_progress)
self._download_thread.speed_progress.connect(self._display_speed)
self.closed.connect(self._download_thread.set_exit) self.closed.connect(self._download_thread.set_exit)
self.cancel_btn.clicked.connect(self._terminate_process) self.cancel_btn.clicked.connect(self._terminate_process)
@@ -51,28 +52,38 @@ class DownloadWindow(QWidget, Ui_Download_window):
"""Start the download thread.""" """Start the download thread."""
self._download_thread.start() self._download_thread.start()
def _downlaod_format_str(self, n, speed): def _downlaod_format_str(self, n):
"""Return a well-formatted string with downloaded MB and speed.""" """Return a well-formatted string with the downloaded MB."""
ret = f"Downloaded: {n} MB\nSpeed: " return f"Downloaded: {n} MB"
if speed == 0.0:
@pyqtSlot(float)
def _display_speed(self, speed):
"""Display the download speed."""
ret = "Speed: "
if speed == Constants.ZERO_INITIAL_SPEED:
ret += "Calculating..."
elif speed == 0.0:
ret += "VERY SLOW" ret += "VERY SLOW"
elif speed == Constants.ZERO_FINAL_SPEED:
ret = ""
else: else:
ret += f"{speed} MB/s" ret += f"{speed} MB/s"
return ret self.speed_lbl.setText(ret)
@pyqtSlot(int)
def _display_progress(self, progress):
"""Display the downloaded MB."""
if progress != Constants.EXTRACTING_CODE:
self.status_lbl.setText(self._downlaod_format_str(progress))
elif progress == Constants.EXTRACTING_CODE:
self.status_lbl.setText(Constants.EXTRACTING_MSG)
def show(self): def show(self):
"""Extends QWidget.show. Set downloaded MB and speed to zero.""" """Extends QWidget.show. Set downloaded MB and speed to zero."""
self.status_lbl.setText(self._downlaod_format_str(0, 0)) self._display_progress(0)
self._display_speed(Constants.ZERO_INITIAL_SPEED)
super().show() super().show()
@pyqtSlot(int, float)
def _display_progress(self, progress, speed):
"""Display the downloaded MB and speed."""
if progress != Constants.EXTRACTING_CODE:
self.status_lbl.setText(self._downlaod_format_str(progress, speed))
elif progress == Constants.EXTRACTING_CODE:
self.status_lbl.setText(Constants.EXTRACTING_MSG + '\n')
def _stop_thread(self): def _stop_thread(self):
"""Ask the download thread to stop.""" """Ask the download thread to stop."""
if self._download_thread.isRunning(): if self._download_thread.isRunning():

View File

@@ -45,9 +45,11 @@ class BaseDownloadThread(QThread):
class DownloadThread(BaseDownloadThread): class DownloadThread(BaseDownloadThread):
"""Subclass BaseDownloadThread. Download the database, images and audio samples.""" """Subclass BaseDownloadThread. Download the database, images and audio samples."""
progress = pyqtSignal(int, float) progress = pyqtSignal(int)
speed_progress = pyqtSignal(float)
_CHUNK = 128 * 1024 _CHUNK = 128 * 1024
_MEGA = 1024**2 _MEGA = 1024**2
_DELTAT = 2
def __init__(self): def __init__(self):
"""Just call super().__init__.""" """Just call super().__init__."""
@@ -80,6 +82,7 @@ class DownloadThread(BaseDownloadThread):
self.status = ThreadStatus.UNDEFINED self.status = ThreadStatus.UNDEFINED
self._db = None self._db = None
raw_data = bytes(0) raw_data = bytes(0)
sub_data = bytes(0)
try: try:
self._db = urllib3.PoolManager().request( self._db = urllib3.PoolManager().request(
'GET', 'GET',
@@ -88,6 +91,7 @@ class DownloadThread(BaseDownloadThread):
timeout=4.0 timeout=4.0
) )
start = perf_counter() start = perf_counter()
prev_downloaded = 0
while True: while True:
try: try:
data = self._db.read(self._CHUNK) data = self._db.read(self._CHUNK)
@@ -98,10 +102,17 @@ class DownloadThread(BaseDownloadThread):
if not data: if not data:
break break
raw_data += data raw_data += data
self.progress.emit( sub_data += data
self._pretty_len(raw_data), # Emit a progress signal only if at least 1 MB has been downloaded.
self._get_download_speed(raw_data, delta) if len(raw_data) - prev_downloaded >= self._MEGA:
prev_downloaded = len(raw_data)
self.progress.emit(self._pretty_len(raw_data))
if delta >= self._DELTAT:
self.speed_progress.emit(
self._get_download_speed(sub_data, delta)
) )
sub_data = bytes(0)
start = perf_counter()
if self._exit_call: if self._exit_call:
self._exit_call = False self._exit_call = False
self._db.release_conn() self._db.release_conn()
@@ -128,7 +139,8 @@ class DownloadThread(BaseDownloadThread):
if os.path.exists(Constants.DATA_FOLDER): if os.path.exists(Constants.DATA_FOLDER):
rmtree(Constants.DATA_FOLDER) rmtree(Constants.DATA_FOLDER)
try: try:
self.progress.emit(Constants.EXTRACTING_CODE, 0.0) self.progress.emit(Constants.EXTRACTING_CODE)
self.speed_progress.emit(Constants.ZERO_FINAL_SPEED)
with ZipFile(BytesIO(raw_data)) as zipped: with ZipFile(BytesIO(raw_data)) as zipped:
zipped.extractall() zipped.extractall()
except Exception: except Exception: