fix(fitness): exclude 0-kcal days from calorie balance stats
Average calorie balance was comparing logged-day intake against all-day expenditure, producing a spurious deficit on weeks with untracked days. Now restrict both sides to days with non-zero logged intake so the subtraction compares apples to apples.
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "homepage",
|
"name": "homepage",
|
||||||
"version": "1.37.4",
|
"version": "1.37.5",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -128,9 +128,10 @@ export const GET: RequestHandler = async ({ locals }) => {
|
|||||||
workoutKcalByDate.set(key, (workoutKcalByDate.get(key) ?? 0) + (s.kcalEstimate?.kcal ?? 0));
|
workoutKcalByDate.set(key, (workoutKcalByDate.get(key) ?? 0) + (s.kcalEstimate?.kcal ?? 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7-day averages (only days with logged entries)
|
// 7-day averages — only over days with non-zero logged intake, so untracked
|
||||||
|
// days don't skew the balance toward an artificial deficit.
|
||||||
const sevenDayStr = sevenDaysAgo.toISOString().slice(0, 10);
|
const sevenDayStr = sevenDaysAgo.toISOString().slice(0, 10);
|
||||||
const recent7 = dailyTotals.filter(d => d.date >= sevenDayStr);
|
const recent7 = dailyTotals.filter(d => d.date >= sevenDayStr && d.calories > 0);
|
||||||
|
|
||||||
let avgProteinPerKg: number | null = null;
|
let avgProteinPerKg: number | null = null;
|
||||||
let avgCalorieBalance: number | null = null;
|
let avgCalorieBalance: number | null = null;
|
||||||
@@ -147,18 +148,16 @@ export const GET: RequestHandler = async ({ locals }) => {
|
|||||||
avgProteinPerKg = Math.round((avgProtein / trendWeight) * 100) / 100;
|
avgProteinPerKg = Math.round((avgProtein / trendWeight) * 100) / 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calorie balance: intake minus estimated expenditure (per-day TDEE + workout kcal)
|
// Calorie balance: intake minus expenditure, averaged over the same
|
||||||
|
// logged days (not all 7 calendar days) so the two sides compare apples
|
||||||
|
// to apples.
|
||||||
if (canComputeTdee) {
|
if (canComputeTdee) {
|
||||||
// Build all 7 calendar days and compute expenditure for each
|
|
||||||
let totalExpenditure = 0;
|
let totalExpenditure = 0;
|
||||||
let expenditureDays = 0;
|
let expenditureDays = 0;
|
||||||
for (let i = 1; i <= 7; i++) {
|
for (const d of recent7) {
|
||||||
const d = new Date(todayStart);
|
const dayTdee = getDailyTdee(d.date);
|
||||||
d.setUTCDate(d.getUTCDate() - i);
|
|
||||||
const dateStr = d.toISOString().slice(0, 10);
|
|
||||||
const dayTdee = getDailyTdee(dateStr);
|
|
||||||
if (dayTdee != null) {
|
if (dayTdee != null) {
|
||||||
totalExpenditure += dayTdee + (workoutKcalByDate.get(dateStr) ?? 0);
|
totalExpenditure += dayTdee + (workoutKcalByDate.get(d.date) ?? 0);
|
||||||
expenditureDays++;
|
expenditureDays++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user