From 7e6f688b01be5e3e76591361add47cb22674e260 Mon Sep 17 00:00:00 2001 From: drew2a Date: Tue, 30 Jan 2024 12:46:30 +0700 Subject: [PATCH] Add `float('inf')` to `all_time_ratio` calculation --- .../download_manager/download_state.py | 8 +++- .../libtorrent/tests/test_download_state.py | 46 ++++++++++++++++++- .../gui/widgets/downloadsdetailstabwidget.py | 3 +- src/tribler/gui/widgets/downloadwidgetitem.py | 6 ++- 4 files changed, 58 insertions(+), 5 deletions(-) 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 ce4a4ece66d..7908874fa32 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download_state.py +++ b/src/tribler/core/components/libtorrent/download_manager/download_state.py @@ -33,7 +33,7 @@ class DownloadState: cf. libtorrent torrent_status """ - def __init__(self, download, lt_status: Optional[libtorrent.torrent_status], error): + def __init__(self, download, lt_status: Optional[libtorrent.torrent_status], error: Optional = None): """ Internal constructor. @param download The download this state belongs too. @@ -156,8 +156,12 @@ def total_payload_download(self) -> int: def get_all_time_ratio(self) -> float: """ Returns the accumulated seeding ratio of the download across multiple sessions. """ - if not self.lt_status or not self.all_time_download: + if not self.lt_status: return 0 + + if not self.all_time_download: + return 0 if not self.all_time_upload else float("inf") + return self.all_time_upload / self.all_time_download def get_seeding_time(self): 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 a96d9329592..965c33df854 100644 --- a/src/tribler/core/components/libtorrent/tests/test_download_state.py +++ b/src/tribler/core/components/libtorrent/tests/test_download_state.py @@ -61,7 +61,6 @@ def test_getters_setters_2(mock_download, mock_lt_status): assert download_state.all_time_upload == 200 assert download_state.all_time_download == 1000 - assert download_state.get_all_time_ratio() == 0.2 assert download_state.get_eta() == 0.25 assert download_state.get_num_seeds_peers() == (5, 5) @@ -79,6 +78,51 @@ def test_getters_setters_2(mock_download, mock_lt_status): assert download_state.get_progress() == 0.75 +def test_all_time_ratio_no_lt_status(): + # Test when lt_status is None + state = DownloadState( + download=Mock(), + lt_status=None, + ) + assert state.get_all_time_ratio() == 0 + + +def test_all_time_ratio(): + # Test all time ratio formula + state = DownloadState( + download=Mock(), + lt_status=Mock( + all_time_upload=200, + all_time_download=1000, + ), + ) + assert state.get_all_time_ratio() == 0.2 + + +def test_all_time_ratio_no_all_time_download(): + # Test all time ratio formula when all_time_download is 0 and all_time_upload is 0 + state = DownloadState( + download=Mock(), + lt_status=Mock( + all_time_upload=0, + all_time_download=0, + ), + ) + assert state.get_all_time_ratio() == 0 + + +def test_all_time_ratio_no_all_time_download_inf(): + # Test all time ratio formula when all_time_download is 0 and all_time_upload is not 0 + state = DownloadState( + download=Mock(), + lt_status=Mock( + all_time_upload=200, + all_time_download=0, + ), + ) + assert state.get_all_time_ratio() == float('inf') + + def test_get_files_completion(mock_download, mock_tdef): """ Testing whether the right completion of files is returned diff --git a/src/tribler/gui/widgets/downloadsdetailstabwidget.py b/src/tribler/gui/widgets/downloadsdetailstabwidget.py index ad79de3026a..04d83259cc5 100644 --- a/src/tribler/gui/widgets/downloadsdetailstabwidget.py +++ b/src/tribler/gui/widgets/downloadsdetailstabwidget.py @@ -174,8 +174,9 @@ def update_pages(self, new_download=False): all_time_upload = format_size(self.current_download['all_time_upload']) all_time_download = format_size(self.current_download['all_time_download']) all_time_ratio = self.current_download['all_time_ratio'] + all_time_ratio = '∞' if all_time_ratio == float('inf') else f'{all_time_ratio:.3f}' self.window().download_detail_ratio_label.setText( - f"{all_time_ratio:.3f}, upload: {all_time_upload}, download: {all_time_download}" + f"{all_time_ratio}, upload: {all_time_upload}, download: {all_time_download}" ) self.window().download_detail_availability_label.setText(f"{self.current_download['availability']:.2f}") diff --git a/src/tribler/gui/widgets/downloadwidgetitem.py b/src/tribler/gui/widgets/downloadwidgetitem.py index ca40235ff00..095a6697576 100644 --- a/src/tribler/gui/widgets/downloadwidgetitem.py +++ b/src/tribler/gui/widgets/downloadwidgetitem.py @@ -96,7 +96,11 @@ def update_item(self): self.setText(5, f"{self.download_info['num_connected_peers']} ({self.download_info['num_peers']})") self.setText(6, format_speed(self.download_info["speed_down"])) self.setText(7, format_speed(self.download_info["speed_up"])) - self.setText(8, f"{float(self.download_info['all_time_ratio']):.3f}") + + all_time_ratio = self.download_info['all_time_ratio'] + all_time_ratio = '∞' if all_time_ratio == float('inf') else f'{all_time_ratio:.3f}' + self.setText(8, all_time_ratio) + self.setText(9, "yes" if self.download_info["anon_download"] else "no") self.setText(10, str(self.download_info["hops"]) if self.download_info["anon_download"] else "-") self.setText(12, datetime.fromtimestamp(int(self.download_info["time_added"])).strftime('%Y-%m-%d %H:%M'))