{"record":{"id":"de90ddda7bd28db2","repo":"odysseus-dev/odysseus","slug":"failed-to-retrieve-database-statistics","errorCode":null,"errorMessage":"Failed to retrieve database statistics","messagePattern":"Failed to retrieve database statistics","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"routes/diagnostics_routes.py","lineNumber":64,"sourceCode":"            tail_lines = [line.rstrip('\\r\\n') for line in tail_lines]\n\n            return {\n                \"status\": \"success\",\n                \"logs\": tail_lines\n            }\n        except Exception as e:\n            logger.error(f\"Diagnostics logs retrieval error: {e}\")\n            raise HTTPException(500, f\"Failed to retrieve logs: {str(e)}\")\n\n    @router.get(\"/api/db/stats\")\n    async def get_database_stats(request: Request) -> Dict[str, Any]:\n        require_admin(request)\n        try:\n            from core.database import get_detailed_stats\n            return get_detailed_stats()\n        except Exception as e:\n            logger.error(f\"DB stats error: {e}\")\n            raise HTTPException(500, \"Failed to retrieve database statistics\")\n\n    @router.get(\"/api/rag/stats\")\n    async def get_rag_stats(request: Request) -> Dict[str, Any]:\n        require_admin(request)\n        if rag_available and rag_manager:\n            return rag_manager.get_stats()\n        return {\"error\": \"RAG system not available\"}\n\n    @router.get(\"/api/test/youtube\")\n    async def test_youtube(request: Request, url: str) -> Dict[str, Any]:\n        require_admin(request)\n        try:\n            video_id = extract_youtube_id(url)\n            if not video_id:\n                return {\"error\": \"Invalid YouTube URL\"}\n\n            data = await extract_transcript_async(url, video_id)\n            return {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/odysseus-dev/odysseus/blob/f9235ebbf13f693a6fd29ce70b097f6ec83705bf/routes/diagnostics_routes.py#L46-L82","documentation":"Raised (HTTP 500) by GET /api/db/stats (admin-gated) when importing core.database.get_detailed_stats or executing it throws. Unlike the logs endpoint, the raw exception text is NOT returned — only the generic message — but the cause is logged as 'DB stats error'. Typical causes: SQLite database file missing/locked/corrupted, schema mismatch after an upgrade, or an import failure inside core.database.","triggerScenarios":"GET /api/db/stats while the database file is locked by another writer (SQLite BUSY), the DB was deleted or moved after startup, a migration left the schema without a table/column get_detailed_stats queries, or core.database import fails (missing optional dependency).","commonSituations":"Two server instances pointed at the same SQLite file; DB directory on a network mount with flaky locking; app upgraded to a version whose get_detailed_stats expects a column the old DB lacks; database file permissions changed.","solutions":["Read the server log for the 'DB stats error: ...' line to get the real exception.","If it is 'database is locked', stop competing processes (second server instance, manual sqlite3 session) or enable WAL mode.","If it is 'no such table/column', run the app's DB init/migration path (init_db) so the schema is brought up to date.","If the file was moved/deleted, restore it or let the app recreate it, then restart."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"# Pre-flight: DB reachable and schema present before the dashboard calls stats\nfrom core.database import SessionLocal\nwith SessionLocal() as db:\n    db.execute(text('SELECT 1'))","typeGuard":null,"tryCatchPattern":"try { const stats = await get('/api/db/stats'); } catch (e) { if (e.status === 500 && /database statistics/.test(e.message)) { showBanner('DB stats unavailable — check server log for DB stats error'); return null; } throw e; }","preventionTips":["Run only one writer process against the SQLite file, or enable WAL.","Run init_db migrations after every app upgrade before serving requests."],"tags":["diagnostics","database","sqlite","migration","admin"],"backgroundTag":null,"analyzedSha":"f9235ebbf13f693a6fd29ce70b097f6ec83705bf","analyzedAt":"2026-08-14T21:47:48.359Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}