Refaktoriert: Logdatei-Auswertung in Hilfsfunktion ausgelagert, Redundanz in /stats und /api/stats beseitigt
This commit is contained in:
52
app.py
52
app.py
@@ -132,19 +132,7 @@ def index():
|
||||
return render_template('index.html', tage=tage, werktage=werktage, wochentag=wochentag, plusminus_result=plusminus_result, kw_berechnen=kw_berechnen, kw_datum=kw_datum, active_idx=active_idx)
|
||||
|
||||
|
||||
@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')
|
||||
def parse_log_stats(log_path):
|
||||
pageviews = 0
|
||||
func_counts = {}
|
||||
impressions_per_day = {}
|
||||
@@ -154,7 +142,6 @@ def stats():
|
||||
for line in f:
|
||||
if 'PAGEVIEW' in line:
|
||||
pageviews += 1
|
||||
# Datum extrahieren (YYYY-MM-DD)
|
||||
try:
|
||||
date = line[:10]
|
||||
if len(date) == 10 and date[4] == '-' and date[7] == '-':
|
||||
@@ -167,6 +154,21 @@ def stats():
|
||||
elif 'FUNC_API:' in line:
|
||||
api = line.split('FUNC_API:')[1].strip()
|
||||
api_counts[api] = api_counts.get(api, 0) + 1
|
||||
return pageviews, func_counts, impressions_per_day, api_counts
|
||||
|
||||
@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)
|
||||
log_path = os.path.join('log', 'pageviews.log')
|
||||
pageviews, func_counts, impressions_per_day, api_counts = parse_log_stats(log_path)
|
||||
return render_template('stats_dashboard.html', pageviews=pageviews, func_counts=func_counts, impressions_per_day=impressions_per_day, api_counts=api_counts)
|
||||
|
||||
# --- REST API ---
|
||||
@@ -279,27 +281,7 @@ def api_plusminus():
|
||||
@app.route('/api/stats', methods=['GET'])
|
||||
def api_stats():
|
||||
log_path = os.path.join('log', 'pageviews.log')
|
||||
pageviews = 0
|
||||
func_counts = {}
|
||||
impressions_per_day = {}
|
||||
api_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
|
||||
try:
|
||||
date = line[:10]
|
||||
if len(date) == 10 and date[4] == '-' and date[7] == '-':
|
||||
impressions_per_day[date] = impressions_per_day.get(date, 0) + 1
|
||||
except Exception:
|
||||
pass
|
||||
elif 'FUNC:' in line:
|
||||
func = line.split('FUNC:')[1].strip()
|
||||
func_counts[func] = func_counts.get(func, 0) + 1
|
||||
elif 'FUNC_API:' in line:
|
||||
api = line.split('FUNC_API:')[1].strip()
|
||||
api_counts[api] = api_counts.get(api, 0) + 1
|
||||
pageviews, func_counts, impressions_per_day, api_counts = parse_log_stats(log_path)
|
||||
return render_template('stats_dashboard.html', pageviews=pageviews, func_counts=func_counts, impressions_per_day=impressions_per_day, api_counts=api_counts)
|
||||
|
||||
@app.route('/api/monitor', methods=['GET'])
|
||||
|
Reference in New Issue
Block a user