Skip to content

Commit

Permalink
Fix DownloadState,get_ratio()
Browse files Browse the repository at this point in the history
- Refactored the `get_total_transferred` method in `DownloadState` to separate `total_upload` and `total_download` properties.
- Updated references to `get_total_transferred(UPLOAD)` and `get_total_transferred(DOWNLOAD)` with the new properties in multiple files (`download.py`, `downloads_endpoint.py`, `test_download_state.py`, and `downloadsdetailstabwidget.py`).
- Renamed the method `get_seeding_ratio` in `DownloadState` to `get_ratio`.
  • Loading branch information
drew2a committed Jan 25, 2024
1 parent ed1d03b commit 8f2a9b9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def on_torrent_finished_alert(self, alert: lt.torrent_finished_alert):
self._logger.info(f'On torrent finished alert: {safe_repr(alert)}')
self.update_lt_status(self.handle.status())
self.checkpoint()
downloaded = self.get_state().get_total_transferred(DOWNLOAD)
downloaded = self.get_state().total_download
if downloaded > 0 and self.stream is not None and self.notifier is not None:
name = self.tdef.get_name_as_unicode()
infohash = self.tdef.get_infohash().hex()
Expand All @@ -465,7 +465,7 @@ def _stop_if_finished(self):
seeding_ratio = self.download_defaults.seeding_ratio
seeding_time = self.download_defaults.seeding_time
if (mode == 'never' or
(mode == 'ratio' and state.get_seeding_ratio() >= seeding_ratio) or
(mode == 'ratio' and state.get_ratio() >= seeding_ratio) or
(mode == 'time' and state.get_seeding_time() >= seeding_time)):
self.stop()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,35 @@ def get_current_payload_speed(self, direct):
return self.lt_status.upload_payload_rate
return self.lt_status.download_payload_rate

def get_total_transferred(self, direct):
@property
def total_upload(self) -> int:
"""
Returns the total amount of up or downloaded bytes.
Returns the total amount of uploaded bytes.
@return The amount in bytes.
"""
if not self.lt_status:
return 0
return self.lt_status.total_upload

@property
def total_download(self) -> int:
"""
Returns the total amount of downloaded bytes.
@return The amount in bytes.
"""
if not self.lt_status:
return 0
elif direct == UPLOAD:
return self.lt_status.total_upload
return self.lt_status.total_download

def get_seeding_ratio(self):
if self.lt_status and self.lt_status.total_done > 0:
return self.lt_status.all_time_upload / float(self.lt_status.total_done)
return 0
def get_ratio(self) -> float:
""" Returns the seeding ratio.
Returns:
float: The seeding ratio.
"""
if not self.lt_status or not self.total_download:
return 0
return self.total_upload / self.total_download

def get_seeding_time(self):
return self.lt_status.finished_time if self.lt_status else 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ async def get_downloads(self, request):
"num_seeds": num_seeds,
"num_connected_peers": num_connected_peers,
"num_connected_seeds": num_connected_seeds,
"total_up": state.get_total_transferred(UPLOAD),
"total_down": state.get_total_transferred(DOWNLOAD),
"ratio": state.get_seeding_ratio(),
"total_up": state.total_upload,
"total_down": state.total_download,
"ratio": state.get_ratio(),
"trackers": tracker_info,
"hops": download.config.get_hops(),
"anon_download": download.get_anon_mode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ def test_on_torrent_finished_alert(test_download: Download):
test_download.handle = Mock(is_valid=Mock(return_value=True))
test_download.notifier = MagicMock()
test_download.stream = Mock()
test_download.get_state = Mock(return_value=Mock(get_total_transferred=Mock(return_value=1)))
test_download.get_state = Mock(return_value=Mock(total_download=1))

test_download.on_torrent_finished_alert(Mock())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_getters_setters_1(mock_download):
assert download_state.get_progress() == 0
assert download_state.get_error() is None
assert download_state.get_current_speed(UPLOAD) == 0
assert download_state.get_total_transferred(UPLOAD) == 0
assert download_state.total_upload == 0
assert download_state.get_num_seeds_peers() == (0, 0)
assert download_state.get_peerlist() == []

Expand All @@ -48,9 +48,9 @@ def test_getters_setters_2(mock_download, mock_lt_status):
assert download_state.get_status() == DownloadStatus.DOWNLOADING
assert download_state.get_current_speed(UPLOAD) == 123
assert download_state.get_current_speed(DOWNLOAD) == 43
assert download_state.get_total_transferred(UPLOAD) == 100
assert download_state.get_total_transferred(DOWNLOAD) == 200
assert download_state.get_seeding_ratio() == 0.5
assert download_state.total_upload == 100
assert download_state.total_download == 200
assert download_state.get_ratio() == 0.5
assert download_state.get_eta() == 0.25
assert download_state.get_num_seeds_peers() == (5, 5)
assert download_state.get_pieces_complete() == []
Expand Down
4 changes: 2 additions & 2 deletions src/tribler/gui/widgets/downloadsdetailstabwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from tribler.core.utilities.simpledefs import DownloadStatus
from tribler.gui.defs import STATUS_STRING
from tribler.gui.network.request_manager import request_manager
from tribler.gui.utilities import compose_magnetlink, connect, copy_to_clipboard, format_size, format_speed, tr
from tribler.gui.widgets.torrentfiletreewidget import PreformattedTorrentFileTreeWidget

Expand Down Expand Up @@ -174,8 +173,9 @@ def update_pages(self, new_download=False):
self.window().download_detail_destination_label.setText(self.current_download["destination"])
up = format_size(self.current_download['total_up'])
down = format_size(self.current_download['total_down'])
ratio = self.current_download['ratio']
self.window().download_detail_ratio_label.setText(
f"{self.current_download['ratio']:.3f}, up: {up}, down: {down}"
f"{ratio:.3f}, up: {up}, down: {down}"
)
self.window().download_detail_availability_label.setText(f"{self.current_download['availability']:.2f}")

Expand Down

0 comments on commit 8f2a9b9

Please sign in to comment.