file_access on seperate page
This commit is contained in:
parent
940984e34f
commit
9023c41cab
110
analytics.py
110
analytics.py
@ -378,36 +378,7 @@ def dashboard():
|
|||||||
params_for_filter = (start_str, filetype + '%')
|
params_for_filter = (start_str, filetype + '%')
|
||||||
|
|
||||||
# 1. Top files by access count
|
# 1. Top files by access count
|
||||||
query = f'''
|
# removed and moved to file_access() function
|
||||||
SELECT rel_path, COUNT(*) as access_count
|
|
||||||
FROM file_access_log
|
|
||||||
WHERE timestamp >= ? {filetype_filter_sql}
|
|
||||||
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,
|
|
||||||
'access_count': access_count,
|
|
||||||
'category': hf.extract_structure_from_string(rel_path)[0]
|
|
||||||
}
|
|
||||||
for rel_path, access_count in rows
|
|
||||||
]
|
|
||||||
|
|
||||||
categories = set(r['category'] for r in rows) # distinct categories
|
|
||||||
top20 = [
|
|
||||||
{
|
|
||||||
'category': categorie,
|
|
||||||
'files': [r for r in rows if r['category'] == categorie][:20]
|
|
||||||
}
|
|
||||||
for categorie in categories
|
|
||||||
]
|
|
||||||
|
|
||||||
# 2. Distinct device trend
|
# 2. Distinct device trend
|
||||||
# We'll group by hour if "today", by day if "7days"/"30days", by month if "365days"
|
# We'll group by hour if "today", by day if "7days"/"30days", by month if "365days"
|
||||||
@ -614,7 +585,6 @@ def dashboard():
|
|||||||
return render_template(
|
return render_template(
|
||||||
"dashboard.html",
|
"dashboard.html",
|
||||||
timeframe=session['timeframe'],
|
timeframe=session['timeframe'],
|
||||||
top20 = top20,
|
|
||||||
distinct_device_data=distinct_device_data,
|
distinct_device_data=distinct_device_data,
|
||||||
user_agent_data=user_agent_data,
|
user_agent_data=user_agent_data,
|
||||||
folder_data=folder_data,
|
folder_data=folder_data,
|
||||||
@ -629,6 +599,84 @@ def dashboard():
|
|||||||
title_long=title_long
|
title_long=title_long
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@require_secret
|
||||||
|
def file_access():
|
||||||
|
if 'timeframe' not in session:
|
||||||
|
session['timeframe'] = 'last24hours'
|
||||||
|
session['timeframe'] = request.args.get('timeframe', session['timeframe'])
|
||||||
|
|
||||||
|
now = datetime.now()
|
||||||
|
|
||||||
|
filetype = 'audio/'
|
||||||
|
|
||||||
|
# Determine start time based on session['timeframe']
|
||||||
|
if session['timeframe'] == 'last24hours':
|
||||||
|
start_dt = now - timedelta(hours=24)
|
||||||
|
elif session['timeframe'] == '7days':
|
||||||
|
start_dt = now - timedelta(days=7)
|
||||||
|
elif session['timeframe'] == '30days':
|
||||||
|
start_dt = now - timedelta(days=30)
|
||||||
|
elif session['timeframe'] == '365days':
|
||||||
|
start_dt = now - timedelta(days=365)
|
||||||
|
else:
|
||||||
|
start_dt = now - timedelta(hours=24)
|
||||||
|
|
||||||
|
# We'll compare the textual timestamp (ISO 8601).
|
||||||
|
start_str = start_dt.isoformat()
|
||||||
|
|
||||||
|
# Filter for mimes that start with the given type
|
||||||
|
filetype_filter_sql = "AND mime LIKE ?"
|
||||||
|
params_for_filter = (start_str, filetype + '%')
|
||||||
|
|
||||||
|
# 1. Top files by access count
|
||||||
|
query = f'''
|
||||||
|
SELECT rel_path, COUNT(*) as access_count
|
||||||
|
FROM file_access_log
|
||||||
|
WHERE timestamp >= ? {filetype_filter_sql}
|
||||||
|
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,
|
||||||
|
'access_count': access_count,
|
||||||
|
'category': hf.extract_structure_from_string(rel_path)[0]
|
||||||
|
}
|
||||||
|
for rel_path, access_count in rows
|
||||||
|
]
|
||||||
|
|
||||||
|
# Get possible categories from the rows
|
||||||
|
categories = sorted({r['category'] for r in rows if r['category'] is not None})
|
||||||
|
all_categories = [None] + categories
|
||||||
|
top20 = []
|
||||||
|
for category in all_categories:
|
||||||
|
label = category if category is not None else 'Keine Kategorie gefunden !'
|
||||||
|
files = [r for r in rows if r['category'] == category][:20]
|
||||||
|
top20.append({
|
||||||
|
'category': label,
|
||||||
|
'files': files
|
||||||
|
})
|
||||||
|
|
||||||
|
title_short = app_config.get('TITLE_SHORT', 'Default Title')
|
||||||
|
title_long = app_config.get('TITLE_LONG' , 'Default Title')
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
"file_access.html",
|
||||||
|
timeframe=session['timeframe'],
|
||||||
|
top20 = top20,
|
||||||
|
admin_enabled=auth.is_admin(),
|
||||||
|
title_short=title_short,
|
||||||
|
title_long=title_long
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def export_to_excel():
|
def export_to_excel():
|
||||||
"""Export search_db to an Excel file and store it locally."""
|
"""Export search_db to an Excel file and store it locally."""
|
||||||
|
|
||||||
|
|||||||
3
app.py
3
app.py
@ -43,6 +43,7 @@ if os.environ.get('FLASK_ENV') == 'production':
|
|||||||
app.config['SESSION_COOKIE_SECURE'] = True
|
app.config['SESSION_COOKIE_SECURE'] = True
|
||||||
|
|
||||||
app.add_url_rule('/dashboard', view_func=a.dashboard)
|
app.add_url_rule('/dashboard', view_func=a.dashboard)
|
||||||
|
app.add_url_rule('/file_access', view_func=a.file_access)
|
||||||
app.add_url_rule('/connections', view_func=a.connections)
|
app.add_url_rule('/connections', view_func=a.connections)
|
||||||
app.add_url_rule('/mylinks', view_func=auth.mylinks)
|
app.add_url_rule('/mylinks', view_func=auth.mylinks)
|
||||||
app.add_url_rule('/remove_secret', view_func=auth.remove_secret, methods=['POST'])
|
app.add_url_rule('/remove_secret', view_func=auth.remove_secret, methods=['POST'])
|
||||||
@ -206,6 +207,8 @@ def generate_breadcrumbs(subpath=None):
|
|||||||
path_accum = ""
|
path_accum = ""
|
||||||
for part in parts:
|
for part in parts:
|
||||||
path_accum = f"{path_accum}/{part}" if path_accum else part
|
path_accum = f"{path_accum}/{part}" if path_accum else part
|
||||||
|
if 'toplist' in part:
|
||||||
|
part = part.replace('toplist', 'oft angehört')
|
||||||
breadcrumbs.append({'name': part, 'path': path_accum})
|
breadcrumbs.append({'name': part, 'path': path_accum})
|
||||||
return breadcrumbs
|
return breadcrumbs
|
||||||
|
|
||||||
|
|||||||
@ -124,9 +124,12 @@ function renderContent(data) {
|
|||||||
if (admin_enabled && data.breadcrumbs.length != 1 && dir.share) {
|
if (admin_enabled && data.breadcrumbs.length != 1 && dir.share) {
|
||||||
share_link = `<a href="#" class="create-share" data-url="${dir.path}">⚙️</a>`;
|
share_link = `<a href="#" class="create-share" data-url="${dir.path}">⚙️</a>`;
|
||||||
}
|
}
|
||||||
contentHTML += `<li class="directory-item"><a href="#" class="directory-link" data-path="${dir.path}">📁 ${dir.name}</a>
|
if (dir.path.includes('toplist')) {
|
||||||
${share_link}
|
link_symbol = '⭐';
|
||||||
</li>`;
|
} else {
|
||||||
|
link_symbol = '📁';
|
||||||
|
}
|
||||||
|
contentHTML += `<li class="directory-item"><a href="#" class="directory-link" data-path="${dir.path}">${link_symbol} ${dir.name}</a>${share_link}</li>`;
|
||||||
});
|
});
|
||||||
if (data.breadcrumbs.length === 1) {
|
if (data.breadcrumbs.length === 1) {
|
||||||
contentHTML += `<li class="link-item" onclick="viewSearch()"><a onclick="viewSearch() class="link-link">🔎 Suche</a></li>`;
|
contentHTML += `<li class="link-item" onclick="viewSearch()"><a onclick="viewSearch() class="link-link">🔎 Suche</a></li>`;
|
||||||
|
|||||||
@ -45,6 +45,8 @@
|
|||||||
<span> | </span>
|
<span> | </span>
|
||||||
<a href="{{ url_for('dashboard') }}">Dashbord</a>
|
<a href="{{ url_for('dashboard') }}">Dashbord</a>
|
||||||
<span> | </span>
|
<span> | </span>
|
||||||
|
<a href="{{ url_for('file_access') }}">Dateizugriffe</a>
|
||||||
|
<span> | </span>
|
||||||
<a href="{{ url_for('songs_dashboard') }}">Wiederholungen</a>
|
<a href="{{ url_for('songs_dashboard') }}">Wiederholungen</a>
|
||||||
<span> | </span>
|
<span> | </span>
|
||||||
<a href="{{ url_for('folder_secret_config_editor') }}" id="edit-folder-config">Ordnerkonfiguration</a>
|
<a href="{{ url_for('folder_secret_config_editor') }}" id="edit-folder-config">Ordnerkonfiguration</a>
|
||||||
|
|||||||
@ -210,38 +210,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Detailed Table of Top File Accesses -->
|
|
||||||
{% for top20_item in top20 %}
|
|
||||||
<div class="card mb-4">
|
|
||||||
<div class="card-header">
|
|
||||||
Top 20 Dateizugriffe ({{ top20_item['category'] }})
|
|
||||||
</div>
|
|
||||||
<div class="card-body">
|
|
||||||
<div class="table-responsive">
|
|
||||||
<table class="table table-striped">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Access Count</th>
|
|
||||||
<th>File Path</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{% for row in top20_item['files'] %}
|
|
||||||
<tr>
|
|
||||||
<td>{{ row.access_count }}</td>
|
|
||||||
<td>{{ row.rel_path }}</td>
|
|
||||||
</tr>
|
|
||||||
{% else %}
|
|
||||||
<tr>
|
|
||||||
<td colspan="2">No data available for the selected timeframe.</td>
|
|
||||||
</tr>
|
|
||||||
{% endfor %}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|||||||
99
templates/file_access.html
Normal file
99
templates/file_access.html
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
{# templates/file_access.html #}
|
||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{# page title #}
|
||||||
|
{% block title %}Dateizugriffe{% endblock %}
|
||||||
|
|
||||||
|
{# page content #}
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
<!-- Main Container -->
|
||||||
|
<div class="container">
|
||||||
|
<h2>Auswertung-Dateizugriffe</h2>
|
||||||
|
|
||||||
|
<!-- Dropdown Controls -->
|
||||||
|
<div class="mb-4 d-flex flex-wrap gap-2">
|
||||||
|
<!-- Timeframe Dropdown -->
|
||||||
|
<div class="dropdown">
|
||||||
|
<button class="btn btn-secondary dropdown-toggle"
|
||||||
|
type="button" id="timeframeDropdown" data-bs-toggle="dropdown" aria-expanded="false">
|
||||||
|
{% if session['timeframe'] == 'last24hours' %}
|
||||||
|
Last 24 Hours
|
||||||
|
{% elif session['timeframe'] == '7days' %}
|
||||||
|
Last 7 Days
|
||||||
|
{% elif session['timeframe'] == '30days' %}
|
||||||
|
Last 30 Days
|
||||||
|
{% elif session['timeframe'] == '365days' %}
|
||||||
|
Last 365 Days
|
||||||
|
{% else %}
|
||||||
|
Select Timeframe
|
||||||
|
{% endif %}
|
||||||
|
</button>
|
||||||
|
<ul class="dropdown-menu" aria-labelledby="timeframeDropdown">
|
||||||
|
<li>
|
||||||
|
<a class="dropdown-item {% if session['timeframe'] == 'last24hours' %}active{% endif %}"
|
||||||
|
href="{{ url_for('file_access', timeframe='last24hours') }}">
|
||||||
|
Last 24 Hours
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a class="dropdown-item {% if session['timeframe'] == '7days' %}active{% endif %}"
|
||||||
|
href="{{ url_for('file_access', timeframe='7days') }}">
|
||||||
|
Last 7 Days
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a class="dropdown-item {% if session['timeframe'] == '30days' %}active{% endif %}"
|
||||||
|
href="{{ url_for('file_access', timeframe='30days') }}">
|
||||||
|
Last 30 Days
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a class="dropdown-item {% if session['timeframe'] == '365days' %}active{% endif %}"
|
||||||
|
href="{{ url_for('file_access', timeframe='365days') }}">
|
||||||
|
Last 365 Days
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Detailed Table of Top File Accesses -->
|
||||||
|
{% for top20_item in top20 %}
|
||||||
|
<div class="card mb-4">
|
||||||
|
<div class="card-header">
|
||||||
|
Top 20 Dateizugriffe ({{ top20_item['category'] }})
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Access Count</th>
|
||||||
|
<th>File Path</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for row in top20_item['files'] %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ row.access_count }}</td>
|
||||||
|
<td>{{ row.rel_path }}</td>
|
||||||
|
</tr>
|
||||||
|
{% else %}
|
||||||
|
<tr>
|
||||||
|
<td colspan="2">No data available for the selected timeframe.</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block scripts %}
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
@ -8,7 +8,7 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
<!-- Main Container -->
|
<!-- Main Container -->
|
||||||
<div class="container-fluid px-4">
|
<div class="container">
|
||||||
<h2>Analyse Wiederholungen</h2>
|
<h2>Analyse Wiederholungen</h2>
|
||||||
<!-- Dropdown Controls -->
|
<!-- Dropdown Controls -->
|
||||||
<div class="mb-4 d-flex flex-wrap gap-2">
|
<div class="mb-4 d-flex flex-wrap gap-2">
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user