Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions crates/piqueld-core/src/observability/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,22 @@ impl Elapsed {
impl fmt::Display for Elapsed {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let milliseconds = self.0.as_millis();
// Rounded first, so the unit is picked after rounding: 3,599.5 s
// reads as an hour rather than 60 minutes.
let seconds = (milliseconds + 500) / 1_000;
match milliseconds {
0..=999 => write!(formatter, "{milliseconds} ms"),
// Tenths of a second, below the 60.0 s that is shown as a
// minute.
1_000..=59_949 => write!(formatter, "{} s", Tenths::of(milliseconds, 1_000)),
59_950..=3_599_999 => {
let seconds = (milliseconds + 500) / 1_000;
_ if seconds < 3_600 => {
write!(formatter, "{} min {} s", seconds / 60, seconds % 60)
}
_ => write!(
formatter,
"{} h {} min",
milliseconds / 3_600_000,
(milliseconds % 3_600_000) / 60_000
seconds / 3_600,
seconds % 3_600 / 60
),
}
}
Expand Down Expand Up @@ -255,6 +257,8 @@ mod tests {
(59_949, "59.9 s"),
(59_950, "1 min 0 s"),
(252_000, "4 min 12 s"),
(3_599_499, "59 min 59 s"),
(3_599_500, "1 h 0 min"),
(7_500_000, "2 h 5 min"),
] {
assert_eq!(Elapsed::from_millis(milliseconds).to_string(), text);
Expand Down
14 changes: 11 additions & 3 deletions crates/piqueld-core/src/observability/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt;
/// A quantity in tenths of a unit, rounded half up from `value` of a `unit`:
/// `Tenths::of(1536, 1024)` reads `1.5`. With `{:#}`, a whole number drops
/// its `.0`. Sizes, durations and compact counts round this way.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(super) struct Tenths(u128);

impl Tenths {
Expand Down Expand Up @@ -43,10 +43,12 @@ impl Bytes {
impl fmt::Display for Bytes {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
const UNITS: [&str; 4] = ["KiB", "MiB", "GiB", "TiB"];
// The largest unit it reaches, of 2^(10 × power) bytes.
// The largest unit it reaches once rounded, of 2^(10 × power) bytes:
// the one whose previous unit rounds to 1024.0 or more, so 1023.95 KiB
// reads as 1.0 MiB.
match (1..=UNITS.len())
.rev()
.find(|power| self.0 >> (10 * power) > 0)
.find(|power| Tenths::of(u128::from(self.0), 1 << (10 * (power - 1))) >= Tenths(10_240))
{
None => write!(formatter, "{} B", self.0),
Some(power) => {
Expand Down Expand Up @@ -115,6 +117,12 @@ mod tests {
(1023, "1023 B"),
(1024, "1.0 KiB"),
(1536, "1.5 KiB"),
(1_048_524, "1023.9 KiB"),
(1_048_525, "1.0 MiB"),
(1_073_689_395, "1023.9 MiB"),
(1_073_689_396, "1.0 GiB"),
(1_099_457_940_684, "1023.9 GiB"),
(1_099_457_940_685, "1.0 TiB"),
(52_428_800, "50.0 MiB"),
(127_026_911_232, "118.3 GiB"),
(u64::MAX, "16777216.0 TiB"),
Expand Down