From 861dbd8a12d3a93ad2d1730cb19f25780ecf3764 Mon Sep 17 00:00:00 2001 From: Vito Sansevero Date: Wed, 28 May 2025 13:00:10 -0700 Subject: [PATCH] refactor(api): clean up and enhance category handling --- py/api.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/py/api.py b/py/api.py index ae49e8d..02cfb25 100644 --- a/py/api.py +++ b/py/api.py @@ -168,9 +168,6 @@ class PromptManagerAPI: async def save_settings_route(request): return await self.save_settings(request) - @routes.get("/prompt_manager/stats") - async def get_statistics_route(request): - return await self.get_statistics(request) # Individual prompt management @routes.put("/prompt_manager/prompts/{prompt_id}") @@ -557,7 +554,7 @@ class PromptManagerAPI: total_prompts = cursor.fetchone()["total"] cursor = conn.execute( - "SELECT COUNT(DISTINCT category) as total FROM prompts WHERE category IS NOT NULL" + "SELECT COUNT(DISTINCT TRIM(category)) as total FROM prompts WHERE category IS NOT NULL AND TRIM(category) != ''" ) total_categories = cursor.fetchone()["total"] @@ -577,16 +574,36 @@ class PromptManagerAPI: except: continue + # Get all categories for debugging (including raw data) + cursor = conn.execute( + "SELECT DISTINCT category FROM prompts WHERE category IS NOT NULL ORDER BY category" + ) + raw_categories = [row['category'] for row in cursor.fetchall()] + + cursor = conn.execute( + "SELECT DISTINCT TRIM(category) as category FROM prompts WHERE category IS NOT NULL AND TRIM(category) != '' ORDER BY category" + ) + filtered_categories = [row['category'] for row in cursor.fetchall()] + + # Debug logging with detailed category info + self.logger.debug(f"Statistics calculated - Prompts: {total_prompts}, Categories: {total_categories}, Tags: {len(all_tags)}, Avg Rating: {avg_rating}") + self.logger.debug(f"Raw categories from DB: {[(cat, len(cat), repr(cat)) for cat in raw_categories]}") + self.logger.debug(f"Filtered categories: {[(cat, len(cat), repr(cat)) for cat in filtered_categories]}") + return web.json_response( { "success": True, "stats": { "total_prompts": total_prompts, - "unique_categories": total_categories, + "total_categories": total_categories, + "unique_categories": total_categories, # Keep both for compatibility "total_tags": len(all_tags), "average_rating": ( round(avg_rating, 2) if avg_rating else None ), + "avg_rating": ( + round(avg_rating, 2) if avg_rating else None + ), # Keep both for compatibility "recent_prompts": total_prompts, # For now, use total as recent count }, }