Added global ratio (only selected labels) and filtered torrents list ratio

This commit is contained in:
Someone
2023-11-06 00:15:20 +00:00
parent f4a4b3c252
commit 8495d5b165
2 changed files with 32 additions and 2 deletions

View File

@@ -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": {

View File

@@ -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 (
<Flex direction="column" sx={{ height: '100%' }} ref={ref}>
<ScrollArea sx={{ height: '100%', width: '100%' }} mb="xs">
@@ -184,7 +191,7 @@ function TorrentTile({ widget }: TorrentTileProps) {
)}
<Text color="dimmed" size="xs">
{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)}`}
</Text>
</Group>
</Flex>
@@ -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;