Full-UK dump: return every station with all prices; radius/limit optional (alert path only)
This commit is contained in:
+13
-10
@@ -256,8 +256,8 @@ def stations(
|
||||
fuel: str = Query("e10", description="Fuel grade: e10, e5, diesel"),
|
||||
lat: Optional[float] = Query(None, description="User latitude"),
|
||||
lng: Optional[float] = Query(None, description="User longitude"),
|
||||
radius: float = Query(20.0, description="Search radius in km"),
|
||||
limit: int = Query(10, ge=1, le=1000),
|
||||
radius: Optional[float] = Query(None, description="Search radius in km (optional; omit for the full dataset)"),
|
||||
limit: int = Query(10000, ge=1, le=10000),
|
||||
):
|
||||
if _state["stations"] is None:
|
||||
raise HTTPException(status_code=502, detail="No data cached yet and upstream fetch failed")
|
||||
@@ -268,18 +268,18 @@ def stations(
|
||||
|
||||
rows = []
|
||||
for station in _stations_list():
|
||||
price = _fuel_price(station, fuel)
|
||||
if price is None:
|
||||
continue
|
||||
s_lat = station.get("latitude", station.get("lat"))
|
||||
s_lng = station.get("longitude", station.get("lng"))
|
||||
# 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 = {}
|
||||
for grade in ("E5", "E10", "DIESEL"):
|
||||
p = _fuel_price(station, grade)
|
||||
if p is not None:
|
||||
prices[grade] = p
|
||||
if not prices:
|
||||
continue # station sells nothing we track — skip
|
||||
item = {
|
||||
"id": station.get("id") or station.get("station_id"),
|
||||
"name": station.get("name") or station.get("station_name"),
|
||||
@@ -288,7 +288,7 @@ def stations(
|
||||
"postcode": station.get("postcode") or "",
|
||||
"lat": s_lat,
|
||||
"lng": s_lng,
|
||||
"price": price,
|
||||
"price": prices.get(fuel.upper()),
|
||||
"prices": prices,
|
||||
"price_updated": station.get("price_updated") or station.get("updated_at"),
|
||||
}
|
||||
@@ -297,10 +297,13 @@ def stations(
|
||||
rows.append(item)
|
||||
|
||||
if lat is not None and lng is not None:
|
||||
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"])
|
||||
# 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.sort(key=lambda r: r.get("distance_km") if r.get("distance_km") is not None else float("inf"))
|
||||
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]}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user