Feature: Meine Wetterrekorde Sektion auf Wetter-Seite (SW by-v694)

- Backend: GET /api/weather/records — liest diary-Einträge mit weather_json
  und berechnet Kältester/Heißester Gassi, Stürmischster Tag, Regentage
- Frontend: #wttr-records 2×2 Grid-Karten unterhalb Hunde-Wetter
  (nur für eingeloggte User mit ≥3 Tagebucheinträgen mit Wetterdaten)
- SW-Version auf by-v694 erhöht, APP_VER auf 694
This commit is contained in:
rene 2026-05-04 20:28:06 +02:00
parent d081029618
commit 6152d6bf0e
5 changed files with 173 additions and 12 deletions

View file

@ -3,9 +3,11 @@ BAN YARO — Wetter-API
GET /api/weather?lat=&lon= aktuelles Wetter + Zecken-Warnung für Nutzerstandort
"""
import json
from fastapi import APIRouter, Query, HTTPException, Depends
import weather as weather_module
from auth import get_current_user
from database import db
router = APIRouter()
@ -31,3 +33,57 @@ async def get_weather_forecast(
return await weather_module.get_forecast(lat, lon)
except Exception as exc:
raise HTTPException(503, f'Wettervorhersage nicht verfügbar: {exc}')
@router.get('/records')
async def weather_records(user=Depends(get_current_user)):
"""Persönliche Wetterrekorde aus diary-Einträgen mit weather_json."""
uid = user["id"]
with db() as conn:
rows = conn.execute("""
SELECT d.datum, d.weather_json, d.titel
FROM diary d
WHERE d.user_id = ? AND d.weather_json IS NOT NULL
ORDER BY d.datum ASC
""", (uid,)).fetchall()
if not rows:
return {"records": None}
entries = []
for r in rows:
try:
w = json.loads(r["weather_json"])
entries.append({
"datum": r["datum"],
"titel": r["titel"],
"temp_c": w.get("temp_c"),
"wind_kmh": w.get("wind_kmh"),
"precip_prob": w.get("precip_prob"),
"desc": w.get("desc", ""),
"weathercode": w.get("weathercode"),
})
except Exception:
pass
if not entries:
return {"records": None}
temps = [e for e in entries if e["temp_c"] is not None]
winds = [e for e in entries if e["wind_kmh"] is not None]
records = {}
if temps:
kaeltester = min(temps, key=lambda e: e["temp_c"])
heissester = max(temps, key=lambda e: e["temp_c"])
records["kaeltester"] = kaeltester
records["heissester"] = heissester
if winds:
stuermischster = max(winds, key=lambda e: e["wind_kmh"])
records["stuermischster"] = stuermischster
regen_count = sum(1 for e in entries if (e.get("precip_prob") or 0) > 60)
records["regen_eintraege"] = regen_count
records["gesamt_eintraege"] = len(entries)
return {"records": records}