Skip to content

Commit

Permalink
Add format_bytes(bytes:) function to convert bytes to human readable …
Browse files Browse the repository at this point in the history
…string. (Chia-Network#5449)
  • Loading branch information
ruslanskorb authored May 25, 2021
1 parent 0fffed0 commit 9e21716
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 35 deletions.
18 changes: 3 additions & 15 deletions chia/cmds/farm_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
12 changes: 2 additions & 10 deletions chia/cmds/netspace_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
12 changes: 2 additions & 10 deletions chia/cmds/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions chia/util/misc.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
18 changes: 18 additions & 0 deletions tests/util/misc.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 9e21716

Please sign in to comment.