pie chart next to logbook

This commit is contained in:
2026-02-10 20:13:09 +01:00
parent 0dd2bd3625
commit 5004726715
3 changed files with 221 additions and 85 deletions

69
app.py
View File

@@ -406,10 +406,75 @@ def task_detail(task_id):
@app.route('/logbook')
def logbook():
if not is_logged_in(): return redirect(url_for('login'))
user_id = get_user_id()
# --- Statistics Logic ---
time_range = request.args.get('range', '24h')
now = datetime.now()
if time_range == '24h':
start_date = now - timedelta(hours=24)
elif time_range == 'week':
start_date = now - timedelta(days=7)
elif time_range == 'month':
start_date = now - timedelta(days=30)
elif time_range == 'year':
start_date = now - timedelta(days=365)
else:
start_date = now - timedelta(hours=24)
# Calculate sums per activity
pipeline_stats = [
{'$match': {
'user_id': user_id,
'end_time': {'$ne': None},
'start_time': {'$gte': start_date}
}},
{'$project': {
'activity_id': 1,
'duration': {'$subtract': ['$end_time', '$start_time']}
}},
{'$group': {
'_id': '$activity_id',
'total_ms': {'$sum': '$duration'}
}}
]
stats_raw = list(db.time_entries.aggregate(pipeline_stats))
chart_data = {
'labels': [],
'chart_values': [], # Renamed from 'values' to avoid method conflict
'colors': []
}
total_ms_all = 0
for s in stats_raw:
# Sum total for all activities
total_ms_all += s['total_ms']
act = db.activities.find_one({'_id': s['_id']})
if act:
chart_data['labels'].append(act['name'])
chart_data['colors'].append(act.get('color', '#ccc'))
# Convert milliseconds to hours
hours = s['total_ms'] / (1000 * 60 * 60)
chart_data['chart_values'].append(round(hours, 2))
# Format total time display
if total_ms_all > 0:
total_hours_val = total_ms_all / (1000 * 60 * 60)
# If less than 1 hour, show minutes? Or just 0.Xh?
# Keeps consistency with pie chart to use hours, but let's make it look nice.
total_time_display = f"{round(total_hours_val, 1)}h"
else:
total_time_display = "0h"
# --- Existing Logbook Logic ---
# Agrregation to join activities and tasks
pipeline = [
{'$match': {'user_id': get_user_id(), 'end_time': {'$ne': None}}},
{'$match': {'user_id': user_id, 'end_time': {'$ne': None}}},
{'$sort': {'start_time': -1}},
{'$lookup': {
'from': 'activities',
@@ -436,7 +501,7 @@ def logbook():
duration = entry['end_time'] - entry['start_time']
entry['duration_str'] = str(duration).split('.')[0] # HH:MM:SS
return render_template('logbook.html', log=log)
return render_template('logbook.html', log=log, chart_data=chart_data, current_range=time_range, total_time_display=total_time_display)
@app.route('/logbook/<entry_id>', methods=['GET', 'POST'])
def log_entry_detail(entry_id):