File size: 3,796 Bytes
c44fa4d
 
 
 
 
 
 
 
 
 
e7dbb79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
title: Rheinpegel Python WebApp
emoji: 📚
colorFrom: purple
colorTo: green
sdk: docker
pinned: false
---

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference

<<<<<<< HEAD
# rheinpegel-python-app
=======
# Rheinpegel App

Eine moderne FastAPI-Anwendung, die den aktuellen Rheinpegel (Köln) abruft, zwischenspeichert und visuell aufbereitet. Die Anwendung kombiniert serverseitiges Polling mit einem reaktiven Frontend (AJAX, Countdown, Sparkline) und liefert zusätzlich Status- sowie Prometheus-Kennzahlen.

## Hauptfunktionen

- **Asynchrones Polling** der offiziellen Quelle via `httpx` mit Timeout (8&nbsp;s) und exponentiellem Backoff (4 Versuche).
- **Automatische Format-Erkennung** für JSON oder XML und robuste Feldzuordnung.
- **Trend-Berechnung** aus Rohdaten oder anhand der letzten Messwerte (Δ > 2 cm ⇒ ↑, Δ < −2 ⇒ ↓).
- **Mehrstufiges Caching**: In-Memory-Ringpuffer (48 Werte) + JSON-Persistenz unter `data/cache.json`.
- **Demo-Modus** bei Netzwerkfehlern oder via `?demo=1` mit synthetischen, plausiblen Messwerten.
- **FastAPI + Jinja2** Dashboard mit responsive Karte, Warnstufen, Sparkline (Inline-SVG) sowie Auto-Refresh-Toggle und Countdown.
- **Prometheus-Metriken**: letzte Latenz, Erfolgs-/Fehlerzähler, Datenalter und Health-Endpoint.

## Voraussetzungen

- Python 3.11+
- Optional: [uv](https://github.com/astral-sh/uv) für schnelle virtuelle Umgebungen

## Installation & Setup

### Variante A: uv

```bash
uv venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
uv pip install -e .
cp .env.example .env
```

### Variante B: pip / venv

```bash
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install --upgrade pip
pip install -e .
copy .env.example .env  # Windows
```

> **Hinweis:** Die Umgebungsvariablen (.env) steuern u.a. den Polling-Intervall (`REFRESH_SECONDS`), die Quelle (`SOURCE_URL`) und die Zeitzone (`TZ`).

## Starten der Anwendung

```bash
uvicorn app.main:app --reload
# oder
python -m app.main
```

Die Oberfläche ist anschließend unter http://127.0.0.1:8000 erreichbar.

## Wichtige Endpunkte

| Route           | Beschreibung                                   |
|-----------------|-------------------------------------------------|
| `/`             | Dashboard mit Live-Karte, Trend, Sparkline      |
| `/api/latest`   | Neueste Messung (JSON) + komplette Historie     |
| `/api/history`  | Maximal 48 Messpunkte aus dem Ringpuffer        |
| `/healthz`      | 200 OK, wenn der Hintergrund-Task aktiv ist     |
| `/metrics`      | Prometheus-kompatible Kennzahlen                |

### Demo-Modus

Alle Endpunkte akzeptieren `?demo=1`, um synthetische Daten zu liefern und das UI als „Demo“ zu markieren.

## Tests

```bash
pytest
```

Die Tests decken das Parsing (JSON/XML) sowie zentrale FastAPI-Endpunkte ab.

## Docker

```bash
docker build -t rheinpegel-app .
docker run -p 8000:8000 --env-file .env rheinpegel-app
```

Oder via Compose:

```bash
docker-compose up --build
```

Der Container läuft als Non-Root-User, besitzt einen Healthcheck (`/healthz`) und bindet `./data` für persistente Caches ein.

## Beispiel systemd Unit (optional)

```ini
[Unit]
Description=Rheinpegel App
After=network.target

[Service]
User=www-data
WorkingDirectory=/opt/rheinpegel-app
EnvironmentFile=/opt/rheinpegel-app/.env
ExecStart=/opt/rheinpegel-app/.venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 8000
Restart=always

[Install]
WantedBy=multi-user.target
```

## Weiterentwicklung

- UI-Optimierungen, z. B. zusätzliche Karten oder historische Auswertungen
- Export in weitere Formate (CSV/ICS)
- Alarm-Benachrichtigungen (E-Mail, Webhooks) basierend auf Warnstufen

Viel Spaß beim Ausprobieren der Rheinpegel-App!