Skip to content

Commit

Permalink
Add float('inf') to all_time_ratio calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jan 30, 2024
1 parent 313c51e commit 7e6f688
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/tribler/gui/widgets/downloadsdetailstabwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
6 changes: 5 additions & 1 deletion src/tribler/gui/widgets/downloadwidgetitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit 7e6f688

Please sign in to comment.