diff --git a/public/locales/en/modules/torrents-status.json b/public/locales/en/modules/torrents-status.json index 3f83a6396..cdca86767 100644 --- a/public/locales/en/modules/torrents-status.json +++ b/public/locales/en/modules/torrents-status.json @@ -19,13 +19,19 @@ "labelFilter": { "label": "Label list", "description": "When 'is whitelist' checked, this will act as a whitelist. If not checked, this is a blacklist. Will not do anything when empty" + }, + "displayRatioWithFilter": { + "label": "View filtered torrent list ratio", + "description": "When 'is whitelist' checked, this will act as a whitelist. If not checked, this is a blacklist. Will not do anything when empty" } } }, "card": { "footer": { "error": "Error", - "lastUpdated": "Last updated {{time}} ago" + "lastUpdated": "Last updated {{time}} ago", + "ratioGlobal": "Global ratio", + "ratioWithFilter": "Ratio with filter" }, "table": { "header": { diff --git a/src/widgets/torrent/TorrentTile.tsx b/src/widgets/torrent/TorrentTile.tsx index 79c52b28d..d2316101d 100644 --- a/src/widgets/torrent/TorrentTile.tsx +++ b/src/widgets/torrent/TorrentTile.tsx @@ -52,6 +52,10 @@ const definition = defineWidget({ type: 'multiple-text', defaultValue: [] as string[], }, + displayRatioWithFilter: { + type: 'switch', + defaultValue: true, + }, }, gridstack: { minWidth: 2, @@ -140,6 +144,9 @@ function TorrentTile({ widget }: TorrentTileProps) { const duration = dayjs.duration(difference, 'ms'); const humanizedDuration = duration.humanize(); + const ratioGlobal = getRatio(widget, torrents, false); + const ratioWithFilter = getRatio(widget, torrents, true); + return ( @@ -184,7 +191,7 @@ function TorrentTile({ widget }: TorrentTileProps) { )} - {t('card.footer.lastUpdated', { time: humanizedDuration })} + {t('card.footer.lastUpdated', { time: humanizedDuration })} - {t('card.footer.ratioGlobal')} : {ratioGlobal === -1 ? "∞" : ratioGlobal.toFixed(2)} {widget.properties.displayRatioWithFilter && ` - ${t('card.footer.ratioWithFilter')} : ${ratioWithFilter === -1 ? "∞" : ratioWithFilter.toFixed(2)}`} @@ -230,4 +237,21 @@ const filterTorrentsByLabels = ( return torrents.filter((torrent) => !labels.includes(torrent.label as string)); }; +const getRatio = ( + widget: ITorrent, + torrents: NormalizedTorrent[], + applyAllFilter:boolean +) => { + + if(applyAllFilter) { + torrents = filterTorrents(widget,torrents) + } else { + torrents = filterTorrentsByLabels(torrents, widget.properties.labelFilter,widget.properties.labelFilterIsWhitelist) + } + + let totalDownloadedSum = torrents.reduce((sum, torrent) => sum + torrent.totalDownloaded, 0); + + return totalDownloadedSum > 0 ? torrents.reduce((sum, torrent) => sum + torrent.totalUploaded, 0) / totalDownloadedSum : -1; +} + export default definition;