Fix geocoding: validate API responses, clean poisoned cache, improve logging

- Cache lookup now requires non-NULL lat/lon — previously a row with
  NULL coordinates counted as a cache hit, returning {lat: None, lon: None}
  which the frontend silently ignored (tower in list but no map pin)
- API response handler validates lat/lon exist before caching, preventing
  error responses (status 200 with error body) from poisoning the cache
- On geocoding worker start, delete any existing poisoned cache rows
- Geocoding worker now logs "API key not configured" vs "rate limit
  reached" so the actual problem is visible in logs
- API error responses now log the response body for easier debugging
This commit is contained in:
Smittix
2026-02-08 19:55:00 +00:00
parent b8780db849
commit b6a43ec1b9
2 changed files with 38 additions and 10 deletions
+19 -3
View File
@@ -104,6 +104,19 @@ def can_use_api():
def start_geocoding_worker():
"""Start background thread for async geocoding."""
global _geocoding_worker_thread
# Clean poisoned cache entries (rows with NULL lat/lon from failed API responses)
try:
with get_db() as conn:
deleted = conn.execute(
'DELETE FROM gsm_cells WHERE lat IS NULL OR lon IS NULL'
).rowcount
conn.commit()
if deleted:
logger.info(f"Cleaned {deleted} poisoned cache entries (NULL coordinates)")
except Exception as e:
logger.warning(f"Could not clean cache: {e}")
if _geocoding_worker_thread is None or not _geocoding_worker_thread.is_alive():
_geocoding_worker_thread = threading.Thread(
target=geocoding_worker,
@@ -125,10 +138,13 @@ def geocoding_worker():
# Wait for pending tower with timeout
tower_data = geocoding_queue.get(timeout=5)
# Check rate limit
# Check API key and rate limit
if not can_use_api():
current_usage = get_api_usage_today()
logger.warning(f"OpenCellID API rate limit reached ({current_usage}/{config.GSM_API_DAILY_LIMIT})")
if not config.GSM_OPENCELLID_API_KEY:
logger.warning("OpenCellID API key not configured (set INTERCEPT_GSM_OPENCELLID_API_KEY)")
else:
current_usage = get_api_usage_today()
logger.warning(f"OpenCellID API daily limit reached ({current_usage}/{config.GSM_API_DAILY_LIMIT})")
geocoding_queue.task_done()
continue