feat(downloads): expose throughput metrics in progress APIs

This commit is contained in:
pixelpaws
2025-10-13 14:39:31 +08:00
parent 01bbaa31a8
commit eb76468280
9 changed files with 310 additions and 52 deletions

View File

@@ -7,6 +7,7 @@ from typing import Any, Dict, List
import pytest
from py.services.download_coordinator import DownloadCoordinator
from py.services.downloader import DownloadProgress
from py.services.metadata_sync_service import MetadataSyncService
from py.services.preview_asset_service import PreviewAssetService
from py.services.tag_update_service import TagUpdateService
@@ -191,10 +192,17 @@ def test_download_coordinator_emits_progress() -> None:
class DownloadManagerStub:
def __init__(self) -> None:
self.calls: List[Dict[str, Any]] = []
self.snapshot = DownloadProgress(
percent_complete=25.0,
bytes_downloaded=256,
total_bytes=1024,
bytes_per_second=128.0,
timestamp=0.0,
)
async def download_from_civitai(self, **kwargs) -> Dict[str, Any]:
self.calls.append(kwargs)
await kwargs["progress_callback"](10)
await kwargs["progress_callback"](self.snapshot)
return {"success": True}
async def cancel_download(self, download_id: str) -> Dict[str, Any]:
@@ -216,6 +224,12 @@ def test_download_coordinator_emits_progress() -> None:
assert result["success"] is True
assert manager_stub.calls
assert ws_stub.progress_events
expected_progress = round(manager_stub.snapshot.percent_complete)
first_event = ws_stub.progress_events[0]
assert first_event["progress"] == expected_progress
assert first_event["bytes_downloaded"] == manager_stub.snapshot.bytes_downloaded
assert first_event["total_bytes"] == manager_stub.snapshot.total_bytes
assert first_event["bytes_per_second"] == manager_stub.snapshot.bytes_per_second
cancel_result = asyncio.run(coordinator.cancel_download(result["download_id"]))
assert cancel_result["success"] is True

View File

@@ -86,10 +86,29 @@ async def test_broadcast_download_progress_tracks_state(manager):
download_id = "abc"
manager._download_websockets[download_id] = ws
await manager.broadcast_download_progress(download_id, {"progress": 55})
await manager.broadcast_download_progress(
download_id,
{
"progress": 55,
"bytes_downloaded": 512,
"total_bytes": 1024,
"bytes_per_second": 128.0,
},
)
assert ws.messages == [{"progress": 55}]
assert manager.get_download_progress(download_id)["progress"] == 55
assert ws.messages == [
{
"progress": 55,
"bytes_downloaded": 512,
"total_bytes": 1024,
"bytes_per_second": 128.0,
}
]
stored = manager.get_download_progress(download_id)
assert stored["progress"] == 55
assert stored["bytes_downloaded"] == 512
assert stored["total_bytes"] == 1024
assert stored["bytes_per_second"] == 128.0
async def test_broadcast_download_progress_to_multiple_updates(manager):
@@ -107,7 +126,9 @@ async def test_broadcast_download_progress_to_multiple_updates(manager):
async def test_broadcast_download_progress_missing_socket(manager):
await manager.broadcast_download_progress("missing", {"progress": 30})
# Progress should be stored even without a live websocket
assert manager.get_download_progress("missing")["progress"] == 30
missing = manager.get_download_progress("missing")
assert missing["progress"] == 30
assert "bytes_downloaded" not in missing
async def test_auto_organize_progress_helpers(manager):