add distinct devices

This commit is contained in:
lelo 2025-04-01 21:57:43 +02:00
parent 4f88727321
commit 7daf2eea6e
2 changed files with 73 additions and 33 deletions

View File

@ -155,20 +155,49 @@ def dashboard():
cursor = log_db.execute(query, params_for_filter)
rows = cursor.fetchall()
# 2. Daily access trend (line chart)
# We'll group by day using substr(timestamp, 1, 10) -> YYYY-MM-DD
# 2. Distinct device trend
# We'll group by hour if "today", by day if "7days"/"30days", by month if "365days"
if timeframe == 'today':
# Group by hour: substr(timestamp, 12, 2) -> HH
query = f'''
SELECT substr(timestamp, 1, 10) AS date, COUNT(*) AS count
SELECT substr(timestamp, 12, 2) AS bucket, COUNT(DISTINCT device_id) AS count
FROM file_access_log
WHERE timestamp >= ? {filetype_filter_sql}
GROUP BY date
ORDER BY date
GROUP BY bucket
ORDER BY bucket
'''
elif timeframe in ('7days', '30days'):
# Group by day: substr(timestamp, 1, 10) -> YYYY-MM-DD
query = f'''
SELECT substr(timestamp, 1, 10) AS bucket, COUNT(DISTINCT device_id) AS count
FROM file_access_log
WHERE timestamp >= ? {filetype_filter_sql}
GROUP BY bucket
ORDER BY bucket
'''
elif timeframe == '365days':
# Group by month: substr(timestamp, 1, 7) -> YYYY-MM
query = f'''
SELECT substr(timestamp, 1, 7) AS bucket, COUNT(DISTINCT device_id) AS count
FROM file_access_log
WHERE timestamp >= ? {filetype_filter_sql}
GROUP BY bucket
ORDER BY bucket
'''
else:
# Default: group by day
query = f'''
SELECT substr(timestamp, 1, 10) AS bucket, COUNT(DISTINCT device_id) AS count
FROM file_access_log
WHERE timestamp >= ? {filetype_filter_sql}
GROUP BY bucket
ORDER BY bucket
'''
with log_db:
cursor = log_db.execute(query, params_for_filter)
daily_rows = cursor.fetchall()
daily_access_data = [
dict(date=r[0], count=r[1]) for r in daily_rows
distinct_device_data_rows = cursor.fetchall()
distinct_device_data = [
dict(bucket=r[0], count=r[1]) for r in distinct_device_data_rows
]
# 3. Timeframe-based aggregation
@ -322,7 +351,7 @@ def dashboard():
"dashboard.html",
timeframe=timeframe,
rows=rows,
daily_access_data=daily_access_data,
distinct_device_data=distinct_device_data,
user_agent_data=user_agent_data,
folder_data=folder_data,
location_data=location_data,

View File

@ -70,8 +70,8 @@
<div class="col-md-6 mb-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">Downloads</h5>
<canvas id="accessTrendChart"></canvas>
<h5 class="card-title">eindeutige Nutzer nach Zeit</h5>
<canvas id="distinctDeviceChart"></canvas>
</div>
</div>
</div>
@ -80,7 +80,7 @@
<div class="card">
<div class="card-body">
<h5 class="card-title">Downloads nach Zeit</h5>
<canvas id="timeframeChart"></canvas>
<canvas id="downloadTimeframeChart"></canvas>
</div>
</div>
</div>
@ -170,7 +170,7 @@
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
// Data passed from the backend as JSON
const dailyAccessData = {{ daily_access_data|tojson }};
const distinctDeviceData = {{ distinct_device_data|tojson }};
// Replace topFilesData usage with timeframeData for this chart
const timeframeData = {{ timeframe_data|tojson }};
const userAgentData = {{ user_agent_data|tojson }};
@ -206,31 +206,36 @@
}
});
// Access Trend Chart - Line Chart
const ctxTrend = document.getElementById('accessTrendChart').getContext('2d');
new Chart(ctxTrend, {
type: 'line',
// Distinct Device Chart
const ctxDistinctDevice = document.getElementById('distinctDeviceChart').getContext('2d');
new Chart(ctxDistinctDevice, {
type: 'bar',
data: {
labels: dailyAccessData.map(item => item.date),
labels: shiftedLabels,
datasets: [{
label: 'Download Count',
data: dailyAccessData.map(item => item.count),
borderWidth: 2,
fill: true
label: 'Device Count',
data: distinctDeviceData.map(item => item.count),
borderWidth: 2
}]
},
options: {
responsive: true,
plugins: { legend: { position: 'top' } },
plugins: { legend: { display: false } },
scales: {
x: { title: { display: true, text: 'Datum' } },
y: { title: { display: true, text: 'Download Count' } }
x: { title: { display: true, text: 'Time Range' } },
y: {
title: { display: true, text: 'Device Count' },
beginAtZero: true,
ticks: {
stepSize: 1
}
}
}
}
});
// Timeframe Breakdown Chart - Bar Chart (for "today" timeframe)
const ctxTimeframe = document.getElementById('timeframeChart').getContext('2d');
const ctxTimeframe = document.getElementById('downloadTimeframeChart').getContext('2d');
new Chart(ctxTimeframe, {
type: 'bar',
data: {
@ -238,15 +243,21 @@
datasets: [{
label: 'Download Count',
data: timeframeData.map(item => item.count),
borderWidth: 1
borderWidth: 2
}]
},
options: {
responsive: true,
plugins: { legend: { display: false } },
scales: {
x: { title: { display: true, text: 'Local Time Range' } },
y: { title: { display: true, text: 'Download Count' } }
x: { title: { display: true, text: 'Time Range' } },
y: {
title: { display: true, text: 'Download Count' },
beginAtZero: true,
ticks: {
stepSize: 1
}
}
}
}
});