perf: connection pooling, query consolidation, inline chart data, batch milestones

This commit is contained in:
Peter Stockings
2026-02-24 21:41:55 +11:00
parent 56168a182b
commit c76b4cd6fc
8 changed files with 219 additions and 88 deletions

View File

@@ -1,6 +1,8 @@
from datetime import timezone
from flask import Blueprint, render_template
from app.auth import login_required, get_current_user
from app.db import query, query_one
from app.config import SYDNEY_TZ
from app.utils import calculate_streak, calculate_weight_change
bp = Blueprint("dashboard", __name__)
@@ -10,38 +12,53 @@ bp = Blueprint("dashboard", __name__)
@login_required
def index():
user = get_current_user()
uid = user["id"]
# Get latest check-in
latest = query_one(
"SELECT * FROM checkins WHERE user_id = %s ORDER BY checked_in_at DESC LIMIT 1",
(user["id"],),
)
# Get check-in count
stats = query_one(
"SELECT COUNT(*) as total_checkins FROM checkins WHERE user_id = %s",
(user["id"],),
)
# Calculate weight change
first_checkin = query_one(
"SELECT weight_kg FROM checkins WHERE user_id = %s ORDER BY checked_in_at ASC LIMIT 1",
(user["id"],),
)
# --- Single query: latest, first, count via window functions ----------
summary = query_one("""
SELECT
total,
first_weight,
latest_weight,
latest_bmi,
latest_at
FROM (
SELECT
COUNT(*) OVER () AS total,
FIRST_VALUE(weight_kg) OVER (ORDER BY checked_in_at ASC) AS first_weight,
FIRST_VALUE(weight_kg) OVER (ORDER BY checked_in_at DESC) AS latest_weight,
FIRST_VALUE(bmi) OVER (ORDER BY checked_in_at DESC) AS latest_bmi,
FIRST_VALUE(checked_in_at) OVER (ORDER BY checked_in_at DESC) AS latest_at,
ROW_NUMBER() OVER (ORDER BY checked_in_at DESC) AS rn
FROM checkins
WHERE user_id = %s
) sub
WHERE rn = 1
""", (uid,))
# Build lightweight "latest" dict for the template
latest = None
weight_change = None
weight_change_pct = None
if latest and first_checkin:
total_checkins = 0
if summary:
total_checkins = summary["total"]
latest = {
"weight_kg": summary["latest_weight"],
"bmi": summary["latest_bmi"],
"checked_in_at": summary["latest_at"],
}
kg_lost, pct_lost = calculate_weight_change(
first_checkin["weight_kg"], latest["weight_kg"]
summary["first_weight"], summary["latest_weight"]
)
weight_change = round(-kg_lost, 1) # negative = gained, positive = lost
weight_change = round(-kg_lost, 1)
weight_change_pct = round(-pct_lost, 1)
# Recent check-ins (last 5)
recent_checkins = query(
"SELECT * FROM checkins WHERE user_id = %s ORDER BY checked_in_at DESC LIMIT 5",
(user["id"],),
(uid,),
)
# Activity feed (recent check-ins from all users)
@@ -52,26 +69,57 @@ def index():
WHERE u.is_private = FALSE OR u.id = %s
ORDER BY c.checked_in_at DESC
LIMIT 10
""", (user["id"],))
""", (uid,))
# Milestones
milestones = query(
"SELECT * FROM milestones WHERE user_id = %s ORDER BY achieved_at DESC",
(user["id"],),
(uid,),
)
# Streak
streak = calculate_streak(user["id"])
streak = calculate_streak(uid)
# --- Pre-compute chart data (eliminates 2 client-side fetches) --------
chart_checkins = query(
"""SELECT weight_kg, bmi, checked_in_at
FROM checkins WHERE user_id = %s
ORDER BY checked_in_at ASC""",
(uid,),
)
chart_labels = []
chart_weights = []
weekly_labels = []
weekly_changes = []
for i, c in enumerate(chart_checkins):
dt = c["checked_in_at"]
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
label = dt.astimezone(SYDNEY_TZ).strftime("%d %b")
chart_labels.append(label)
chart_weights.append(float(c["weight_kg"]))
if i > 0:
prev_w = float(chart_checkins[i - 1]["weight_kg"])
curr_w = float(c["weight_kg"])
weekly_labels.append(label)
weekly_changes.append(round(curr_w - prev_w, 1))
return render_template(
"dashboard.html",
user=user,
latest=latest,
stats=stats,
stats={"total_checkins": total_checkins},
weight_change=weight_change,
weight_change_pct=weight_change_pct,
recent_checkins=recent_checkins,
activity=activity,
milestones=milestones,
streak=streak,
chart_labels=chart_labels,
chart_weights=chart_weights,
weekly_labels=weekly_labels,
weekly_changes=weekly_changes,
)