moving geocoding to a background task
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 57s

This commit is contained in:
Tudor
2026-01-08 15:30:33 +00:00
parent 73971a43f0
commit 40348cb1bd
4 changed files with 216 additions and 70 deletions

View File

@@ -21,10 +21,9 @@ from starlette.middleware.base import BaseHTTPMiddleware
from .config import settings
from .data_loader import (
clear_cache,
geocode_postcodes_bulk,
geocode_single_postcode,
haversine_distance,
load_school_data,
geocode_single_postcode,
)
from .data_loader import get_data_info as get_db_info
from .database import init_db
@@ -256,7 +255,7 @@ async def get_schools(
]
schools_df = df_latest[available_cols].drop_duplicates(subset=["urn"])
# Location-based search
# Location-based search (uses pre-geocoded data from database)
search_coords = None
if postcode:
coords = geocode_single_postcode(postcode)
@@ -264,24 +263,7 @@ async def get_schools(
search_coords = coords
schools_df = schools_df.copy()
# Geocode school postcodes on-demand if not already cached
if "postcode" in schools_df.columns:
unique_postcodes = schools_df["postcode"].dropna().unique().tolist()
geocoded = geocode_postcodes_bulk(unique_postcodes)
# Add lat/long from geocoded data
schools_df["latitude"] = schools_df["postcode"].apply(
lambda pc: geocoded.get(str(pc).strip().upper(), (None, None))[0]
if pd.notna(pc)
else None
)
schools_df["longitude"] = schools_df["postcode"].apply(
lambda pc: geocoded.get(str(pc).strip().upper(), (None, None))[1]
if pd.notna(pc)
else None
)
# Filter by distance
# Filter by distance using pre-geocoded lat/long from database
def calc_distance(row):
if pd.isna(row.get("latitude")) or pd.isna(row.get("longitude")):
return float("inf")