Add POI API routes and Celery task
FastAPI router with CRUD endpoints for POIs, distance calculation trigger, and distance queries. Streaming GeoJSON endpoint now accepts include_poi_distances=true to inject travel times into features. Celery task wraps the distance calculator with progress reporting.
This commit is contained in:
parent
da0a56895d
commit
bd788df9aa
4 changed files with 332 additions and 3 deletions
41
api/app.py
41
api/app.py
|
|
@ -7,6 +7,7 @@ from typing import Annotated, AsyncGenerator, Optional
|
|||
from api.auth import get_current_user
|
||||
from api.config import DEV_TIER_ORIGINS, PROD_TIER_ORIGINS
|
||||
from api.passkey_routes import passkey_router
|
||||
from api.poi_routes import poi_router
|
||||
from api.rate_limit_config import RateLimitConfig
|
||||
from api.rate_limiter import RateLimitMiddleware
|
||||
from api.audit_middleware import AuditLogMiddleware
|
||||
|
|
@ -28,6 +29,8 @@ from services.listing_cache import (
|
|||
get_cached_features,
|
||||
cache_features_batch,
|
||||
)
|
||||
from repositories.poi_repository import POIRepository
|
||||
from repositories.user_repository import UserRepository
|
||||
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
||||
from api.metrics import metrics_app
|
||||
from opentelemetry.metrics import get_meter
|
||||
|
|
@ -71,6 +74,7 @@ def get_query_parameters(
|
|||
|
||||
app = FastAPI()
|
||||
app.include_router(passkey_router)
|
||||
app.include_router(poi_router)
|
||||
app.mount("/metrics", metrics_app)
|
||||
meter = get_meter(__name__)
|
||||
request_counter = meter.create_counter(
|
||||
|
|
@ -173,6 +177,7 @@ async def _stream_from_db(
|
|||
query_parameters: QueryParameters,
|
||||
batch_size: int,
|
||||
limit: int | None,
|
||||
poi_distances_lookup: dict[int, list[dict[str, str | int]]] | None = None,
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""Stream GeoJSON features from the database, populating the cache as we go."""
|
||||
repository = ListingRepository(engine)
|
||||
|
|
@ -193,6 +198,9 @@ async def _stream_from_db(
|
|||
query_parameters, limit=limit, page_size=batch_size
|
||||
):
|
||||
feature = convert_row_to_geojson(row, query_parameters.listing_type.value)
|
||||
# Inject POI distances if available
|
||||
if poi_distances_lookup and row['id'] in poi_distances_lookup:
|
||||
feature['properties']['poi_distances'] = poi_distances_lookup[row['id']]
|
||||
batch.append(feature)
|
||||
count += 1
|
||||
|
||||
|
|
@ -214,6 +222,7 @@ async def stream_listing_geojson(
|
|||
query_parameters: Annotated[QueryParameters, Depends(get_query_parameters)],
|
||||
batch_size: int = DEFAULT_BATCH_SIZE,
|
||||
limit: int | None = None,
|
||||
include_poi_distances: bool = False,
|
||||
) -> StreamingResponse:
|
||||
"""Stream listings as NDJSON for progressive map loading.
|
||||
|
||||
|
|
@ -228,11 +237,39 @@ async def stream_listing_geojson(
|
|||
else:
|
||||
limit = _rate_limit_config.geojson_stream_limit_cap
|
||||
|
||||
# Build POI distances lookup if requested
|
||||
poi_distances_lookup: dict[int, list[dict[str, str | int]]] | None = None
|
||||
if include_poi_distances:
|
||||
user_repo = UserRepository(engine)
|
||||
db_user = user_repo.get_user_by_email(user.email)
|
||||
if db_user and db_user.id is not None:
|
||||
poi_repo = POIRepository(engine)
|
||||
pois = {p.id: p for p in poi_repo.get_pois_for_user(db_user.id)}
|
||||
if pois:
|
||||
# Get all listing IDs first for the query
|
||||
listing_repo = ListingRepository(engine)
|
||||
all_ids = list(listing_repo.get_listing_ids(query_parameters.listing_type))
|
||||
if all_ids:
|
||||
distances = poi_repo.get_distances_for_listings(
|
||||
all_ids, query_parameters.listing_type, db_user.id
|
||||
)
|
||||
poi_distances_lookup = {}
|
||||
for d in distances:
|
||||
poi_name = pois[d.poi_id].name if d.poi_id in pois else "Unknown"
|
||||
entry = {
|
||||
"poi_id": d.poi_id,
|
||||
"poi_name": poi_name,
|
||||
"travel_mode": d.travel_mode,
|
||||
"duration_seconds": d.duration_seconds,
|
||||
"distance_meters": d.distance_meters,
|
||||
}
|
||||
poi_distances_lookup.setdefault(d.listing_id, []).append(entry)
|
||||
|
||||
cached_count = get_cached_count(query_parameters)
|
||||
if cached_count is not None and cached_count > 0:
|
||||
if cached_count is not None and cached_count > 0 and not include_poi_distances:
|
||||
generator = _stream_from_cache(query_parameters, batch_size, limit)
|
||||
else:
|
||||
generator = _stream_from_db(query_parameters, batch_size, limit)
|
||||
generator = _stream_from_db(query_parameters, batch_size, limit, poi_distances_lookup)
|
||||
|
||||
return StreamingResponse(
|
||||
generator,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue