From 7dd107fc725544537a4840485b67f7af1bded880 Mon Sep 17 00:00:00 2001 From: lelo Date: Thu, 29 May 2025 12:38:11 +0000 Subject: [PATCH] add top list folder --- app.py | 23 +++++++++++++++++++++ helperfunctions.py | 51 +++++++++++++++++++++++++++++++++++++++++++++- static/app.js | 3 +++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 6284744..4ca3361 100755 --- a/app.py +++ b/app.py @@ -274,6 +274,29 @@ def api_browse(subpath): 'folder_today': [], 'folder_yesterday': [] }) + + if subpath.startswith('toplist'): + foldernames = [] + files = [] + split_path = subpath.split('/') + if len(split_path) == 1 and split_path[0] == 'toplist': + foldernames = [ + {'name': 'Predigten', 'path': 'toplist/predigt'}, + {'name': 'Chorlieder', 'path': 'toplist/chor'}, + {'name': 'Gemeinsamer Gesang', 'path': 'toplist/gemeinsamer_gesang'}, + {'name': 'Gruppengesang', 'path': 'toplist/gruppengesang'}, + ] + elif len(split_path) > 1 and split_path[0] == 'toplist': + files = [ + hf.generate_top_list(split_path[1]) + ] + + return jsonify({ + 'breadcrumbs': generate_breadcrumbs(subpath), + 'directories': foldernames, + 'files': files + }) + root, *relative_parts = subpath.split('/') base_path = session['folders'][root] diff --git a/helperfunctions.py b/helperfunctions.py index 32d1310..7323e89 100644 --- a/helperfunctions.py +++ b/helperfunctions.py @@ -1,6 +1,10 @@ from datetime import datetime import re import os +import sqlite3 +from datetime import datetime, timedelta + +log_db = sqlite3.connect("access_log.db", check_same_thread=False) def extract_date_from_string(string_with_date): # grab X.Y.Z where X,Y,Z are 1–4 digits @@ -89,4 +93,49 @@ def extract_structure_from_string(input_string): titel = None name = None - return category, titel, name \ No newline at end of file + return category, titel, name + +def generate_top_list(category): + + now = datetime.now() + + # We'll compare the timestamp + start_dt = now - timedelta(days=30) + start_str = start_dt.isoformat() + + # Filter for mimes that start with the given type + params_for_filter = (start_str) + + # 1. Top files by access count + query = f''' + SELECT rel_path, COUNT(*) as access_count + FROM file_access_log + WHERE timestamp >= ? AND mime LIKE 'audio/%' + GROUP BY rel_path + ORDER BY access_count DESC + LIMIT 1000 + ''' + with log_db: + cursor = log_db.execute(query, params_for_filter) + rows = cursor.fetchall() + + # Convert rows to a list of dictionaries and add category + rows = [ + { + 'rel_path': rel_path, + 'category': extract_structure_from_string(rel_path)[0] + } + for rel_path in rows + ] + + rows = [r for r in rows if r['category'] == category][:20] + + filelist = [ + { + 'name': rel_path, + 'path': rel_path + } + for rel_path in [r['rel_path'] for r in rows] + ] + + return filelist diff --git a/static/app.js b/static/app.js index 621180a..95150d1 100644 --- a/static/app.js +++ b/static/app.js @@ -116,6 +116,9 @@ function renderContent(data) { } else if (data.breadcrumbs.length === 1 && Array.isArray(data.folder_yesterday) && data.folder_yesterday.length > 0) { contentHTML += `
  • 📅 Gestern
  • `; } + if (data.breadcrumbs.length === 1 ) { + contentHTML += `
  • 👥 oft gehört
  • `; + } console.log(data.folder_today, data.folder_yesterday); data.directories.forEach(dir => { if (admin_enabled && data.breadcrumbs.length != 1 && dir.share) {