better logs
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				Run testing on the backend code / Build (pull_request) Has been cancelled
				
			
		
			
				
	
				Run linting on the backend code / Build (pull_request) Has been cancelled
				
			
		
			
				
	
				Build and deploy the backend to staging / Deploy to staging (pull_request) Has been cancelled
				
			
		
			
				
	
				Build and deploy the backend to staging / Build and push image (pull_request) Has been cancelled
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	Run testing on the backend code / Build (pull_request) Has been cancelled
				
			Run linting on the backend code / Build (pull_request) Has been cancelled
				
			Build and deploy the backend to staging / Deploy to staging (pull_request) Has been cancelled
				
			Build and deploy the backend to staging / Build and push image (pull_request) Has been cancelled
				
			This commit is contained in:
		| @@ -14,7 +14,7 @@ from .utils.landmarks_manager import LandmarkManager | ||||
| from .utils.toilets_manager import ToiletsManager | ||||
| from .optimization.optimizer import Optimizer | ||||
| from .optimization.refiner import Refiner | ||||
| from .overpass.caching_strategy import fill_cache | ||||
| from .overpass.overpass import fill_cache | ||||
| from .cache import client as cache_client | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -3,7 +3,6 @@ import json | ||||
| import hashlib | ||||
|  | ||||
| from ..constants import OSM_CACHE_DIR, OSM_TYPES | ||||
| from .overpass import Overpass | ||||
|  | ||||
|  | ||||
| def get_cache_key(query: str) -> str: | ||||
| @@ -13,6 +12,7 @@ def get_cache_key(query: str) -> str: | ||||
|     """ | ||||
|     return hashlib.md5(query.encode('utf-8')).hexdigest() | ||||
|  | ||||
|  | ||||
| class CachingStrategyBase: | ||||
|     """ | ||||
|     Base class for implementing caching strategies. | ||||
| @@ -25,15 +25,10 @@ class CachingStrategyBase: | ||||
|         """Store data in the cache with the specified key.""" | ||||
|         raise NotImplementedError('Subclass should implement set') | ||||
|  | ||||
|     def set_hollow(self, key, cell: tuple, osm_types: OSM_TYPES, | ||||
|                     selector: str, conditions=[], out='center'): | ||||
|     def set_hollow(self, key, **kwargs): | ||||
|         """Create a hollow (empty) cache entry with a specific key.""" | ||||
|         raise NotImplementedError('Subclass should implement set_hollow') | ||||
|  | ||||
|     def fill_hollow(self, key, value): | ||||
|         """Fill in the cache for an existing hollow entry.""" | ||||
|         raise NotImplementedError('Subclass should implement fill_hollow') | ||||
|  | ||||
|     def close(self): | ||||
|         """Clean up or close any resources used by the caching strategy.""" | ||||
|  | ||||
| @@ -134,27 +129,3 @@ class CachingStrategy: | ||||
|                     selector: str, conditions: list=None, out='center'): | ||||
|         """Create a hollow cache entry.""" | ||||
|         cls.__strategy.set_hollow(key, cell, osm_types, selector, conditions, out) | ||||
|  | ||||
|  | ||||
| def fill_cache(): | ||||
|     """ | ||||
|     Scans the specified cache directory for files starting with 'hollow_' and attempts to load | ||||
|     their contents as JSON to fill the cache of the Overpass system. | ||||
|     """ | ||||
|     overpass = Overpass() | ||||
|  | ||||
|     with os.scandir(OSM_CACHE_DIR) as it: | ||||
|         for entry in it: | ||||
|             if entry.is_file() and entry.name.startswith('hollow_'): | ||||
|  | ||||
|                 try : | ||||
|                     # Read the whole file content as a string | ||||
|                     with open(entry.path, 'r') as f: | ||||
|                         # load data and fill the cache with the query and key | ||||
|                         json_data = json.load(f) | ||||
|                         overpass.fill_cache(json_data) | ||||
|                     # Now delete the file as the cache is filled | ||||
|                     os.remove(entry.path) | ||||
|  | ||||
|                 except Exception as exc : | ||||
|                     overpass.logger.error(f'An error occured while parsing file {f} as .json file') | ||||
|   | ||||
| @@ -52,10 +52,11 @@ class Overpass : | ||||
|  | ||||
|         # If there is no missing data, return the cached responses | ||||
|         if not hollow_cache_keys : | ||||
|             self.logger.info('Cache hit.') | ||||
|             self.logger.info(f'Cache hit for {len(cached_responses)} quadrants.') | ||||
|             return self._combine_cached_data(cached_responses) | ||||
|  | ||||
|         # TODO If there is SOME missing data : hybrid stuff with partial cache | ||||
|         self.logger.info(f'Cache miss for {len(hollow_cache_keys)} quadrants.') | ||||
|  | ||||
|         # Missing data: Make a query to Overpass API | ||||
|         query_str = Overpass.build_query(bbox, osm_types, selector, conditions, out) | ||||
| @@ -80,9 +81,7 @@ class Overpass : | ||||
|                 response_data = response.read().decode('utf-8')  # Convert the HTTPResponse to a string | ||||
|                 data = json.loads(response_data)  # Load the JSON from the string | ||||
|                 elements = data.get('elements', []) | ||||
|  | ||||
|                 self.logger.info(f'Cache miss. Fetching data through Overpass.') | ||||
|                 self.logger.debug(f'Query = {query_str}') | ||||
|                 # self.logger.debug(f'Query = {query_str}') | ||||
|                 return elements | ||||
|  | ||||
|         except urllib.error.URLError as e: | ||||
| @@ -321,3 +320,27 @@ def get_base_info(elem: dict, osm_type: OSM_TYPES, with_name=False) : | ||||
|         return osm_id, coords, name | ||||
|     else : | ||||
|         return osm_id, coords | ||||
|  | ||||
|  | ||||
| def fill_cache(): | ||||
|     """ | ||||
|     Scans the specified cache directory for files starting with 'hollow_' and attempts to load | ||||
|     their contents as JSON to fill the cache of the Overpass system. | ||||
|     """ | ||||
|     overpass = Overpass() | ||||
|  | ||||
|     with os.scandir(OSM_CACHE_DIR) as it: | ||||
|         for entry in it: | ||||
|             if entry.is_file() and entry.name.startswith('hollow_'): | ||||
|  | ||||
|                 try : | ||||
|                     # Read the whole file content as a string | ||||
|                     with open(entry.path, 'r') as f: | ||||
|                         # load data and fill the cache with the query and key | ||||
|                         json_data = json.load(f) | ||||
|                         overpass.fill_cache(json_data) | ||||
|                     # Now delete the file as the cache is filled | ||||
|                     os.remove(entry.path) | ||||
|  | ||||
|                 except Exception as exc : | ||||
|                     overpass.logger.error(f'An error occured while parsing file {entry.path} as .json file') | ||||
|   | ||||
		Reference in New Issue
	
	Block a user