Skip to content

Add duration and byte-count formatters#188

Closed
gheine wants to merge 4 commits intomainfrom
formatters
Closed

Add duration and byte-count formatters#188
gheine wants to merge 4 commits intomainfrom
formatters

Conversation

@gheine
Copy link
Contributor

@gheine gheine commented Jun 19, 2024

No description provided.

@gheine gheine requested review from jlogeart and nicolagi June 19, 2024 15:32
@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor


object ByteFormatter {
def byteCountSI(bytes: Long): String = {
format(bytes, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how you feel about flag arguments but throwing in an alternative, e.g., format(value, multiplier) where the multiplier has only two cases (SI, Binary) and the list of symbols is derived from it with pattern matching.


object DurationFormatter {
import ByteFormatter.{df0, df1, df2}
private val units = Seq((1000L, "ns"), (1000L, "us"), (1000L, "ms"), (60L, "sec"), (60L, "min"), (24L, "h"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not μs and s? I'd suggest m instead of min, because while m is very common for durations, although techinically that's a meter, and d for days (also avois "days" vs "day") 🤷‍♂️ FWIW, Go (hence k8s and all infra stuff written in Go) formats durations as, e.g., 2d9h45m19s.

} else {
val signum = if (bytes < 0) "-" else ""
val exponent = getExponent(absBytes, baseValue)
val divisor = Math.pow(baseValue.toDouble, exponent.toDouble)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Seems both simpler and more precise to just return both exponent and divisor from getExponent. (In the recursive step, exponent becomes exponent+1 and divisor becomes divisor*baseValue.) Or just look it up like the unit perhaps.

"TiB values" in {
ByteFormatter.byteCountBinary(1024L * 1024 * 1024 * 1024) mustBe "1 TiB"
ByteFormatter.byteCountBinary(1024L * 1024 * 1024 * 1024 * 1024 - 1) mustBe "1023 TiB"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about larger values; can the unit lookup fail?

@jlogeart
Copy link
Contributor

jlogeart commented Jul 5, 2024

What's the context?

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@flow-tech
Copy link
Contributor

@lauravarga lauravarga closed this Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants