From 9e21716965f6f6250f6fe4b3449a66f20794d3d9 Mon Sep 17 00:00:00 2001 From: Ruslan Skorb Date: Tue, 25 May 2021 08:14:29 +0300 Subject: [PATCH] Add format_bytes(bytes:) function to convert bytes to human readable string. (#5449) --- chia/cmds/farm_funcs.py | 18 +++--------------- chia/cmds/netspace_funcs.py | 12 ++---------- chia/cmds/show.py | 12 ++---------- chia/util/misc.py | 17 +++++++++++++++++ tests/util/misc.py | 18 ++++++++++++++++++ 5 files changed, 42 insertions(+), 35 deletions(-) diff --git a/chia/cmds/farm_funcs.py b/chia/cmds/farm_funcs.py index 7419f2a44c14..42811ffe5bec 100644 --- a/chia/cmds/farm_funcs.py +++ b/chia/cmds/farm_funcs.py @@ -11,6 +11,7 @@ from chia.util.config import load_config from chia.util.default_root import DEFAULT_ROOT_PATH from chia.util.ints import uint16 +from chia.util.misc import format_bytes from chia.util.misc import format_minutes SECONDS_PER_BLOCK = (24 * 3600) / 4608 @@ -217,27 +218,14 @@ async def summary(rpc_port: int, wallet_rpc_port: int, harvester_rpc_port: int, print(f"Plot count: {len(plots['plots'])}") print("Total size of plots: ", end="") - plots_space_human_readable = total_plot_size / 1024 ** 3 - if plots_space_human_readable >= 1024 ** 2: - plots_space_human_readable = plots_space_human_readable / (1024 ** 2) - print(f"{plots_space_human_readable:.3f} PiB") - elif plots_space_human_readable >= 1024: - plots_space_human_readable = plots_space_human_readable / 1024 - print(f"{plots_space_human_readable:.3f} TiB") - else: - print(f"{plots_space_human_readable:.3f} GiB") + print(format_bytes(total_plot_size)) else: print("Plot count: Unknown") print("Total size of plots: Unknown") if blockchain_state is not None: print("Estimated network space: ", end="") - network_space_human_readable = blockchain_state["space"] / 1024 ** 4 - if network_space_human_readable >= 1024: - network_space_human_readable = network_space_human_readable / 1024 - print(f"{network_space_human_readable:.3f} PiB") - else: - print(f"{network_space_human_readable:.3f} TiB") + print(format_bytes(blockchain_state["space"])) else: print("Estimated network space: Unknown") diff --git a/chia/cmds/netspace_funcs.py b/chia/cmds/netspace_funcs.py index 4ee713c9e5c9..aba126c61fb5 100644 --- a/chia/cmds/netspace_funcs.py +++ b/chia/cmds/netspace_funcs.py @@ -5,6 +5,7 @@ from chia.util.config import load_config from chia.util.default_root import DEFAULT_ROOT_PATH from chia.util.ints import uint16 +from chia.util.misc import format_bytes async def netstorge_async(rpc_port: int, delta_block_height: str, start: str) -> None: @@ -59,16 +60,7 @@ async def netstorge_async(rpc_port: int, delta_block_height: str, start: str) -> f"VDF Iterations: {newer_block_header.total_iters}\n" f"Header Hash: 0x{newer_block_header.header_hash}\n" ) - network_space_terabytes_estimate = network_space_bytes_estimate / 1024 ** 4 - if network_space_terabytes_estimate >= 1024: - network_space_terabytes_estimate = network_space_terabytes_estimate / 1024 - if network_space_terabytes_estimate >= 1024: - network_space_terabytes_estimate = network_space_terabytes_estimate / 1024 - print(f"The network has an estimated {network_space_terabytes_estimate:.3f} EiB") - else: - print(f"The network has an estimated {network_space_terabytes_estimate:.3f} PiB") - else: - print(f"The network has an estimated {network_space_terabytes_estimate:.3f} TiB") + print(format_bytes(network_space_bytes_estimate)) except Exception as e: if isinstance(e, aiohttp.ClientConnectorError): diff --git a/chia/cmds/show.py b/chia/cmds/show.py index 9cda057664df..d698a0f4167f 100644 --- a/chia/cmds/show.py +++ b/chia/cmds/show.py @@ -28,6 +28,7 @@ async def show_async( from chia.util.config import load_config from chia.util.default_root import DEFAULT_ROOT_PATH from chia.util.ints import uint16 + from chia.util.misc import format_bytes try: config = load_config(DEFAULT_ROOT_PATH, "config.yaml") @@ -85,16 +86,7 @@ async def show_async( ) print("Estimated network space: ", end="") - network_space_human_readable = blockchain_state["space"] / 1024 ** 4 - if network_space_human_readable >= 1024: - network_space_human_readable = network_space_human_readable / 1024 - if network_space_human_readable >= 1024: - network_space_human_readable = network_space_human_readable / 1024 - print(f"{network_space_human_readable:.3f} EiB") - else: - print(f"{network_space_human_readable:.3f} PiB") - else: - print(f"{network_space_human_readable:.3f} TiB") + print(format_bytes(blockchain_state["space"])) print(f"Current difficulty: {difficulty}") print(f"Current VDF sub_slot_iters: {sub_slot_iters}") print("Total iterations since the start of the blockchain:", total_iters) diff --git a/chia/util/misc.py b/chia/util/misc.py index 13893f3e5b7f..9acd6e66da3d 100644 --- a/chia/util/misc.py +++ b/chia/util/misc.py @@ -1,3 +1,20 @@ +def format_bytes(bytes: int) -> str: + + if not isinstance(bytes, int) or bytes < 0: + return "Invalid" + + labels = {0: "MiB", 1: "GiB", 2: "TiB", 3: "PiB", 4: "EiB", 5: "ZiB", 6: "YiB"} + + base = 1024 + value = bytes / base ** 2 + key = 0 + while value >= base and key < 6: + value /= base + key += 1 + + return f"{value:.3f} {labels[key]}" + + def format_minutes(minutes: int) -> str: if not isinstance(minutes, int): diff --git a/tests/util/misc.py b/tests/util/misc.py index 468c740e2b60..98e8c4d2c26e 100644 --- a/tests/util/misc.py +++ b/tests/util/misc.py @@ -1,8 +1,26 @@ import pytest +from chia.util.misc import format_bytes from chia.util.misc import format_minutes class TestMisc: + @pytest.mark.asyncio + async def test_format_bytes(self): + assert format_bytes(None) == "Invalid" + assert format_bytes(dict()) == "Invalid" + assert format_bytes("some bytes") == "Invalid" + assert format_bytes(-1024) == "Invalid" + assert format_bytes(0) == "0.000 MiB" + assert format_bytes(1024) == "0.001 MiB" + assert format_bytes(1024 ** 2) == "1.000 MiB" + assert format_bytes(1024 ** 3) == "1.000 GiB" + assert format_bytes(1024 ** 4) == "1.000 TiB" + assert format_bytes(1024 ** 5) == "1.000 PiB" + assert format_bytes(1024 ** 6) == "1.000 EiB" + assert format_bytes(1024 ** 7) == "1.000 ZiB" + assert format_bytes(1024 ** 8) == "1.000 YiB" + assert format_bytes(1024 ** 9) == "1024.000 YiB" + @pytest.mark.asyncio async def test_format_minutes(self): assert format_minutes(None) == "Invalid"