improve ux, moved functionality for better ui experience on app and extension.

This commit is contained in:
Bryan Van Deusen
2026-01-26 11:38:51 -05:00
parent 74b1ecf2cf
commit 2a0e37a73d
12 changed files with 369 additions and 78 deletions
+37 -36
View File
@@ -1,54 +1,37 @@
"""Settings API endpoints."""
import json
import os
from pathlib import Path
from quart import Blueprint, request, jsonify, current_app
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.setting import Setting, DEFAULT_SETTINGS, EXTENSION_API_KEY_SETTING, generate_api_key
from app.models.setting import Setting, DEFAULT_SETTINGS, EXTENSION_API_KEY_SETTING, STORAGE_STATS_SETTING, generate_api_key
from app.config import get_settings
bp = Blueprint("settings", __name__)
def get_storage_stats():
"""Calculate storage statistics for the downloads directory."""
config = get_settings()
downloads_path = Path(config.download_path)
async def get_cached_storage_stats(session: AsyncSession) -> dict:
"""Get cached storage statistics from the database.
if not downloads_path.exists():
return {"total_size": 0, "total_size_formatted": "0 B", "file_count": 0}
Returns cached stats if available, otherwise returns placeholder.
Stats are calculated by a background Celery task.
"""
result = await session.execute(
select(Setting).where(Setting.key == STORAGE_STATS_SETTING)
)
setting = result.scalar_one_or_none()
total_size = 0
file_count = 0
try:
for root, dirs, files in os.walk(downloads_path):
for f in files:
fp = os.path.join(root, f)
try:
total_size += os.path.getsize(fp)
file_count += 1
except OSError:
pass
except Exception as e:
current_app.logger.error(f"Error calculating storage stats: {e}")
return {"total_size": 0, "total_size_formatted": "Error", "file_count": 0}
# Format size
def format_size(size):
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if size < 1024:
return f"{size:.1f} {unit}"
size /= 1024
return f"{size:.1f} PB"
if setting and setting.value:
return setting.value
# No cached stats yet - return placeholder
return {
"total_size": total_size,
"total_size_formatted": format_size(total_size),
"file_count": file_count,
"total_size": 0,
"total_size_formatted": "Calculating...",
"file_count": 0,
"calculated_at": None,
}
@@ -112,8 +95,8 @@ async def get_all_settings():
api_key = await get_or_create_extension_api_key(session)
settings_dict[EXTENSION_API_KEY_SETTING] = api_key
# Include storage statistics
storage_stats = get_storage_stats()
# Get cached storage statistics (calculated by background task)
storage_stats = await get_cached_storage_stats(session)
return jsonify({"settings": settings_dict, "storage_stats": storage_stats})
@@ -231,6 +214,24 @@ async def update_gallery_dl_config():
return jsonify({"error": f"Failed to write config: {e}"}), 500
@bp.route("/storage-stats/refresh", methods=["POST"])
async def refresh_storage_stats():
"""Trigger a refresh of storage statistics.
This queues a background task to recalculate storage stats.
"""
from app.tasks.maintenance import update_storage_stats
try:
# Queue the task
update_storage_stats.delay()
current_app.logger.info("Queued storage stats refresh")
return jsonify({"message": "Storage stats refresh queued"})
except Exception as e:
current_app.logger.error(f"Failed to queue storage stats refresh: {e}")
return jsonify({"error": str(e)}), 500
@bp.route("/logs", methods=["GET"])
async def get_logs():
"""Get recent download logs from download metadata.