From 5b4e7188cd68be255dec5995f3c15a279f4c778a Mon Sep 17 00:00:00 2001 From: lelo Date: Sat, 24 May 2025 09:49:53 +0000 Subject: [PATCH] add yesterday --- analytics.py | 45 ++++++++++++++++++++++++++++++++++++++++----- app.py | 19 +++++++++++++------ static/app.js | 5 ++++- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/analytics.py b/analytics.py index 500e254..dd3617a 100644 --- a/analytics.py +++ b/analytics.py @@ -11,6 +11,7 @@ import helperfunctions as hf file_access_temp = [] folder_today = [] +folder_yesterday = [] app_config = auth.return_app_config() @@ -102,7 +103,7 @@ def parse_timestamp(ts_str): def log_file_access(rel_path, filesize, mime, ip_address, user_agent, device_id, cached): """Insert a file access record into the database and prune entries older than 10 minutes, and track today’s files separately in folder_today.""" - global file_access_temp, folder_today + global file_access_temp, folder_today, folder_yesterday # Create a timezone-aware timestamp now = datetime.now(timezone.utc).astimezone() @@ -126,22 +127,37 @@ def log_file_access(rel_path, filesize, mime, ip_address, user_agent, device_id, ] # Keep only today's entries in folder_today - date_str = iso_ts.split('T', 1)[0] + today_str = iso_ts.split('T', 1)[0] folder_today[:] = [ entry for entry in folder_today - if entry['date_str'] == date_str + if entry['date_str'] == today_str + ] + + # Keep only yesterday's entries in folder_yesterday + yesterday_str = (now - timedelta(days=1)).isoformat().split('T', 1)[0] + folder_yesterday[:] = [ + entry for entry in folder_yesterday + if entry['date_str'] == yesterday_str ] # If this new access is from today, record it # Compare the helper’s YYYY-MM-DD string to today’s ISO date string date_from_path = hf.extract_date_from_string(rel_path) - if date_from_path == date_str: + if date_from_path == today_str: # get just the folder part (everything before the final '/') folder_path = rel_path.rsplit('/', 1)[0] if '/' in rel_path else rel_path # only append if that folder isn't already in folder_today if not any(entry['rel_path'] == folder_path for entry in folder_today): - folder_today.append({'date_str': date_str, 'rel_path': folder_path}) + folder_today.append({'date_str': today_str, 'rel_path': folder_path}) + + # If this new access is from yesterday, record it + if date_from_path == yesterday_str: + # get just the folder part (everything before the final '/') + folder_path = rel_path.rsplit('/', 1)[0] if '/' in rel_path else rel_path + # only append if that folder isn't already in folder_yesterday + if not any(entry['rel_path'] == folder_path for entry in folder_yesterday): + folder_yesterday.append({'date_str': yesterday_str, 'rel_path': folder_path}) # Finally, insert the new access at the top of the temp log file_access_temp.insert(0, [ @@ -177,6 +193,25 @@ def return_folder_today(): return filtered +def return_folder_yesterday(): + """ + Return only those folder_yesterday entries whose first segment + (up to the first '/') is in session['folders'].keys(). + """ + valid_keys = set(session.get('folders', {}).keys()) + + filtered = [] + for entry in folder_yesterday: + # get the part before the first slash + top_level = entry['rel_path'].split('/', 1)[0] + + # include only if this segment is one of the session keys + if top_level in valid_keys: + filtered.append(entry) + + return filtered + + def return_file_access(): """Return recent file access logs from memory (the last 10 minutes).""" global file_access_temp diff --git a/app.py b/app.py index 1ae7f9b..6284744 100755 --- a/app.py +++ b/app.py @@ -254,19 +254,25 @@ def api_browse(subpath): 'breadcrumbs': generate_breadcrumbs(), 'directories': foldernames, 'files': [], - 'folder_today': a.return_folder_today() + 'folder_today': a.return_folder_today(), + 'folder_yesterday': a.return_folder_yesterday() }) - if subpath == 'today': + if subpath == 'heute' or subpath == 'gestern': foldernames = [] - for item in a.return_folder_today(): - foldernames.append({'name': item['rel_path'], 'path': item['rel_path']}) + if len(a.return_folder_today()) > 0: + for item in a.return_folder_today(): + foldernames.append({'name': item['rel_path'], 'path': item['rel_path']}) + elif len(a.return_folder_yesterday()) > 0: + for item in a.return_folder_yesterday(): + foldernames.append({'name': item['rel_path'], 'path': item['rel_path']}) return jsonify({ 'breadcrumbs': generate_breadcrumbs(subpath), 'directories': foldernames, 'files': [], - 'folder_today': [] + 'folder_today': [], + 'folder_yesterday': [] }) root, *relative_parts = subpath.split('/') @@ -299,7 +305,8 @@ def api_browse(subpath): 'breadcrumbs': breadcrumbs, 'directories': directories, 'files': files, - 'folder_today': a.return_folder_today() + 'folder_today': a.return_folder_today(), + 'folder_yesterday': a.return_folder_yesterday() } # If a filename was selected include it. diff --git a/static/app.js b/static/app.js index 43e2b5c..621180a 100644 --- a/static/app.js +++ b/static/app.js @@ -112,8 +112,11 @@ function renderContent(data) { } else { contentHTML += '