11import json
22import language_tags
3- import logging
43from datetime import datetime
54from http import HTTPMethod
65from typing import Dict , Any , Optional , List , Tuple
2827)
2928from sqlalchemy .orm import Session
3029from shared .database .database import with_db_session
30+ from shared .helpers .logger import get_logger
3131from shared .helpers .utils import create_http_task
3232
3333FEATURE_ENDPOINTS = [
@@ -51,6 +51,7 @@ def __init__(self, stable_id: str, feed_id: str):
5151 self .gbfs_versions : List [GBFSVersion ] = []
5252 self .gbfs_endpoints : Dict [str , List [GBFSEndpoint ]] = {}
5353 self .validation_reports : Dict [str , Dict [str , Any ]] = {}
54+ self .logger = get_logger (GBFSDataProcessor .__name__ , stable_id )
5455
5556 def process_gbfs_data (self , autodiscovery_url : str ) -> None :
5657 """Process the GBFS data from the autodiscovery URL."""
@@ -76,8 +77,10 @@ def record_autodiscovery_request(
7677 self , autodiscovery_url : str , db_session : Session
7778 ) -> None :
7879 """Record the request to the autodiscovery URL."""
79- logging .info (f"Accessing auto-discovery URL: { autodiscovery_url } " )
80- request_metadata = GBFSEndpoint .get_request_metadata (autodiscovery_url )
80+ self .logger .info ("Accessing auto-discovery URL: %s" , autodiscovery_url )
81+ request_metadata = GBFSEndpoint .get_request_metadata (
82+ autodiscovery_url , self .logger
83+ )
8184 gbfs_feed = (
8285 db_session .query (Gbfsfeed ).filter (Gbfsfeed .id == self .feed_id ).one_or_none ()
8386 )
@@ -96,28 +99,28 @@ def record_autodiscovery_request(
9699 if request_metadata .get ("response_size_bytes" ) is None :
97100 raise ValueError (f"Error fetching { autodiscovery_url } " )
98101
99- @staticmethod
100102 def extract_gbfs_endpoints (
103+ self ,
101104 gbfs_json_url : str ,
102105 ) -> Tuple [Optional [List [GBFSEndpoint ]], GBFSVersion ]:
103106 """
104107 Extract GBFS endpoints from the GBFS JSON URL.
105108 @:returns: GBFS endpoints and the version of the GBFS feed.
106109 """
107- logging . info (f "Fetching GBFS data from { gbfs_json_url } " )
110+ self . logger . info ("Fetching GBFS data from %s" , gbfs_json_url )
108111 gbfs_json = fetch_gbfs_data (gbfs_json_url )
109112 feeds_matches = parse ("$..feeds" ).find (gbfs_json )
110113 version_match = parse ("$..version" ).find (gbfs_json )
111114 if not version_match :
112- logging .warning (
115+ self . logger .warning (
113116 "No version found in the GBFS data. Defaulting to version 1.0."
114117 )
115118 gbfs_version = GBFSVersion ("1.0" , gbfs_json_url )
116119 else :
117120 gbfs_version = GBFSVersion (version_match [0 ].value , gbfs_json_url )
118121 if not feeds_matches :
119- logging .error (
120- f "No feeds found in the GBFS data for version { gbfs_version .version } ."
122+ self . logger .error (
123+ "No feeds found in the GBFS data for version %s." , gbfs_version .version
121124 )
122125 return None , gbfs_version
123126 endpoints = []
@@ -144,15 +147,15 @@ def extract_gbfs_endpoints(
144147 for endpoint in endpoints
145148 }.values ()
146149 )
147- logging . info (f "Found version { gbfs_version .version } ." )
148- logging .info (
149- f "Found endpoints { ', ' .join ([endpoint .name for endpoint in endpoints ])} ."
150+ self . logger . info ("Found version %s." , gbfs_version .version )
151+ self . logger .info (
152+ "Found endpoints %s." , ", " .join ([endpoint .name for endpoint in endpoints ])
150153 )
151154 return unique_endpoints , gbfs_version
152155
153156 def extract_gbfs_versions (self , gbfs_json_url : str ) -> Optional [List [GBFSVersion ]]:
154157 """Extract GBFS versions from the autodiscovery URL"""
155- all_endpoints , version = GBFSDataProcessor .extract_gbfs_endpoints (gbfs_json_url )
158+ all_endpoints , version = self .extract_gbfs_endpoints (gbfs_json_url )
156159 if not all_endpoints or not version :
157160 return None
158161 self .gbfs_endpoints [version .version ] = all_endpoints
@@ -163,17 +166,20 @@ def extract_gbfs_versions(self, gbfs_json_url: str) -> Optional[List[GBFSVersion
163166 )
164167
165168 if gbfs_versions_endpoint :
166- logging .info (f"Fetching GBFS versions from { gbfs_versions_endpoint .url } " )
169+ self .logger .info (
170+ "Fetching GBFS versions from %s" , gbfs_versions_endpoint .url
171+ )
167172 gbfs_versions_json = fetch_gbfs_data (gbfs_versions_endpoint .url )
168173 versions_matches = parse ("$..versions" ).find (gbfs_versions_json )
169174 if versions_matches :
170175 gbfs_versions = GBFSVersion .from_dict (versions_matches [0 ].value )
171- logging .info (
172- f"Found versions { ', ' .join ([version .version for version in gbfs_versions ])} "
176+ self .logger .info (
177+ "Found versions %s" ,
178+ ", " .join ([version .version for version in gbfs_versions ]),
173179 )
174180 return gbfs_versions
175181 else :
176- logging .warning (
182+ self . logger .warning (
177183 "No versions found in the GBFS versions data. Defaulting to the autodiscovery URL version."
178184 )
179185 return [
@@ -192,14 +198,14 @@ def get_latest_version(self) -> Optional[str]:
192198 default = None ,
193199 )
194200 if not max_version :
195- logging .error (
201+ self . logger .error (
196202 "No non-RC versions found. Trying to set the latest to a RC version."
197203 )
198204 max_version = max (
199205 self .gbfs_versions , key = lambda version : version .version , default = None
200206 )
201207 if not max_version :
202- logging .error ("No versions found." )
208+ self . logger .error ("No versions found." )
203209 return None
204210 return max_version .version
205211
@@ -213,7 +219,7 @@ def update_database_entities(self, db_session: Session) -> None:
213219 .one_or_none ()
214220 )
215221 if not gbfs_feed :
216- logging . error (f "GBFS feed with ID { self . feed_id } not found." )
222+ self . logger . error ("GBFS feed with ID %s not found." , self . feed_id )
217223 return
218224 gbfs_versions_orm = []
219225 latest_version = self .get_latest_version ()
@@ -329,7 +335,8 @@ def validate_gbfs_feed_versions(self) -> None:
329335 response .raise_for_status ()
330336 json_report_summary = response .json ()
331337 except RequestException as e :
332- logging .error (f"Validation request failed for { version .url } : { e } " )
338+ self .logger .error ("Validation request failed for %s" , version .url )
339+ self .logger .error (e )
333340 continue
334341
335342 report_summary_blob = bucket .blob (
@@ -362,7 +369,7 @@ def create_validation_report_entities(
362369 "validatorVersion" , None
363370 )
364371 if validator_version is None or validation_time is None :
365- logging .error ("Validation version or time not found." )
372+ self . logger .error ("Validation version or time not found." )
366373 return None
367374
368375 validation_report_id = (
@@ -400,13 +407,13 @@ def extract_endpoints_for_all_versions(self):
400407 if endpoints :
401408 self .gbfs_endpoints [version .version ] = endpoints
402409 else :
403- logging . error (f "No endpoints found for version { version . version } ." )
410+ self . logger . error ("No endpoints found for version %s." , version . version )
404411
405412 def trigger_location_extraction (self ):
406413 """Trigger the location extraction process."""
407414 latest_version = self .get_latest_version ()
408415 if not latest_version :
409- logging .error ("No latest version found." )
416+ self . logger .error ("No latest version found." )
410417 return
411418 endpoints = self .gbfs_endpoints .get (latest_version , [])
412419 # Find the station_information_url endpoint
@@ -428,7 +435,9 @@ def trigger_location_extraction(self):
428435 None ,
429436 )
430437 if not station_information_url and not vehicle_status_url :
431- logging .warning ("No station_information_url or vehicle_status_url found." )
438+ self .logger .warning (
439+ "No station_information_url or vehicle_status_url found."
440+ )
432441 return
433442 client = tasks_v2 .CloudTasksClient ()
434443 body = json .dumps (
0 commit comments