fixed bug with sleeping overview barchart

This commit is contained in:
Brian Bjarke Jensen
2025-11-11 13:58:04 +01:00
parent bb27d4437f
commit 954e5a9f6f
+11 -16
View File
@@ -536,21 +536,13 @@
return `${hours}h ${mins}m`;
}
function prepareBarChartData(sleepSessions) {
function prepareBarChartData(sleepSessions, timeRange) {
const now = new Date();
const labels = [];
const data = [];
// Filter by selected child if applicable
let filteredSessions = sleepSessions;
if (selectedChildId !== null) {
filteredSessions = sleepSessions.filter(
(session) => session.child_id === selectedChildId,
);
}
// If 24 hours selected, show 3-hour aggregates
if (currentDaysRange === 1) {
if (timeRange === 1) {
// Create 8 three-hour buckets
for (let i = 7; i >= 0; i--) {
const periodStart = new Date(now);
@@ -570,14 +562,14 @@
labels.push(`${startLabel}-${endLabel}`);
// Count sleep sessions for this 3-hour period
const count = filteredSessions.filter((session) => {
const count = sleepSessions.filter((session) => {
const startDate = new Date(session.start_time);
return startDate >= periodStart && startDate < periodEnd;
}).length;
data.push(count);
}
} else if (currentDaysRange === 3) {
} else if (timeRange === 3) {
// Show 8-hour aggregates for 3 days (9 buckets total)
for (let i = 8; i >= 0; i--) {
const periodStart = new Date(now);
@@ -603,7 +595,7 @@
labels.push(periodLabel);
// Count sleep sessions for this 8-hour period
const count = filteredSessions.filter((session) => {
const count = sleepSessions.filter((session) => {
const startDate = new Date(session.start_time);
return startDate >= periodStart && startDate < periodEnd;
}).length;
@@ -611,8 +603,8 @@
data.push(count);
}
} else {
// Create days based on currentDaysRange (including today)
for (let i = currentDaysRange - 1; i >= 0; i--) {
// Create days based on timeRange (including today)
for (let i = timeRange - 1; i >= 0; i--) {
const date = new Date(now);
date.setDate(date.getDate() - i);
date.setHours(0, 0, 0, 0);
@@ -627,7 +619,7 @@
const nextDay = new Date(date);
nextDay.setDate(nextDay.getDate() + 1);
const count = filteredSessions.filter((session) => {
const count = sleepSessions.filter((session) => {
const startDate = new Date(session.start_time);
return startDate >= date && startDate < nextDay;
}).length;
@@ -636,6 +628,9 @@
}
}
return { labels, data };
}
function prepareDurationDistributionData(sleeps) {
const ranges = {
"0-30m": 0,