Full-UK dump: return every station with all prices; radius/limit optional (alert path only)

This commit is contained in:
FuelBoard Contributor
2026-08-11 19:27:34 +01:00
parent 3b209b9557
commit 142061be96
+12 -9
View File
@@ -256,8 +256,8 @@ def stations(
fuel: str = Query("e10", description="Fuel grade: e10, e5, diesel"), fuel: str = Query("e10", description="Fuel grade: e10, e5, diesel"),
lat: Optional[float] = Query(None, description="User latitude"), lat: Optional[float] = Query(None, description="User latitude"),
lng: Optional[float] = Query(None, description="User longitude"), lng: Optional[float] = Query(None, description="User longitude"),
radius: float = Query(20.0, description="Search radius in km"), radius: Optional[float] = Query(None, description="Search radius in km (optional; omit for the full dataset)"),
limit: int = Query(10, ge=1, le=1000), limit: int = Query(10000, ge=1, le=10000),
): ):
if _state["stations"] is None: if _state["stations"] is None:
raise HTTPException(status_code=502, detail="No data cached yet and upstream fetch failed") raise HTTPException(status_code=502, detail="No data cached yet and upstream fetch failed")
@@ -268,18 +268,18 @@ def stations(
rows = [] rows = []
for station in _stations_list(): for station in _stations_list():
price = _fuel_price(station, fuel)
if price is None:
continue
s_lat = station.get("latitude", station.get("lat")) s_lat = station.get("latitude", station.get("lat"))
s_lng = station.get("longitude", station.get("lng")) s_lng = station.get("longitude", station.get("lng"))
# All known fuel grades for this station (E5/E10/DIESEL) so clients can # All known fuel grades for this station (E5/E10/DIESEL) so clients can
# switch fuel tabs without another request. # switch fuel tabs without another request. Every station is returned
# regardless of the requested fuel — clients filter on-device.
prices = {} prices = {}
for grade in ("E5", "E10", "DIESEL"): for grade in ("E5", "E10", "DIESEL"):
p = _fuel_price(station, grade) p = _fuel_price(station, grade)
if p is not None: if p is not None:
prices[grade] = p prices[grade] = p
if not prices:
continue # station sells nothing we track — skip
item = { item = {
"id": station.get("id") or station.get("station_id"), "id": station.get("id") or station.get("station_id"),
"name": station.get("name") or station.get("station_name"), "name": station.get("name") or station.get("station_name"),
@@ -288,7 +288,7 @@ def stations(
"postcode": station.get("postcode") or "", "postcode": station.get("postcode") or "",
"lat": s_lat, "lat": s_lat,
"lng": s_lng, "lng": s_lng,
"price": price, "price": prices.get(fuel.upper()),
"prices": prices, "prices": prices,
"price_updated": station.get("price_updated") or station.get("updated_at"), "price_updated": station.get("price_updated") or station.get("updated_at"),
} }
@@ -297,10 +297,13 @@ def stations(
rows.append(item) rows.append(item)
if lat is not None and lng is not None: if lat is not None and lng is not None:
# Optional server-side radius filter — used by the alert path for a
# focused fresh fetch. The main app fetch omits radius and gets all.
if radius is not None:
rows = [r for r in rows if r.get("distance_km") is not None and r["distance_km"] <= radius] rows = [r for r in rows if r.get("distance_km") is not None and r["distance_km"] <= radius]
rows.sort(key=lambda r: r["distance_km"]) rows.sort(key=lambda r: r.get("distance_km") if r.get("distance_km") is not None else float("inf"))
else: else:
rows.sort(key=lambda r: r["price"]) rows.sort(key=lambda r: r["price"] if r["price"] is not None else float("inf"))
return {"fuel": fuel, "count": len(rows[:limit]), "stations": rows[:limit]} return {"fuel": fuel, "count": len(rows[:limit]), "stations": rows[:limit]}