Skip to content

Commit 9cd2637

Browse files
committed
refactor: display sizes in a human readable format
1 parent 35f658f commit 9cd2637

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

mithril-client-cli/src/commands/cardano_db/list.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use cli_table::{format::Justify, print_stdout, Cell, Table};
33

44
use crate::{
55
commands::{client_builder_with_fallback_genesis_key, SharedArgs},
6+
utils::CardanoDbUtils,
67
CommandContext,
78
};
89
use mithril_client::MithrilResult;
@@ -39,7 +40,7 @@ impl CardanoDbListCommand {
3940
format!("{}", item.beacon.immutable_file_number).cell(),
4041
item.network.cell(),
4142
item.digest.cell(),
42-
item.size.cell(),
43+
CardanoDbUtils::format_bytes_to_gigabytes(item.size).cell(),
4344
format!("{}", item.locations.len()).cell(),
4445
item.created_at.to_string().cell(),
4546
]

mithril-client-cli/src/commands/cardano_db/show.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cli_table::{print_stdout, Cell, Table};
44

55
use crate::{
66
commands::{client_builder_with_fallback_genesis_key, SharedArgs},
7-
utils::ExpanderUtils,
7+
utils::{CardanoDbUtils, ExpanderUtils},
88
CommandContext,
99
};
1010
use mithril_client::MithrilResult;
@@ -70,7 +70,7 @@ impl CardanoDbShowCommand {
7070
vec!["Digest".cell(), cardano_db_message.digest.cell()],
7171
vec![
7272
"Size".cell(),
73-
format!("{}", &cardano_db_message.size).cell(),
73+
CardanoDbUtils::format_bytes_to_gigabytes(cardano_db_message.size).cell(),
7474
],
7575
vec![
7676
"Cardano node version".cell(),

mithril-client-cli/src/commands/cardano_db_v2/list.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use mithril_client::MithrilResult;
55

66
use crate::{
77
commands::{client_builder_with_fallback_genesis_key, SharedArgs},
8+
utils::CardanoDbUtils,
89
CommandContext,
910
};
1011

@@ -40,7 +41,8 @@ impl CardanoDbListCommand {
4041
format!("{}", item.beacon.immutable_file_number).cell(),
4142
item.hash.cell(),
4243
item.merkle_root.cell(),
43-
item.total_db_size_uncompressed.cell(),
44+
CardanoDbUtils::format_bytes_to_gigabytes(item.total_db_size_uncompressed)
45+
.cell(),
4446
format!("{}", item.compression_algorithm).cell(),
4547
item.cardano_node_version.cell(),
4648
item.created_at.to_string().cell(),

mithril-client-cli/src/commands/cardano_db_v2/show.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cli_table::{print_stdout, Cell, CellStruct, Table};
44

55
use crate::{
66
commands::{client_builder_with_fallback_genesis_key, SharedArgs},
7-
utils::ExpanderUtils,
7+
utils::{CardanoDbUtils, ExpanderUtils},
88
CommandContext,
99
};
1010

@@ -78,7 +78,10 @@ impl CardanoDbShowCommand {
7878
vec!["Merkle root".cell(), cardano_db_message.merkle_root.cell()],
7979
vec![
8080
"Database size".cell(),
81-
format!("{}", &cardano_db_message.total_db_size_uncompressed).cell(),
81+
CardanoDbUtils::format_bytes_to_gigabytes(
82+
cardano_db_message.total_db_size_uncompressed,
83+
)
84+
.cell(),
8285
],
8386
vec![
8487
"Cardano node version".cell(),

mithril-client-cli/src/utils/cardano_db.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ impl CardanoDbUtils {
4242
res = future => res,
4343
}
4444
}
45+
46+
pub fn format_bytes_to_gigabytes(bytes: u64) -> String {
47+
let size_in_giga = bytes as f64 / (1024.0 * 1024.0 * 1024.0);
48+
49+
format!("{:.2} GiB", size_in_giga)
50+
}
4551
}
4652

4753
#[cfg(test)]
@@ -80,4 +86,26 @@ mod test {
8086
error
8187
);
8288
}
89+
90+
#[test]
91+
fn format_bytes_to_gigabytes_zero() {
92+
let one_gigabyte = 1024 * 1024 * 1024;
93+
94+
assert_eq!(CardanoDbUtils::format_bytes_to_gigabytes(0), "0.00 GiB");
95+
96+
assert_eq!(
97+
CardanoDbUtils::format_bytes_to_gigabytes(one_gigabyte),
98+
"1.00 GiB"
99+
);
100+
101+
assert_eq!(
102+
CardanoDbUtils::format_bytes_to_gigabytes(one_gigabyte / 2),
103+
"0.50 GiB"
104+
);
105+
106+
assert_eq!(
107+
CardanoDbUtils::format_bytes_to_gigabytes(one_gigabyte * 10),
108+
"10.00 GiB"
109+
);
110+
}
83111
}

0 commit comments

Comments
 (0)