Add pagination and API improvements

- Add pagination to effects list with infinite scroll
- Refactor home stats into reusable get_user_stats function
- Add /api/stats endpoint for CLI/API clients
- Add has_more flag to recipes listing
- Add JSON API support to storage type page

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
gilesb
2026-01-12 14:22:09 +00:00
parent ee8719ac0b
commit 7d24ba4dd7
5 changed files with 98 additions and 47 deletions

View File

@@ -16,12 +16,68 @@ from ..dependencies import get_templates, get_current_user
router = APIRouter()
async def get_user_stats(actor_id: str) -> dict:
"""Get stats for a user."""
import database
from ..services.recipe_service import RecipeService
from ..services.run_service import RunService
from ..dependencies import get_redis_client, get_cache_manager
stats = {}
try:
stats["media"] = await database.count_user_items(actor_id)
except Exception:
stats["media"] = 0
try:
recipe_service = RecipeService(get_redis_client(), get_cache_manager())
recipes = await recipe_service.list_recipes(actor_id)
stats["recipes"] = len(recipes)
except Exception:
stats["recipes"] = 0
try:
run_service = RunService(database, get_redis_client(), get_cache_manager())
runs = await run_service.list_runs(actor_id)
stats["runs"] = len(runs)
except Exception:
stats["runs"] = 0
try:
storage_providers = await database.get_user_storage_providers(actor_id)
stats["storage"] = len(storage_providers) if storage_providers else 0
except Exception:
stats["storage"] = 0
try:
effects_dir = Path(get_cache_manager().cache_dir) / "_effects"
if effects_dir.exists():
stats["effects"] = len([d for d in effects_dir.iterdir() if d.is_dir()])
else:
stats["effects"] = 0
except Exception:
stats["effects"] = 0
return stats
@router.get("/api/stats")
async def api_stats(request: Request):
"""Get user stats as JSON for CLI and API clients."""
user = await get_current_user(request)
if not user:
raise HTTPException(401, "Authentication required")
stats = await get_user_stats(user.actor_id)
return stats
@router.get("/")
async def home(request: Request):
"""
Home page - show README and stats.
"""
import database
user = await get_current_user(request)
# Load README
@@ -36,46 +92,7 @@ async def home(request: Request):
# Get stats for current user
stats = {}
if user:
try:
stats["media"] = await database.count_user_items(user.actor_id)
except Exception:
pass
try:
from ..services.recipe_service import RecipeService
from ..dependencies import get_redis_client, get_cache_manager
import logging
logger = logging.getLogger(__name__)
recipe_service = RecipeService(get_redis_client(), get_cache_manager())
recipes = await recipe_service.list_recipes(user.actor_id)
stats["recipes"] = len(recipes)
logger.info(f"Home page: found {len(recipes)} recipes for {user.actor_id}")
except Exception as e:
import logging
logging.getLogger(__name__).error(f"Failed to get recipe count: {e}")
try:
from ..services.run_service import RunService
from ..dependencies import get_redis_client, get_cache_manager
run_service = RunService(database, get_redis_client(), get_cache_manager())
runs = await run_service.list_runs(user.actor_id)
stats["runs"] = len(runs)
except Exception:
pass
try:
storage_providers = await database.get_user_storage_providers(user.actor_id)
stats["storage"] = len(storage_providers) if storage_providers else 0
except Exception:
pass
try:
# Effects are stored in _effects/ directory, not in cache
from pathlib import Path
from ..dependencies import get_cache_manager
effects_dir = Path(get_cache_manager().cache_dir) / "_effects"
if effects_dir.exists():
stats["effects"] = len([d for d in effects_dir.iterdir() if d.is_dir()])
else:
stats["effects"] = 0
except Exception:
pass
stats = await get_user_stats(user.actor_id)
templates = get_templates(request)
return render(templates, "home.html", request,