Graph hinzugefügt

This commit is contained in:
hubobel 2026-06-23 17:49:52 +02:00
parent b8897ce67c
commit 2f4fa79027
2 changed files with 113 additions and 7 deletions

67
app.py
View file

@ -1,13 +1,15 @@
from pathlib import Path
from flask import Flask, render_template, request from flask import Flask, render_template, request
import subprocess import subprocess
import sys import sys
import json import json
from pathlib import Path
from flask import redirect from flask import redirect
from datetime import datetime
from urllib.request import urlopen from urllib.request import urlopen
from pathlib import Path from pathlib import Path
from influxdb_client import InfluxDBClient
from datetime import datetime, timedelta
app = Flask(__name__) app = Flask(__name__)
@ -40,6 +42,10 @@ with open(config_file, encoding="utf-8") as f:
IOBROKER_HOST = config["IOBROKER_HOST"] IOBROKER_HOST = config["IOBROKER_HOST"]
IOBROKER_PORT = config["IOBROKER_PORT"] IOBROKER_PORT = config["IOBROKER_PORT"]
IOBROKER_DP = config["IOBROKER_DP"] IOBROKER_DP = config["IOBROKER_DP"]
INFLUX_URL = config["INFLUX_URL"]
INFLUX_ORG = config["INFLUX_ORG"]
INFLUX_BUCKET = config["INFLUX_BUCKET"]
INFLUX_TOKEN = config["INFLUX_TOKEN"]
BALANCE_YELLOW_DAY = int( BALANCE_YELLOW_DAY = int(
config["BALANCE_YELLOW_DAY"] config["BALANCE_YELLOW_DAY"]
) )
@ -178,8 +184,37 @@ def get_balance_color(balance):
return "orange" return "orange"
return "green" return "green"
@app.route("/")
def get_balance_history(days=30):
client = InfluxDBClient(
url=INFLUX_URL,
token=INFLUX_TOKEN,
org=INFLUX_ORG
)
query = f'''
from(bucket: "{INFLUX_BUCKET}")
|> range(start: -{days}d)
|> filter(fn: (r) => r._measurement == "Kontostand")
|> filter(fn: (r) => r._field == "value")
'''
result = client.query_api().query(query)
history = []
for table in result:
for record in table.records:
history.append({
"date": record.get_time().strftime("%d.%m"),
"value": round(record.get_value(), 2)
})
return history
@app.route("/")
def index(): def index():
( (
@ -192,8 +227,14 @@ def index():
total_expenses, total_expenses,
total_saldo total_saldo
) = get_current_week_data() ) = get_current_week_data()
balance = get_balance() balance = get_balance()
balance_color = get_balance_color(balance)
balance_color = get_balance_color(
balance
)
balance_history = get_balance_history()
return render_template( return render_template(
"index.html", "index.html",
@ -206,9 +247,21 @@ def index():
total_expenses=total_expenses, total_expenses=total_expenses,
total_saldo=total_saldo, total_saldo=total_saldo,
balance=balance, balance=balance,
balance_color = get_balance_color(balance) balance_color=balance_color,
balance_history=balance_history
) )
@app.route("/test-influx")
def test_influx():
data = get_balance_history()
return {
"count": len(data),
"first": data[0] if data else None,
"last": data[-1] if data else None
}
@app.route("/run/balance", methods=["POST"]) @app.route("/run/balance", methods=["POST"])
def run_balance(): def run_balance():

View file

@ -15,6 +15,8 @@
href="{{ url_for('static', filename='style.css') }}"> href="{{ url_for('static', filename='style.css') }}">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head> </head>
<body> <body>
@ -112,7 +114,15 @@
</p> </p>
</div> </div>
<div class="card">
<h2>
Kontostand letzte 30 Tage
</h2>
<canvas id="balanceChart"></canvas>
</div>
<div class="card"> <div class="card">
<h2> <h2>
@ -242,6 +252,49 @@ Hintergasse © 2026
</div> </div>
<script>
const history = {{ balance_history|tojson }};
const labels = history.map(item => item.date);
const values = history.map(item => item.value);
new Chart(
document.getElementById("balanceChart"),
{
type: "line",
data: {
labels: labels,
datasets: [{
label: "Kontostand (€)",
data: values,
tension: 0.3
}]
},
options: {
responsive: true,
plugins: {
legend: {
display: false
}
},
scales: {
y: {
beginAtZero: false
}
}
}
}
);
</script>
</body> </body>
</html> </html>