From 8f2a9b9f9b4d8686f52460e3da8a8cdca9ab4fe1 Mon Sep 17 00:00:00 2001 From: drew2a Date: Thu, 25 Jan 2024 17:52:28 +0700 Subject: [PATCH] Fix `DownloadState,get_ratio()` - 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`. --- .../libtorrent/download_manager/download.py | 4 +-- .../download_manager/download_state.py | 30 ++++++++++++++----- .../libtorrent/restapi/downloads_endpoint.py | 6 ++-- .../libtorrent/tests/test_download.py | 2 +- .../libtorrent/tests/test_download_state.py | 8 ++--- .../gui/widgets/downloadsdetailstabwidget.py | 4 +-- 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/tribler/core/components/libtorrent/download_manager/download.py b/src/tribler/core/components/libtorrent/download_manager/download.py index 58a6e8ff0f6..1fa5da60067 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download.py +++ b/src/tribler/core/components/libtorrent/download_manager/download.py @@ -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() @@ -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() diff --git a/src/tribler/core/components/libtorrent/download_manager/download_state.py b/src/tribler/core/components/libtorrent/download_manager/download_state.py index 3793189c0aa..3bf0f74380a 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download_state.py +++ b/src/tribler/core/components/libtorrent/download_manager/download_state.py @@ -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 diff --git a/src/tribler/core/components/libtorrent/restapi/downloads_endpoint.py b/src/tribler/core/components/libtorrent/restapi/downloads_endpoint.py index d78f13a854d..c330ff5f193 100644 --- a/src/tribler/core/components/libtorrent/restapi/downloads_endpoint.py +++ b/src/tribler/core/components/libtorrent/restapi/downloads_endpoint.py @@ -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(), diff --git a/src/tribler/core/components/libtorrent/tests/test_download.py b/src/tribler/core/components/libtorrent/tests/test_download.py index 6ba9dee6dde..a58915fe12b 100644 --- a/src/tribler/core/components/libtorrent/tests/test_download.py +++ b/src/tribler/core/components/libtorrent/tests/test_download.py @@ -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()) diff --git a/src/tribler/core/components/libtorrent/tests/test_download_state.py b/src/tribler/core/components/libtorrent/tests/test_download_state.py index 1e841dc40a1..87c689543b1 100644 --- a/src/tribler/core/components/libtorrent/tests/test_download_state.py +++ b/src/tribler/core/components/libtorrent/tests/test_download_state.py @@ -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() == [] @@ -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() == [] diff --git a/src/tribler/gui/widgets/downloadsdetailstabwidget.py b/src/tribler/gui/widgets/downloadsdetailstabwidget.py index 8b6b9cc46bc..48d6cacd553 100644 --- a/src/tribler/gui/widgets/downloadsdetailstabwidget.py +++ b/src/tribler/gui/widgets/downloadsdetailstabwidget.py @@ -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 @@ -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}")