Fix stats counting to use ownership-based database queries
- Media: Only count video/image/audio/unknown types, not effects/recipes - Effects: Use database count_user_items instead of filesystem scan - Recipes: Use database count_user_items instead of loading all recipes This ensures stats reflect user ownership via item_types table, and prevents effects from being double-counted as media. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,21 +19,23 @@ router = APIRouter()
|
|||||||
async def get_user_stats(actor_id: str) -> dict:
|
async def get_user_stats(actor_id: str) -> dict:
|
||||||
"""Get stats for a user."""
|
"""Get stats for a user."""
|
||||||
import database
|
import database
|
||||||
from ..services.recipe_service import RecipeService
|
|
||||||
from ..services.run_service import RunService
|
from ..services.run_service import RunService
|
||||||
from ..dependencies import get_redis_client, get_cache_manager
|
from ..dependencies import get_redis_client, get_cache_manager
|
||||||
|
|
||||||
stats = {}
|
stats = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
stats["media"] = await database.count_user_items(actor_id)
|
# Count only actual media types (video, image, audio), not effects/recipes
|
||||||
|
media_count = 0
|
||||||
|
for media_type in ["video", "image", "audio", "unknown"]:
|
||||||
|
media_count += await database.count_user_items(actor_id, item_type=media_type)
|
||||||
|
stats["media"] = media_count
|
||||||
except Exception:
|
except Exception:
|
||||||
stats["media"] = 0
|
stats["media"] = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
recipe_service = RecipeService(get_redis_client(), get_cache_manager())
|
# Count user's recipes from database (ownership-based)
|
||||||
recipes = await recipe_service.list_recipes(actor_id)
|
stats["recipes"] = await database.count_user_items(actor_id, item_type="recipe")
|
||||||
stats["recipes"] = len(recipes)
|
|
||||||
except Exception:
|
except Exception:
|
||||||
stats["recipes"] = 0
|
stats["recipes"] = 0
|
||||||
|
|
||||||
@@ -51,11 +53,8 @@ async def get_user_stats(actor_id: str) -> dict:
|
|||||||
stats["storage"] = 0
|
stats["storage"] = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
effects_dir = Path(get_cache_manager().cache_dir) / "_effects"
|
# Count user's effects from database (ownership-based)
|
||||||
if effects_dir.exists():
|
stats["effects"] = await database.count_user_items(actor_id, item_type="effect")
|
||||||
stats["effects"] = len([d for d in effects_dir.iterdir() if d.is_dir()])
|
|
||||||
else:
|
|
||||||
stats["effects"] = 0
|
|
||||||
except Exception:
|
except Exception:
|
||||||
stats["effects"] = 0
|
stats["effects"] = 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user