diff --git a/database/operations.py b/database/operations.py index 122d23f..fcf009a 100644 --- a/database/operations.py +++ b/database/operations.py @@ -361,7 +361,7 @@ class PromptDatabase: """ with self.model.get_connection() as conn: cursor = conn.execute( - "SELECT DISTINCT category FROM prompts WHERE category IS NOT NULL ORDER BY category" + "SELECT DISTINCT TRIM(category) as category FROM prompts WHERE category IS NOT NULL AND TRIM(category) != '' ORDER BY category" ) return [row['category'] for row in cursor.fetchall()] 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 }, } diff --git a/web/admin.html b/web/admin.html index e0ade2d..3c8fbda 100644 --- a/web/admin.html +++ b/web/admin.html @@ -1022,8 +1022,9 @@ // This prevents overwriting the correct total counts from the API const currentTotal = document.getElementById("totalPrompts").textContent; - // Only update if the current total is still the default "-" or if we have more prompts locally than the API count - if (currentTotal === "-" || (this.prompts.length > parseInt(currentTotal) && this.prompts.length > 0)) { + // Only update if the current total is still the default "-" + if (currentTotal === "-") { + // Show local counts as fallback only when API hasn't loaded yet document.getElementById("totalPrompts").textContent = this.prompts.length; const uniqueCategories = new Set(this.prompts.map(p => p.category).filter(Boolean)); @@ -1062,7 +1063,6 @@ if (data.success) { this.prompts = data.results; this.renderPrompts(); - this.updateLocalStats(); document.getElementById("resultsTitle").textContent = "Search Results"; } } @@ -1335,6 +1335,8 @@ if (response.ok) { this.showNotification("Tag added", "success"); + // Refresh both stats and results + await this.loadStatistics(); this.search(); } } catch (error) { @@ -1352,6 +1354,8 @@ if (response.ok) { this.showNotification("Tag removed", "success"); + // Refresh both stats and results + await this.loadStatistics(); this.search(); } } catch (error) { @@ -1459,6 +1463,8 @@ if (response.ok) { this.showNotification(`Tags added to ${this.selectedPrompts.size} prompts`, "success"); this.hideModal("bulkTagModal"); + // Refresh both stats and results + await this.loadStatistics(); this.search(); } } catch (error) { @@ -1483,6 +1489,8 @@ if (response.ok) { this.showNotification(`Category set for ${this.selectedPrompts.size} prompts`, "success"); this.hideModal("bulkCategoryModal"); + // Refresh both stats and results + await this.loadStatistics(); this.search(); } } catch (error) {