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)