fix(platform): address review — format_bytes rollup, storage bar visibility

- Rename _format_bytes → format_bytes (public API, used cross-module)
- Add unit boundary rollup (1024 KB → 1.0 MB) matching frontend formatBytes
- Show WorkspaceStorageSection even when token limits are null
- Move import to top level in routes.py

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Nicholas Tindle
2026-04-24 14:32:24 -05:00
parent 938cf9ea5f
commit 33eb9e9ad9
3 changed files with 21 additions and 13 deletions

View File

@@ -27,7 +27,7 @@ from backend.data.workspace import (
soft_delete_workspace_file,
)
from backend.util.settings import Config
from backend.util.workspace import WorkspaceManager, _format_bytes
from backend.util.workspace import WorkspaceManager, format_bytes
from backend.util.workspace_storage import get_workspace_storage
@@ -286,8 +286,8 @@ async def upload_file(
status_code=413,
detail=(
f"Storage limit exceeded. "
f"You've used {_format_bytes(new_total)} of your "
f"{_format_bytes(storage_limit_bytes)} quota. "
f"You've used {format_bytes(new_total)} of your "
f"{format_bytes(storage_limit_bytes)} quota. "
f"Delete some files or upgrade your plan for more storage."
),
)

View File

@@ -21,15 +21,18 @@ from backend.util.virus_scanner import scan_content_safe
from backend.util.workspace_storage import compute_file_checksum, get_workspace_storage
def _format_bytes(n: int) -> str:
def format_bytes(n: int) -> str:
"""Format bytes as a human-readable string (e.g. 250 MB, 1.0 GB)."""
if n < 1024:
KB, MB, GB = 1024, 1024**2, 1024**3
if n < KB:
return f"{n} B"
if n < 1024 * 1024:
return f"{n / 1024:.0f} KB"
if n < 1024 * 1024 * 1024:
return f"{n / (1024 * 1024):.0f} MB"
return f"{n / (1024 * 1024 * 1024):.1f} GB"
if n < MB:
kb = round(n / KB)
return f"{n / MB:.1f} MB" if kb >= 1024 else f"{kb} KB"
if n < GB:
mb = round(n / MB)
return f"{n / GB:.1f} GB" if mb >= 1024 else f"{mb} MB"
return f"{n / GB:.1f} GB"
logger = logging.getLogger(__name__)
@@ -225,8 +228,8 @@ class WorkspaceManager:
if storage_limit > 0 and projected_usage > storage_limit:
raise ValueError(
f"Storage limit exceeded. "
f"You've used {_format_bytes(current_usage)} of your "
f"{_format_bytes(storage_limit)} quota. "
f"You've used {format_bytes(current_usage)} of your "
f"{format_bytes(storage_limit)} quota. "
f"Delete some files or upgrade your plan for more storage."
)
if storage_limit > 0 and projected_usage / storage_limit >= 0.8:

View File

@@ -166,7 +166,12 @@ export function UsagePanelContent({
if (!daily && !weekly) {
return (
<div className="text-xs text-neutral-500">No usage limits configured</div>
<div className="flex flex-col gap-3">
<div className="text-xs text-neutral-500">
No usage limits configured
</div>
<WorkspaceStorageSection />
</div>
);
}