README: Anleitung für STATS_PASSWORD und Dashboard ergänzt

This commit is contained in:
2025-07-23 11:12:53 +02:00
parent 3041b86f3e
commit cdbe6f0574
3 changed files with 116 additions and 1 deletions

40
app.py
View File

@@ -1,9 +1,11 @@
from flask import Flask, render_template, request
from flask import Flask, render_template, request, redirect, url_for, session, abort
from datetime import datetime, timedelta
import numpy as np
from dateutil.relativedelta import relativedelta
import os
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', 'dev-key')
# HTML-Template wird jetzt aus templates/index.html geladen
@@ -11,10 +13,20 @@ WOCHENTAGE = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samsta
@app.route('/', methods=['GET', 'POST'])
def index():
# Rudimentäres Logging für Page Impressions
log_dir = 'log'
os.makedirs(log_dir, exist_ok=True)
with open(os.path.join(log_dir, 'pageviews.log'), 'a', encoding='utf-8') as f:
from datetime import datetime as dt
f.write(f"{dt.now().isoformat()} PAGEVIEW\n")
tage = werktage = wochentag = datumsrechnung = werktagsrechnung = kw_berechnen = kw_datum = wochen_monate = None
active_idx = 0
if request.method == 'POST':
action = request.form.get('action')
# Funktions-Logging
with open(os.path.join(log_dir, 'pageviews.log'), 'a', encoding='utf-8') as f:
from datetime import datetime as dt
f.write(f"{dt.now().isoformat()} FUNC: {action}\n")
if action == 'tage':
active_idx = 0
start = request.form.get('start1')
@@ -120,5 +132,31 @@ def index():
return render_template('index.html', tage=tage, werktage=werktage, wochentag=wochentag, datumsrechnung=datumsrechnung, werktagsrechnung=werktagsrechnung, kw_berechnen=kw_berechnen, kw_datum=kw_datum, active_idx=active_idx, wochen_monate=wochen_monate)
@app.route('/stats', methods=['GET', 'POST'])
def stats():
stats_password = os.environ.get('STATS_PASSWORD', 'changeme')
if 'stats_auth' not in session:
if request.method == 'POST':
if request.form.get('password') == stats_password:
session['stats_auth'] = True
return redirect(url_for('stats'))
else:
return render_template('stats_login.html', error='Falsches Passwort!')
return render_template('stats_login.html', error=None)
# Auswertung der Logdatei
log_path = os.path.join('log', 'pageviews.log')
pageviews = 0
func_counts = {}
if os.path.exists(log_path):
with open(log_path, encoding='utf-8') as f:
for line in f:
if 'PAGEVIEW' in line:
pageviews += 1
elif 'FUNC:' in line:
func = line.split('FUNC:')[1].strip()
func_counts[func] = func_counts.get(func, 0) + 1
return render_template('stats_dashboard.html', pageviews=pageviews, func_counts=func_counts)
if __name__ == '__main__':
app.run(debug=True)

View File

@@ -0,0 +1,52 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Dashboard Elpatrons Datumsrechner</title>
<link rel="stylesheet" href="/static/style.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
.dashboard-box { max-width: 600px; margin: 3em auto; background: #fff; border-radius: 12px; box-shadow: 0 2px 8px #cbd5e1; padding: 2em 2em 1.5em 2em; border: 1px solid #e5e7eb; }
.dashboard-box h2 { text-align: center; margin-bottom: 1.2em; }
.stats-row { display: flex; justify-content: space-between; margin-bottom: 2em; }
.stats-label { color: #64748b; }
.stats-value { font-size: 1.5em; font-weight: bold; }
.chart-container { margin: 2em 0; }
</style>
</head>
<body>
<div class="dashboard-box">
<h2>Statistik-Dashboard</h2>
<div class="stats-row">
<div class="stats-label">Gesamt-Pageviews:</div>
<div class="stats-value">{{ pageviews }}</div>
</div>
<div class="chart-container">
<canvas id="funcChart" width="400" height="220"></canvas>
</div>
<a href="/" style="color:#2563eb;">Zurück zur App</a>
</div>
<script>
const funcCounts = {{ func_counts|tojson }};
const labels = Object.keys(funcCounts);
const data = Object.values(funcCounts);
new Chart(document.getElementById('funcChart').getContext('2d'), {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Funktionsaufrufe',
data: data,
backgroundColor: '#2563eb',
}]
},
options: {
plugins: { legend: { display: false } },
scales: {
y: { beginAtZero: true, ticks: { stepSize: 1 } }
}
}
});
</script>
</body>
</html>

View File

@@ -0,0 +1,25 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Dashboard Login</title>
<link rel="stylesheet" href="/static/style.css">
<style>
.login-box { max-width: 340px; margin: 5em auto; background: #fff; border-radius: 12px; box-shadow: 0 2px 8px #cbd5e1; padding: 2em 2em 1.5em 2em; border: 1px solid #e5e7eb; }
.login-box h2 { text-align: center; margin-bottom: 1.2em; }
.login-box input[type=password] { width: 100%; padding: 0.6em; border-radius: 6px; border: 1px solid #e5e7eb; margin-bottom: 1em; }
.login-box button { width: 100%; }
.error { color: #dc2626; text-align: center; margin-bottom: 1em; }
</style>
</head>
<body>
<div class="login-box">
<h2>Dashboard Login</h2>
{% if error %}<div class="error">{{ error }}</div>{% endif %}
<form method="post">
<input type="password" name="password" placeholder="Passwort" required autofocus>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>