Skip to content

Commit

Permalink
feat: better stats duration log
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiroaisen committed Dec 24, 2023
1 parent 0063817 commit c50a1f6
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion rs/packages/api/src/ws_stats/routes/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ impl WsConnectionHandler {

log::info!(
target: "ws-stats",
"CLOSE ws-stats connection {connection_id} for station {station_id} ({reconnections})"
"CLOSE ws-stats connection {connection_id} for station {station_id} ({reconnections}) in {duration}",
duration=FormatDuration(duration_ms),
);
});

Expand Down Expand Up @@ -311,3 +312,26 @@ impl Handler for WsConnectionHandler {
r.into()
}
}

struct FormatDuration(f64);
impl core::fmt::Display for FormatDuration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
const S: u64 = 1000;
const M: u64 = 60 * S;
const H: u64 = 60 * M;

let d = self.0.round() as u64;

let h = d / H;
let m = (d % H) / M;
let s = (d % M) / S;

if h != 0 {
write!(f, "{h}h {m}m {s}s")
} else if m != 0 {
write!(f, "{m}m {s}s")
} else {
write!(f, "{s}s")
}
}
}

0 comments on commit c50a1f6

Please sign in to comment.