moved totals calculation to seperate file too and implementet three out of four stats. Only 'this semster' is missing now and were done!!
This commit is contained in:
+6
-21
@@ -1,28 +1,11 @@
|
||||
from flask import Flask, jsonify, render_template
|
||||
from src.fetch_sessions import get_events, get_last_n_days
|
||||
from src.fetch_sessions import *
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
sessions = get_events()
|
||||
|
||||
def calculate_totals(sessions):
|
||||
totals = {}
|
||||
for session in sessions:
|
||||
subject = session["subject"]
|
||||
minutes = session["minutes"]
|
||||
hours = minutes / 60
|
||||
if subject in totals.keys():
|
||||
totals[subject] += hours
|
||||
else:
|
||||
totals[subject] = hours
|
||||
|
||||
return totals
|
||||
|
||||
def get_daily_minutes():
|
||||
return get_last_n_days(50)
|
||||
sessions = get_sessions()
|
||||
|
||||
# web ui
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
labels = [s["subject"] for s in sessions]
|
||||
@@ -38,10 +21,12 @@ def index():
|
||||
|
||||
@app.route("/api/dashboard")
|
||||
def api_dashboard():
|
||||
totals = calculate_totals(sessions)
|
||||
return jsonify({
|
||||
"sessions": sessions,
|
||||
"subjectTotals": calculate_totals(sessions),
|
||||
"dailyMinutes": get_daily_minutes()
|
||||
"subjectTotals": totals,
|
||||
"dailyMinutes": get_last_n_days(50, sessions),
|
||||
"stats": get_stats(sessions, totals),
|
||||
})
|
||||
|
||||
# external api
|
||||
|
||||
@@ -22,7 +22,7 @@ client = caldav.DAVClient(
|
||||
calendar = client.calendar(url=CALDAV_URL)
|
||||
|
||||
|
||||
def get_events():
|
||||
def get_sessions():
|
||||
events = calendar.events()
|
||||
events.sort(key=lambda e: e.vobject_instance.vevent.dtstart.value)
|
||||
|
||||
@@ -38,9 +38,21 @@ def get_events():
|
||||
})
|
||||
return event_list
|
||||
|
||||
def get_last_n_days(n: int):
|
||||
events = get_events()
|
||||
|
||||
def calculate_totals(sessions):
|
||||
totals = {}
|
||||
for session in sessions:
|
||||
subject = session["subject"]
|
||||
minutes = session["minutes"]
|
||||
hours = minutes / 60
|
||||
if subject in totals.keys():
|
||||
totals[subject] += hours
|
||||
else:
|
||||
totals[subject] = hours
|
||||
|
||||
return totals
|
||||
|
||||
def get_last_n_days(n: int, sessions):
|
||||
days = []
|
||||
|
||||
today = datetime.now().date()
|
||||
@@ -51,10 +63,46 @@ def get_last_n_days(n: int):
|
||||
)
|
||||
|
||||
# populate minutes field
|
||||
for event in events:
|
||||
date = event["date"]
|
||||
for session in sessions:
|
||||
date = session["date"]
|
||||
for day in days:
|
||||
if date == day["date"]:
|
||||
day["minutes"] += event["minutes"]
|
||||
day["minutes"] += session["minutes"]
|
||||
|
||||
return days
|
||||
|
||||
def get_total_month(sessions):
|
||||
total_min = 0
|
||||
for session in sessions:
|
||||
year_month = str(session["date"])[:7]
|
||||
|
||||
if year_month == datetime.now(TIME_ZONE).strftime("%Y-%m"):
|
||||
total_min += session["minutes"]
|
||||
return total_min
|
||||
|
||||
def get_worst_and_best(totals):
|
||||
worst = "None"
|
||||
worst_h = 99999999
|
||||
best = "None"
|
||||
best_h = 0
|
||||
for subject in totals.keys():
|
||||
h = totals[subject]
|
||||
if h < worst_h:
|
||||
worst = subject
|
||||
worst_h = h
|
||||
if h > best_h:
|
||||
best = subject
|
||||
best_h = h
|
||||
return {
|
||||
"worst":worst,
|
||||
"best":best
|
||||
}
|
||||
|
||||
def get_stats(sessions, totals):
|
||||
w_and_b = get_worst_and_best(totals)
|
||||
return {
|
||||
"semester":"hi",
|
||||
"month":round(get_total_month(sessions)/60, 1),
|
||||
"top_subject":w_and_b["best"],
|
||||
"worst_subject":w_and_b["worst"]
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ async function loadDashboard() {
|
||||
renderSessions(data.sessions);
|
||||
renderDailyDots(data.dailyMinutes);
|
||||
renderSubjectChart(data.subjectTotals);
|
||||
renderStats(data.stats);
|
||||
}
|
||||
|
||||
/* ---------------- render session list ---------------- */
|
||||
@@ -74,4 +75,11 @@ function renderSubjectChart(subjectTotals) {
|
||||
});
|
||||
}
|
||||
|
||||
function renderStats(stats) {
|
||||
document.getElementById("stat-semester").textContent = stats.semester + "h";
|
||||
document.getElementById("stat-month").textContent = stats.month + "h";
|
||||
document.getElementById("stat-top-subject").textContent = stats.top_subject;
|
||||
document.getElementById("stat-worst-subject").textContent = stats.worst_subject;
|
||||
}
|
||||
|
||||
loadDashboard().catch(err => console.error("Failed to load dashboard:", err));
|
||||
|
||||
@@ -26,22 +26,19 @@
|
||||
|
||||
<section class="box stat-box stat-1">
|
||||
<span class="stat-label">This semester</span>
|
||||
<span class="stat-value">260h</span>
|
||||
<span class="stat-value" id="stat-semester">...</span>
|
||||
</section>
|
||||
|
||||
<section class="box stat-box stat-2 accent">
|
||||
<span class="stat-label">Top subject</span>
|
||||
<span class="stat-value">Math</span>
|
||||
<span class="stat-value" id="stat-top-subject">...</span>
|
||||
</section>
|
||||
|
||||
<section class="box stat-box stat-3">
|
||||
<span class="stat-label">This month</span>
|
||||
<span class="stat-value">30h</span>
|
||||
<span class="stat-value" id="stat-month">...</span>
|
||||
</section>
|
||||
|
||||
<section class="box stat-box stat-4 warn">
|
||||
<span class="stat-label">Worst subject</span>
|
||||
<span class="stat-value">Databases</span>
|
||||
<span class="stat-value" id="stat-worst-subject">...</span>
|
||||
</section>
|
||||
|
||||
<section class="box daily-box">
|
||||
|
||||
Reference in New Issue
Block a user