Skip to content

Commit

Permalink
Add math.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 269d09a commit 4a1811d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Author(s): Arno Bakker
"""
import logging
import math
from typing import Optional

from tribler.core.components.libtorrent.utils.libtorrent_helper import libtorrent
Expand Down Expand Up @@ -33,7 +34,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 +157,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 math.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
@@ -1,3 +1,4 @@
import math
from unittest.mock import Mock

import pytest
Expand Down Expand Up @@ -61,7 +62,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 +79,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() == math.inf


def test_get_files_completion(mock_download, mock_tdef):
"""
Testing whether the right completion of files is returned
Expand Down
4 changes: 3 additions & 1 deletion src/tribler/gui/widgets/downloadsdetailstabwidget.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import operator
from enum import IntEnum
from pathlib import PurePosixPath
Expand Down Expand Up @@ -174,8 +175,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 == math.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
7 changes: 6 additions & 1 deletion src/tribler/gui/widgets/downloadwidgetitem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import math
from datetime import datetime
from typing import Dict, Optional

Expand Down Expand Up @@ -96,7 +97,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 == math.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 4a1811d

Please sign in to comment.