Graph hinzugefügt

This commit is contained in:
hubobel 2026-06-23 18:36:27 +02:00
parent b65125fcf4
commit 43641057f2
3 changed files with 174 additions and 25 deletions

100
app.py
View file

@ -8,6 +8,7 @@ from urllib.request import urlopen
from pathlib import Path
from influxdb_client import InfluxDBClient
from datetime import datetime, timedelta
from collections import defaultdict
app = Flask(__name__)
@ -184,14 +185,16 @@ def get_balance_color(balance):
return "green"
def get_balance_history(days=30):
def get_balance_comparison():
if not all([
INFLUX_URL,
INFLUX_ORG,
INFLUX_BUCKET,
INFLUX_TOKEN
]):
return []
return {}
client = InfluxDBClient(
url=INFLUX_URL,
token=INFLUX_TOKEN,
@ -200,24 +203,103 @@ def get_balance_history(days=30):
query = f'''
from(bucket: "{INFLUX_BUCKET}")
|> range(start: -{days}d)
|> range(start: -180d)
|> filter(fn: (r) => r._measurement == "Kontostand")
|> filter(fn: (r) => r._field == "value")
'''
result = client.query_api().query(query)
history = []
raw_data = []
for table in result:
for record in table.records:
history.append({
"date": record.get_time().strftime("%d.%m"),
raw_data.append({
"time": record.get_time(),
"value": round(record.get_value(), 2)
})
return history
raw_data.sort(
key=lambda x: x["time"]
)
periods = defaultdict(dict)
for item in raw_data:
dt = item["time"]
if dt.day >= 15:
period_start = datetime(
dt.year,
dt.month,
15
)
else:
if dt.month == 1:
period_start = datetime(
dt.year - 1,
12,
15
)
else:
period_start = datetime(
dt.year,
dt.month - 1,
15
)
day_index = (
dt.date() -
period_start.date()
).days + 1
periods[period_start][day_index] = item["value"]
sorted_periods = sorted(
periods.keys(),
reverse=True
)
result_data = {}
names = [
"current",
"period_1",
"period_2",
"period_3",
"period_4"
]
for idx, period in enumerate(sorted_periods[:5]):
data = []
days = periods[period]
last_value = None
for day in range(1, 32):
if day in days:
last_value = days[day]
if last_value is not None:
data.append({
"day": day,
"value": last_value
})
result_data[names[idx]] = data
return result_data
@app.route("/")
def index():
@ -238,8 +320,8 @@ def index():
balance
)
balance_history = get_balance_history()
balance_history = get_balance_comparison()
print(balance_history)
return render_template(
"index.html",
year=year,