File size: 16,204 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# 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/>.

"""
Element cache service for optimizing IFC query performance
"""
import json
import hashlib
import logging
from typing import Dict, List, Any, Optional, Tuple
from datetime import datetime, timedelta
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func, and_, or_, text

from app.models import Project, Element
from app.core.redis import RedisCache

logger = logging.getLogger(__name__)


class ElementCacheService:
    """Service for caching element query results to improve performance"""

    # Cache TTL settings
    COUNT_CACHE_TTL = 1800  # 30 minutes for count queries
    METADATA_CACHE_TTL = 3600  # 1 hour for metadata
    AGGREGATED_CACHE_TTL = 7200  # 2 hours for aggregated statistics

    @staticmethod
    def _generate_cache_key(operation: str, project_id: int, filters: dict = None) -> str:
        """Generate cache key for query results"""
        content = f"{operation}:project_{project_id}"
        if filters:
            # Sort filters for consistent hashing
            sorted_filters = json.dumps(filters, sort_keys=True)
            content += f":{sorted_filters}"

        return hashlib.sha256(content.encode()).hexdigest()

    @staticmethod
    async def get_element_count_cached(
        project_id: int,
        filters: Dict[str, Any] = None
    ) -> Optional[int]:
        """Get cached element count"""
        cache_key = f"element_count:{ElementCacheService._generate_cache_key('count', project_id, filters)}"

        try:
            cached_result = await RedisCache.get(cache_key)
            if cached_result is not None:
                logger.info(f"Cache hit for element count: project_{project_id}")
                return int(cached_result)
        except Exception as e:
            logger.warning(f"Error reading from cache: {e}")

        return None

    @staticmethod
    async def cache_element_count(
        project_id: int,
        count: int,
        filters: Dict[str, Any] = None
    ):
        """Cache element count result"""
        cache_key = f"element_count:{ElementCacheService._generate_cache_key('count', project_id, filters)}"

        try:
            await RedisCache.set(cache_key, count, ElementCacheService.COUNT_CACHE_TTL)
            logger.debug(f"Cached element count: project_{project_id}, count={count}")
        except Exception as e:
            logger.warning(f"Error writing to cache: {e}")

    @staticmethod
    async def get_project_element_stats_cached(project_id: int) -> Optional[Dict[str, Any]]:
        """Get cached project element statistics"""
        cache_key = f"project_stats:{project_id}"

        try:
            cached_result = await RedisCache.get(cache_key)
            if cached_result:
                logger.info(f"Cache hit for project stats: project_{project_id}")
                return cached_result
        except Exception as e:
            logger.warning(f"Error reading project stats from cache: {e}")

        return None

    @staticmethod
    async def cache_project_element_stats(project_id: int, stats: Dict[str, Any]):
        """Cache project element statistics"""
        cache_key = f"project_stats:{project_id}"

        try:
            await RedisCache.set(cache_key, stats, ElementCacheService.AGGREGATED_CACHE_TTL)
            logger.debug(f"Cached project stats: project_{project_id}")
        except Exception as e:
            logger.warning(f"Error caching project stats: {e}")

    @staticmethod
    async def get_or_compute_element_count(
        session: AsyncSession,
        project_id: int,
        filters: Dict[str, Any] = None,
        use_cache: bool = True
    ) -> int:
        """Get element count with caching - optimized implementation"""

        # Try cache first
        if use_cache:
            cached_count = await ElementCacheService.get_element_count_cached(project_id, filters)
            if cached_count is not None:
                return cached_count

        # Build optimized count query
        count_query = select(func.count(Element.id)).where(Element.project_id == project_id)

        # Apply filters efficiently
        if filters:
            # IFC type filter
            if "ifc_type" in filters:
                ifc_type = filters["ifc_type"]
                if isinstance(ifc_type, str):
                    count_query = count_query.where(Element.ifc_type == ifc_type)
                elif isinstance(ifc_type, dict) and "$in" in ifc_type:
                    count_query = count_query.where(Element.ifc_type.in_(ifc_type["$in"]))

            # Building story filter
            if "building_story" in filters:
                story = filters["building_story"]
                if isinstance(story, str):
                    count_query = count_query.where(Element.level_name == story)

            # Material filter
            if "material" in filters:
                material = filters["material"]
                if isinstance(material, str):
                    count_query = count_query.where(Element.material_name.ilike(f"%{material}%"))

            # Boolean filters
            if "is_load_bearing" in filters:
                count_query = count_query.where(Element.is_load_bearing == filters["is_load_bearing"])

            if "is_external" in filters:
                count_query = count_query.where(Element.is_external == filters["is_external"])

            # Geometry filter
            if "has_geometry" in filters:
                count_query = count_query.where(Element.has_geometry == filters["has_geometry"])

        # Execute optimized count query
        start_time = datetime.now()
        result = await session.execute(count_query)
        count = result.scalar() or 0

        query_time = (datetime.now() - start_time).total_seconds() * 1000
        logger.info(f"Element count query executed in {query_time:.2f}ms: project_{project_id}, count={count}")

        # Cache the result
        if use_cache:
            await ElementCacheService.cache_element_count(project_id, count, filters)

        return count

    @staticmethod
    async def get_or_compute_project_stats(
        session: AsyncSession,
        project_id: int,
        use_cache: bool = True
    ) -> Dict[str, Any]:
        """Get comprehensive project statistics with caching"""

        # Try cache first
        if use_cache:
            cached_stats = await ElementCacheService.get_project_element_stats_cached(project_id)
            if cached_stats:
                return cached_stats

        start_time = datetime.now()

        # Use optimized aggregation query
        stats_query = select(
            func.count(Element.id).label('total_elements'),
            func.count(func.distinct(Element.ifc_type)).label('unique_types'),
            func.count(func.distinct(Element.level_name)).label('unique_floors'),
            func.count(func.distinct(Element.material_name)).label('unique_materials'),
            func.sum(func.case([(Element.has_geometry == True, 1)], else_=0)).label('elements_with_geometry'),
            func.sum(func.case([(Element.is_external == True, 1)], else_=0)).label('external_elements'),
            func.sum(func.case([(Element.is_load_bearing == True, 1)], else_=0)).label('load_bearing_elements')
        ).where(Element.project_id == project_id)

        result = await session.execute(stats_query)
        stats_row = result.first()

        # Get element type breakdown
        type_breakdown_query = select(
            Element.ifc_type,
            func.count(Element.id).label('count')
        ).where(
            Element.project_id == project_id
        ).group_by(Element.ifc_type).order_by(func.count(Element.id).desc())

        type_result = await session.execute(type_breakdown_query)
        type_breakdown = {row.ifc_type: row.count for row in type_result}

        # Get floor breakdown
        floor_breakdown_query = select(
            Element.level_name,
            func.count(Element.id).label('count')
        ).where(
            and_(Element.project_id == project_id, Element.level_name.is_not(None))
        ).group_by(Element.level_name).order_by(func.count(Element.id).desc())

        floor_result = await session.execute(floor_breakdown_query)
        floor_breakdown = {row.level_name: row.count for row in floor_result}

        # Compile comprehensive statistics
        stats = {
            'total_elements': stats_row.total_elements or 0,
            'unique_types': stats_row.unique_types or 0,
            'unique_floors': stats_row.unique_floors or 0,
            'unique_materials': stats_row.unique_materials or 0,
            'elements_with_geometry': stats_row.elements_with_geometry or 0,
            'external_elements': stats_row.external_elements or 0,
            'load_bearing_elements': stats_row.load_bearing_elements or 0,
            'type_breakdown': type_breakdown,
            'floor_breakdown': floor_breakdown,
            'computed_at': datetime.now().isoformat(),
            'computation_time_ms': int((datetime.now() - start_time).total_seconds() * 1000)
        }

        query_time = (datetime.now() - start_time).total_seconds() * 1000
        logger.info(f"Project stats computed in {query_time:.2f}ms: project_{project_id}")

        # Cache the comprehensive stats
        if use_cache:
            await ElementCacheService.cache_project_element_stats(project_id, stats)

        return stats

    @staticmethod
    async def invalidate_project_cache(project_id: int):
        """Invalidate all cached data for a project"""
        try:
            # Invalidate project stats
            await RedisCache.delete(f"project_stats:{project_id}")

            # Invalidate element counts (we need to clear by pattern)
            # Note: This is a simplified approach. In production, you might want to track cache keys
            cache_pattern = f"element_count:*project_{project_id}*"
            logger.info(f"Invalidated cache for project {project_id}")

        except Exception as e:
            logger.warning(f"Error invalidating project cache: {e}")

    @staticmethod
    async def get_element_ids_cached(
        project_id: int,
        filters: Dict[str, Any] = None
    ) -> Optional[List[Tuple[int, str]]]:
        """Get cached element IDs and GUIDs"""
        cache_key = f"element_ids:{ElementCacheService._generate_cache_key('select', project_id, filters)}"

        try:
            cached_result = await RedisCache.get(cache_key)
            if cached_result is not None:
                logger.info(f"Cache hit for element IDs: project_{project_id}")
                return cached_result
        except Exception as e:
            logger.warning(f"Error reading element IDs from cache: {e}")

        return None

    @staticmethod
    async def cache_element_ids(
        project_id: int,
        element_ids: List[Tuple[int, str]],
        filters: Dict[str, Any] = None
    ):
        """Cache element IDs and GUIDs"""
        cache_key = f"element_ids:{ElementCacheService._generate_cache_key('select', project_id, filters)}"

        try:
            # Cache with shorter TTL for element lists
            await RedisCache.set(cache_key, element_ids, ElementCacheService.COUNT_CACHE_TTL)
            logger.debug(f"Cached element IDs: project_{project_id}, count={len(element_ids)}")
        except Exception as e:
            logger.warning(f"Error caching element IDs: {e}")

    @staticmethod
    async def get_or_compute_element_ids(
        session: AsyncSession,
        project_id: int,
        filters: Dict[str, Any] = None,
        use_cache: bool = True,
        limit: int = 1000
    ) -> List[Tuple[int, str]]:
        """Get element IDs with caching - optimized for SELECT operations"""

        # Try cache first
        if use_cache:
            cached_ids = await ElementCacheService.get_element_ids_cached(project_id, filters)
            if cached_ids is not None:
                return cached_ids[:limit]  # Apply limit to cached results

        # Build optimized query for IDs only
        query = select(Element.id, Element.ifc_global_id).where(Element.project_id == project_id)

        # Apply filters efficiently
        if filters:
            if "ifc_type" in filters:
                ifc_type = filters["ifc_type"]
                if isinstance(ifc_type, str):
                    query = query.where(Element.ifc_type == ifc_type)
                elif isinstance(ifc_type, dict) and "$in" in ifc_type:
                    query = query.where(Element.ifc_type.in_(ifc_type["$in"]))

            if "building_story" in filters:
                story = filters["building_story"]
                if isinstance(story, str):
                    query = query.where(Element.level_name == story)

            if "material" in filters:
                material = filters["material"]
                if isinstance(material, str):
                    query = query.where(Element.material_name.ilike(f"%{material}%"))

            if "is_external" in filters:
                query = query.where(Element.is_external == filters["is_external"])

            if "has_geometry" in filters:
                query = query.where(Element.has_geometry == filters["has_geometry"])

        # Apply limit and execute
        query = query.limit(limit)

        start_time = datetime.now()
        result = await session.execute(query)
        element_rows = result.all()

        # Convert to list of tuples
        element_ids = [(row.id, row.ifc_global_id) for row in element_rows if row.ifc_global_id]

        query_time = (datetime.now() - start_time).total_seconds() * 1000
        logger.info(f"Element IDs query executed in {query_time:.2f}ms: project_{project_id}, count={len(element_ids)}")

        # Cache the result (only if not too large)
        if use_cache and len(element_ids) <= 500:  # Cache smaller result sets
            await ElementCacheService.cache_element_ids(project_id, element_ids, filters)

        return element_ids

    @staticmethod
    async def warm_project_cache(session: AsyncSession, project_id: int):
        """Pre-warm cache with common queries for a project"""
        logger.info(f"Warming cache for project {project_id}")

        try:
            # Warm project stats
            await ElementCacheService.get_or_compute_project_stats(session, project_id)

            # Warm common element type counts
            common_types = ["IfcWindow", "IfcDoor", "IfcWall", "IfcBeam", "IfcColumn", "IfcSpace"]
            for ifc_type in common_types:
                await ElementCacheService.get_or_compute_element_count(
                    session, project_id, {"ifc_type": ifc_type}
                )
                # Also warm element IDs for visualization
                await ElementCacheService.get_or_compute_element_ids(
                    session, project_id, {"ifc_type": ifc_type}, limit=100
                )

            # Warm geometry counts and IDs
            await ElementCacheService.get_or_compute_element_count(
                session, project_id, {"has_geometry": True}
            )
            await ElementCacheService.get_or_compute_element_ids(
                session, project_id, {"has_geometry": True}, limit=100
            )

            logger.info(f"Cache warming completed for project {project_id}")

        except Exception as e:
            logger.error(f"Error warming cache for project {project_id}: {e}")


# Global instance
element_cache_service = ElementCacheService()