Backend: - DB: 3 neue Indizes (forum_posts thread+user, routes user) — Forum/Routen-Queries - Caching: cache.py (TTL-Cache ohne neue Dependency) für 5 statische Listen (training_exercises, pflege_tipps, wiki_stats, wiki_gruppen, help_articles) - diary.py + breeder_photos.py: Bildverarbeitung (ffmpeg/PIL/EXIF) per run_in_executor → blockiert Event-Loop nicht mehr - scheduler.py: 11 kollidierende Jobs auf 5-Min-Intervalle gestaggert, coalesce=True - social.py: ORDER BY RANDOM() ohne LIMIT in 2 Stellen gefixt - alerts.py: Haversine-Loop bekommt SQL-Bounding-Box-Vorfilter Frontend: - sw.js: Tile-Cache mit LRU-Eviction (max 500 Einträge) - admin.js: Event-Listener-Leak — Tab-Klicks per Delegation statt N Listener - api.js: compressImage() Helper — Client-seitiges Resize auf max 2000px (HEIC/Videos/<500KB unverändert), integriert in 8 Upload-Stellen (diary, dog-profile×2, walks, poison, lost, health×2) Bump APP_VER 1071 → 1072 (sw.js, app.js, main.py, index.html)
66 lines
2.6 KiB
Python
66 lines
2.6 KiB
Python
"""BAN YARO — Nearby Alerts (Giftköder + Vermisste Hunde)"""
|
|
import math
|
|
from datetime import datetime
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from database import db
|
|
from auth import get_current_user_optional as get_optional_user
|
|
|
|
router = APIRouter()
|
|
|
|
_RADIUS_M = 20_000 # 20 km
|
|
_RADIUS_KM = _RADIUS_M / 1000.0
|
|
|
|
|
|
def _haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
|
|
R = 6_371_000
|
|
p1, p2 = math.radians(lat1), math.radians(lat2)
|
|
dp = math.radians(lat2 - lat1)
|
|
dl = math.radians(lon2 - lon1)
|
|
a = math.sin(dp / 2) ** 2 + math.cos(p1) * math.cos(p2) * math.sin(dl / 2) ** 2
|
|
return 2 * R * math.asin(math.sqrt(a))
|
|
|
|
|
|
def _bbox(lat: float, lon: float, radius_km: float) -> tuple[float, float, float, float]:
|
|
"""Bounding-Box-Approximation für lat/lon innerhalb radius_km."""
|
|
lat_delta = radius_km / 111.0
|
|
# cos darf bei Polen nicht 0 werden → mit kleinem Minimum absichern
|
|
cos_lat = max(abs(math.cos(math.radians(lat))), 0.01)
|
|
lon_delta = radius_km / (111.0 * cos_lat)
|
|
return (lat - lat_delta, lat + lat_delta, lon - lon_delta, lon + lon_delta)
|
|
|
|
|
|
@router.get("")
|
|
async def nearby_alerts(lat: float, lon: float, user=Depends(get_optional_user)):
|
|
now = datetime.utcnow().isoformat()
|
|
lat_min, lat_max, lon_min, lon_max = _bbox(lat, lon, _RADIUS_KM)
|
|
with db() as conn:
|
|
# Bounding-Box-Vorfilter per SQL (billig) → reduziert die Kandidaten
|
|
# auf ~10 Einträge statt "alle". Die exakte Haversine-Prüfung passiert
|
|
# anschließend in Python.
|
|
poisons = conn.execute(
|
|
"""SELECT lat, lon FROM poison
|
|
WHERE geloest=0 AND expires_at > ?
|
|
AND lat BETWEEN ? AND ?
|
|
AND lon BETWEEN ? AND ?""",
|
|
(now, lat_min, lat_max, lon_min, lon_max)
|
|
).fetchall()
|
|
lost = conn.execute(
|
|
"""SELECT lat, lon FROM lost_dogs
|
|
WHERE is_active=1
|
|
AND lat BETWEEN ? AND ?
|
|
AND lon BETWEEN ? AND ?""",
|
|
(lat_min, lat_max, lon_min, lon_max)
|
|
).fetchall()
|
|
# Letzten Standort des Users für geo-basierte Push-Filter speichern
|
|
if user:
|
|
conn.execute(
|
|
"""UPDATE push_subscriptions SET last_lat=?, last_lon=?
|
|
WHERE user_id=?""",
|
|
(lat, lon, user["id"])
|
|
)
|
|
|
|
has_poison = any(_haversine(lat, lon, r["lat"], r["lon"]) <= _RADIUS_M for r in poisons)
|
|
has_lost = any(_haversine(lat, lon, r["lat"], r["lon"]) <= _RADIUS_M for r in lost)
|
|
|
|
return {"poison": has_poison, "lost": has_lost}
|