fitness: limit workouts-per-week chart to 10 weeks and trim empty leading weeks

Reduce the chart window from 12 to 10 weeks and trim leading weeks
with zero workouts so the chart starts from the first week with data.
This commit is contained in:
2026-03-19 09:14:45 +01:00
parent 2fdfa2df6a
commit 620687f8f3
@@ -16,8 +16,8 @@ export const GET: RequestHandler = async ({ locals }) => {
await dbConnect(); await dbConnect();
console.timeEnd('[stats/profile] dbConnect'); console.timeEnd('[stats/profile] dbConnect');
const twelveWeeksAgo = new Date(); const tenWeeksAgo = new Date();
twelveWeeksAgo.setDate(twelveWeeksAgo.getDate() - 84); tenWeeksAgo.setDate(tenWeeksAgo.getDate() - 70);
console.time('[stats/profile] countDocuments'); console.time('[stats/profile] countDocuments');
const totalWorkouts = await WorkoutSession.countDocuments({ createdBy: user.nickname }); const totalWorkouts = await WorkoutSession.countDocuments({ createdBy: user.nickname });
@@ -28,7 +28,7 @@ export const GET: RequestHandler = async ({ locals }) => {
{ {
$match: { $match: {
createdBy: user.nickname, createdBy: user.nickname,
startTime: { $gte: twelveWeeksAgo } startTime: { $gte: tenWeeksAgo }
} }
}, },
{ {
@@ -62,18 +62,27 @@ export const GET: RequestHandler = async ({ locals }) => {
weekMap.set(`${item._id.year}-${item._id.week}`, item.count); weekMap.set(`${item._id.year}-${item._id.week}`, item.count);
} }
const workoutsChart: { labels: string[]; data: number[] } = { labels: [], data: [] }; const allLabels: string[] = [];
const allData: number[] = [];
const now = new Date(); const now = new Date();
for (let i = 11; i >= 0; i--) { for (let i = 9; i >= 0; i--) {
const d = new Date(now); const d = new Date(now);
d.setDate(d.getDate() - i * 7); d.setDate(d.getDate() - i * 7);
const year = getISOWeekYear(d); const year = getISOWeekYear(d);
const week = getISOWeek(d); const week = getISOWeek(d);
const key = `${year}-${week}`; const key = `${year}-${week}`;
workoutsChart.labels.push(`W${week}`); allLabels.push(`W${week}`);
workoutsChart.data.push(weekMap.get(key) ?? 0); allData.push(weekMap.get(key) ?? 0);
} }
// Trim leading empty weeks, but always keep from first week with data
let firstNonZero = allData.findIndex((v) => v > 0);
if (firstNonZero === -1) firstNonZero = allData.length - 1; // show at least current week
const workoutsChart = {
labels: allLabels.slice(firstNonZero),
data: allData.slice(firstNonZero)
};
// Build chart-ready weight data with SMA ± 1 std dev confidence band // Build chart-ready weight data with SMA ± 1 std dev confidence band
const weightChart: { const weightChart: {
labels: string[]; labels: string[];