Skip to content

Commit

Permalink
Use bytes completed instead of all_time_download when calculating the…
Browse files Browse the repository at this point in the history
… ratio
  • Loading branch information
egbertbouman committed Dec 6, 2024
1 parent 49aa305 commit c22322e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,12 @@ def get_all_time_ratio(self) -> float:
if not self.lt_status:
return 0

if not self.all_time_download:
bytes_completed = self.get_progress() * self.download.tdef.get_length()
if not bytes_completed:
# We're returning -1 instead of infinity, as it avoids issues when JSON encoding.
return 0 if not self.all_time_upload else -1

return self.all_time_upload / self.all_time_download
return self.all_time_upload / bytes_completed

def get_seeding_time(self) -> int:
"""
Expand Down Expand Up @@ -316,7 +317,7 @@ def get_availability(self) -> float:
completed = peer.get('completed', 0)
have = peer.get('have', [])

if completed == 1 or have and all(have):
if completed == 1 or (have and all(have)):
nr_seeders_complete += 1
elif have and len(have) == len(merged_bitfields):
for i in range(len(have)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,26 @@ def test_all_time_ratio(self) -> None:
"""
Test if the all-time ratio is the fraction of the all-time up and down.
"""
state = DownloadState(Mock(), Mock(all_time_upload=200, all_time_download=1000), None)
tdef = Mock(get_length=Mock(return_value=1000))
state = DownloadState(Mock(tdef=tdef), Mock(progress=1, all_time_upload=200), None)

self.assertEqual(0.2, state.get_all_time_ratio())

def test_all_time_ratio_no_all_time_download(self) -> None:
"""
Test if the all-time ratio is 0 when the all-time up and down are both 0.
"""
state = DownloadState(Mock(), Mock(all_time_upload=0, all_time_download=0), None)
tdef = Mock(get_length=Mock(return_value=1000))
state = DownloadState(Mock(tdef=tdef), Mock(progress=0, all_time_upload=0), None)

self.assertEqual(0, state.get_all_time_ratio())

def test_all_time_ratio_no_all_time_download_inf(self) -> None:
"""
Test if the all-time ratio is 0 when the all-time download is 0.
"""
state = DownloadState(Mock(), Mock(all_time_upload=200, all_time_download=0), None)
tdef = Mock(get_length=Mock(return_value=1000))
state = DownloadState(Mock(tdef=tdef), Mock(progress=0, all_time_upload=1000), None)

self.assertEqual(-1, state.get_all_time_ratio())

Expand Down
20 changes: 10 additions & 10 deletions src/tribler/ui/src/pages/Downloads/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,40 @@ export default function DownloadDetails({ selectedDownloads }: { selectedDownloa
</div>
<div className="flex flex-row">
<div className="basis-1/4">{t('Name')}</div>
<div className="basis-3/4 break-all line-clamp-1">{download?.name}</div>
<div className="basis-3/4 break-all line-clamp-1">{download.name}</div>
</div>
<div className="flex flex-row">
<div className="basis-1/4">{t('Status')}</div>
<div className="basis-3/4">{capitalize(download?.status)}</div>
<div className="basis-3/4">{capitalize(download.status)}</div>
</div>
<div className="flex flex-row">
<div className="basis-1/4">{t('Filesize')}</div>
<div className="basis-3/4">{formatBytes(download?.size)}</div>
<div className="basis-3/4">{formatBytes(download.size)}</div>
</div>
<div className="flex flex-row">
<div className="basis-1/4">{t('Health')}</div>
<div className="basis-3/4">{t('SeedersLeechers', { seeders: download?.num_seeds, leechers: download?.num_peers })}</div>
<div className="basis-3/4">{t('SeedersLeechers', { seeders: download.num_seeds, leechers: download.num_peers })}</div>
</div>
<div className="flex flex-row">
<div className="basis-1/4">{t('Infohash')}</div>
<div className="basis-3/4">{download?.infohash}</div>
<div className="basis-3/4">{download.infohash}</div>
</div>
<div className="flex flex-row">
<div className="basis-1/4">{t('Destination')}</div>
<div className="basis-3/4 break-all line-clamp-1">{download?.destination}</div>
<div className="basis-3/4 break-all line-clamp-1">{download.destination}</div>
</div>
<div className="flex flex-row">
<div className="basis-1/4">{t('Ratio')}</div>
<div className="basis-3/4">{
download.all_time_ratio < 0 ?
String(`∞`) :
download?.all_time_ratio.toFixed(2)}
&nbsp;({formatBytes(download?.all_time_upload)} upload; {formatBytes(download?.all_time_download)} dowload)
download.all_time_ratio.toFixed(2)}
&nbsp;({formatBytes(download.all_time_upload)} / {formatBytes(download.size * download.progress)})
</div>
</div>
<div className="flex flex-row">
<div className="basis-1/4">{t('Availability')}</div>
<div className="basis-3/4">{download?.availability}</div>
<div className="basis-3/4">{download.availability}</div>
</div>
</div>
</ScrollArea>
Expand All @@ -107,7 +107,7 @@ export default function DownloadDetails({ selectedDownloads }: { selectedDownloa
</TabsContent>
<TabsContent value="peers" style={contentStyle}>
<ScrollArea className="h-full">
<Peers download={download} height={contentStyle.height}/>
<Peers download={download} height={contentStyle.height} />
</ScrollArea>
</TabsContent>
</Tabs>
Expand Down

0 comments on commit c22322e

Please sign in to comment.