Skip to content

Commit

Permalink
calculate ratio if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
dpozinen committed May 22, 2022
1 parent f33f7cf commit 2542bbb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/main/kotlin/dpozinen/deluge/DelugeTorrentConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class DelugeTorrentConverter(

val name = field<String>(fields, "name")
val state = field<String>(fields, "state")
val progress = field<Double, String>(fields, "progress") { it.toString() }
val progress = field<Double, String>(fields, "progress") { roundDouble(it) }
val uploaded = field<Double, String>(fields, "total_uploaded") { bytesToSize(it) }
val downloaded = field<Double, String>(fields, "total_done") { bytesToSize(it) }
val size = field<Double, String>(fields, "total_wanted") { bytesToSize(it) }
val ratio = field<Double, String>(fields, "ratio") { return@field if (it < 0) "-" else it.round(2).toString() }
val ratio = field<Double, String>(fields, "ratio") { ratio(it, fields) }

val eta = field<Double, String>(fields, "eta") { eta(it) }
val date = field<Long, String>(fields, "time_added") { date(it) }
Expand All @@ -33,6 +33,14 @@ class DelugeTorrentConverter(
)
}

private fun ratio(it: Double, fields: Map<String, *>) =
if (it == -1.0)
roundDouble((fields["total_uploaded"] as Long / fields["total_wanted"] as Long).toDouble())
else
roundDouble(it)

private fun roundDouble(it: Double) = if (it < 0) "-" else it.round(2).toString()

private fun date(timestamp: Long): String {
return dateTimeFormatter.format(Instant.ofEpochSecond(timestamp))
}
Expand Down
3 changes: 2 additions & 1 deletion static/js/deluge/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function infinityIfEmpty(value) {
return value === "" ? '<i class="fa-solid fa-infinity"></i>' : value
let empty = value === "" || value === "-" || value === "0s"
return empty ? '<i class="fa-solid fa-infinity"></i>' : value
}

function torrentCard(torrent) {
Expand Down

0 comments on commit 2542bbb

Please sign in to comment.