ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
refactor(utils): one byte formatter for prose, not three copies of it
Juan Denis803f107e1499
diff --git a/backend/app/services/capacity_service.py b/backend/app/services/capacity_service.pyindex 6b172cc7..4a63ee11 100644--- a/backend/app/services/capacity_service.py+++ b/backend/app/services/capacity_service.py@@ -66,16 +66,10 @@ def parse_size(value):return None-def format_size(num_bytes):- """Bytes -> a short human string ('512 MB', '1.5 GB')."""- if num_bytes is None:- return 'unknown'- step = float(num_bytes)- for unit in ('B', 'KB', 'MB', 'GB', 'TB'):- if step < 1024 or unit == 'TB':- return f'{step:.0f} {unit}' if step >= 10 or unit == 'B' else f'{step:.1f} {unit}'- step /= 1024- return f'{step:.1f} TB'+# Re-exported so existing `from app.services.capacity_service import format_size`+# callers keep working; the implementation moved to the leaf utils module when+# host_inventory_service turned out to hold a byte-for-byte copy (plan 75 §F1).+from app.utils.formatting import format_size # noqa: E402,F401def template_footprint(template):diff --git a/backend/app/services/host_inventory_service.py b/backend/app/services/host_inventory_service.pyindex 43c68558..9392d031 100644--- a/backend/app/services/host_inventory_service.py+++ b/backend/app/services/host_inventory_service.py@@ -22,6 +22,8 @@ import osimport psutil+from app.utils.formatting import format_size+logger = logging.getLogger(__name__)#: Below this, image pulls and backups start failing before RAM is ever the@@ -281,17 +283,6 @@ def _detect_container():# Diffing# --------------------------------------------------------------------------- #-def _format_bytes(num_bytes):- if num_bytes is None:- return 'unknown'- step = float(num_bytes)- for unit in ('B', 'KB', 'MB', 'GB', 'TB'):- if step < 1024 or unit == 'TB':- return f'{step:.0f} {unit}' if step >= 10 or unit == 'B' else f'{step:.1f} {unit}'- step /= 1024- return f'{step:.1f} TB'--#: Ignore total-size jitter below this. A filesystem's reported total can move#: by a few blocks across remounts without anything having actually changed._SIZE_NOISE_FLOOR = 64 * 1024 ** 2 # 64 MB@@ -310,8 +301,8 @@ def diff(previous, current):for field, label, fmt in (('cpu_cores', 'CPU cores', str),- ('ram_bytes', 'RAM', _format_bytes),- ('swap_bytes', 'Swap', _format_bytes),+ ('ram_bytes', 'RAM', format_size),+ ('swap_bytes', 'Swap', format_size),):before, after = previous.get(field), current.get(field)# A probe that failed this time must not be reported as a loss.@@ -336,7 +327,7 @@ def diff(previous, current):'from': None,'to': mount,'summary': (f'New filesystem {mount} '- f'({_format_bytes(entry.get("total"))}, {entry.get("device")})'),+ f'({format_size(entry.get("total"))}, {entry.get("device")})'),})for mount in sorted(before_fs.keys() - after_fs.keys()):@@ -360,8 +351,8 @@ def diff(previous, current):'kind': 'resized','from': before_total,'to': after_total,- 'summary': (f'{mount} {_format_bytes(before_total)} → '- f'{_format_bytes(after_total)}'),+ 'summary': (f'{mount} {format_size(before_total)} → '+ f'{format_size(after_total)}'),})return changes@@ -416,8 +407,8 @@ def advisories(filesystems=None):'severity': 'warning','mountpoint': short['mountpoint'],'summary': (- f'{short["mountpoint"]} has only {_format_bytes(short["free"])} free '- f'while {spare["mountpoint"]} has {_format_bytes(spare["free"])} '+ f'{short["mountpoint"]} has only {format_size(short["free"])} free '+ f'while {spare["mountpoint"]} has {format_size(spare["free"])} 'f'spare. Consider relocating Docker data or backups onto it.'),})@@ -430,7 +421,7 @@ def advisories(filesystems=None):'mountpoint': short['mountpoint'],'summary': (f'{short["mountpoint"]} is at {short.get("percent")}% — '- f'{_format_bytes(short["free"])} free.'+ f'{format_size(short["free"])} free.'),})diff --git a/backend/app/utils/formatting.py b/backend/app/utils/formatting.pyindex a7e1000e..9d0af798 100644--- a/backend/app/utils/formatting.py+++ b/backend/app/utils/formatting.py@@ -31,3 +31,26 @@ def format_bytes(n, precision=1, suffix_sep=' '):return f'{n:.{precision}f}{suffix_sep}{suffix}'n /= 1024return f'{n:.{precision}f}{suffix_sep}PB'+++def format_size(num_bytes):+ """Bytes -> a short human string for prose ('512 MB', '1.5 GB', 'unknown').++ Differs from :func:`format_bytes` in two ways that matter where the result+ is read as a sentence rather than a table cell: an unknown value reads as+ ``'unknown'`` rather than an empty string, and precision adapts so figures+ land as '512 MB' instead of '512.0 MB' while small ones keep a decimal.++ Lives here rather than in a service because capacity_service and+ host_inventory_service both need it, and this module imports nothing — the+ duplicate that prompted this was written to dodge an import cycle that+ cannot exist (plan 75 §F1).+ """+ if num_bytes is None:+ return 'unknown'+ step = float(num_bytes)+ for unit in ('B', 'KB', 'MB', 'GB', 'TB'):+ if step < 1024 or unit == 'TB':+ return f'{step:.0f} {unit}' if step >= 10 or unit == 'B' else f'{step:.1f} {unit}'+ step /= 1024+ return f'{step:.1f} TB'