# Copyright (C) 2026 Mattia Zatelli # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . """ Admin endpoints for database optimization and cache management """ import logging from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import text from app.core.database import get_async_session from app.services.element_cache_service import element_cache_service from app.core.openai_pool import openai_warm_pool logger = logging.getLogger(__name__) router = APIRouter() @router.post("/create-indexes") async def create_performance_indexes( session: AsyncSession = Depends(get_async_session) ): """Create database indexes for better query performance""" # Performance indexes for common query patterns indexes = [ # Basic filtering indexes "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_project_ifc_type ON elements(project_id, ifc_type);", "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_project_level ON elements(project_id, level_name);", "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_project_material ON elements(project_id, material_name);", "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_project_geometry ON elements(project_id, has_geometry);", # Boolean property indexes "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_project_external ON elements(project_id, is_external);", "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_project_load_bearing ON elements(project_id, is_load_bearing);", # Composite indexes for common combinations "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_type_level ON elements(project_id, ifc_type, level_name);", "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_type_external ON elements(project_id, ifc_type, is_external);", "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_type_geometry ON elements(project_id, ifc_type, has_geometry);", # Global ID index for fast lookups "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_global_id ON elements(ifc_global_id);", # Composite GIN indexes for JSON fields "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_centroid_gin ON elements USING gin(centroid);", "CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_elements_bounding_box_gin ON elements USING gin(bounding_box);", ] created_indexes = [] failed_indexes = [] try: logger.info("Creating performance indexes for BIM queries...") for index_sql in indexes: try: index_name = index_sql.split("idx_")[1].split(" ")[0] if "idx_" in index_sql else "unknown" logger.info(f"Creating index: idx_{index_name}") await session.execute(text(index_sql)) await session.commit() created_indexes.append(f"idx_{index_name}") logger.info(f"✅ Index created successfully: idx_{index_name}") except Exception as e: error_msg = str(e).lower() if "already exists" in error_msg or "relation" in error_msg and "already exists" in error_msg: logger.info(f"â„šī¸ Index already exists: idx_{index_name}") created_indexes.append(f"idx_{index_name} (already existed)") else: logger.error(f"❌ Error creating index idx_{index_name}: {e}") failed_indexes.append(f"idx_{index_name}: {str(e)}") # Update table statistics (whitelist only known tables) ALLOWED_TABLES = {"elements", "projects", "queries"} for table in ALLOWED_TABLES: try: await session.execute(text(f"ANALYZE {table};")) logger.info(f"Analyzed table: {table}") except Exception as e: logger.warning(f"Could not analyze table {table}: {e}") await session.commit() logger.info("🚀 Database optimization completed!") return { "success": True, "message": "Database indexes created and tables analyzed", "created_indexes": created_indexes, "failed_indexes": failed_indexes, "total_attempted": len(indexes) } except Exception as e: logger.error(f"Error during index creation: {e}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to create indexes" ) @router.post("/warm-cache/{project_id}") async def warm_project_cache( project_id: str, session: AsyncSession = Depends(get_async_session) ): """Warm cache for a specific project""" try: # Convert project ID to internal format internal_id = int(project_id.replace('prj_', '')) logger.info(f"Warming cache for project {project_id}") await element_cache_service.warm_project_cache(session, internal_id) return { "success": True, "message": f"Cache warmed successfully for project {project_id}", "project_id": project_id } except ValueError: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid project ID format" ) except Exception as e: logger.error(f"Error warming cache for project {project_id}: {e}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to warm cache" ) @router.post("/invalidate-cache/{project_id}") async def invalidate_project_cache(project_id: str): """Invalidate cache for a specific project""" try: # Convert project ID to internal format internal_id = int(project_id.replace('prj_', '')) await element_cache_service.invalidate_project_cache(internal_id) return { "success": True, "message": f"Cache invalidated for project {project_id}", "project_id": project_id } except ValueError: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid project ID format" ) except Exception as e: logger.error(f"Error invalidating cache for project {project_id}: {e}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to invalidate cache" ) @router.post("/trigger-processing/{project_id}") async def trigger_project_processing( project_id: str, session: AsyncSession = Depends(get_async_session) ): """Trigger IFC processing for a project (admin endpoint)""" try: # Convert project ID to internal format internal_id = int(project_id.replace('prj_', '')) # Get project from database from app.models import Project, ProjectStatus from sqlalchemy import select query = select(Project).where(Project.id == internal_id) result = await session.execute(query) project = result.scalar_one_or_none() if not project: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"Project {project_id} not found" ) # Trigger processing using the task try: from app.tasks.ifc_processing import process_ifc_file project_data = { 'project_id': project_id, 'tenant_id': project.tenant_id, 's3_bucket': project.s3_bucket, 's3_key': project.s3_key, 'filename': project.original_filename } # Try to queue the task task = process_ifc_file.delay(project_data) return { "success": True, "message": f"Processing triggered for project {project_id}", "project_id": project_id, "task_id": task.id, "current_status": project.status.value } except Exception as task_error: logger.error(f"Error queuing task: {task_error}") # If Celery is not available, process directly (synchronous fallback) logger.warning("Celery unavailable, processing synchronously...") # Process directly using the async function try: from app.tasks.ifc_processing import _process_ifc_async # Mark as processing project.mark_processing("manual_processing") await session.commit() # Process the IFC file await _process_ifc_async(project_data) return { "success": True, "message": f"Project {project_id} processed successfully (direct mode)", "project_id": project_id, "processing_mode": "direct" } except Exception as proc_error: # Mark as failed project.mark_failed(str(proc_error)) await session.commit() raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Processing failed: {str(proc_error)}" ) except ValueError: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid project ID format" ) except Exception as e: logger.error(f"Error triggering processing for project {project_id}: {e}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to trigger processing" ) @router.get("/openai-pool-status") async def get_openai_pool_status(): """Get OpenAI warm pool status for monitoring""" try: pool_status = openai_warm_pool.get_pool_status() return { "success": True, "message": "OpenAI warm pool status retrieved", "pool_status": pool_status, "performance_info": { "target_latency_reduction": "50-80%", "cold_start_elimination": pool_status["healthy_clients"] > 0, "recommended_pool_size": "1-2 clients" } } except Exception as e: logger.error(f"Error getting OpenAI pool status: {e}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to get pool status" )