File size: 11,257 Bytes
e6be672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# 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 <https://www.gnu.org/licenses/>.

"""
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"
        )