# 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 . """ 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()