diff --git a/src/baby_monitor/static/index.html b/src/baby_monitor/static/index.html
index c1f974b..5141436 100644
--- a/src/baby_monitor/static/index.html
+++ b/src/baby_monitor/static/index.html
@@ -384,8 +384,8 @@
activeFeeding = await activeResponse.json();
}
- // Fetch all feedings to get the last one
- const feedingsResponse = await fetch("/api/feedings", {
+ // Fetch the 10 most recent feedings to calculate the most recent session
+ const feedingsResponse = await fetch("/api/feedings?limit=10", {
headers: {
Authorization: `Bearer ${token}`,
},
@@ -394,8 +394,66 @@
if (feedingsResponse.ok) {
const feedings = await feedingsResponse.json();
if (feedings.length > 0) {
- // Feedings are ordered by start_time desc, so first is most recent
- lastFeeding = feedings[0];
+ // Calculate the most recent feeding session
+ let sessionStart = feedings[0].start_time;
+ let sessionEnd = feedings[0].end_time || feedings[0].start_time;
+ let sessionChildId = feedings[0].child_id;
+ let sessionEntries = [feedings[0]];
+
+ for (let i = 1; i < feedings.length; i++) {
+ const entry = feedings[i];
+ // Only group entries for the same child (ignore feeding type)
+ if (entry.child_id !== sessionChildId) {
+ break;
+ }
+ // If the gap between this entry's end_time and current sessionStart is more than 30 min, stop
+ const prevEnd = entry.end_time || entry.start_time;
+ const sessionStartDate = new Date(sessionStart);
+ const prevEndDate = new Date(prevEnd);
+ const diffMins = Math.abs((sessionStartDate - prevEndDate) / 60000);
+ if (diffMins > 30) {
+ break;
+ }
+ // Extend session to include this entry
+ sessionStart = entry.start_time;
+ sessionEntries.push(entry);
+ }
+
+ // Calculate accumulated feeding time for all events in the session
+ let accumulatedMinutes = 0;
+ sessionEntries.forEach(entry => {
+ if (entry.end_time && entry.start_time) {
+ const start = new Date(entry.start_time);
+ const end = new Date(entry.end_time);
+ const diffMs = end - start;
+ if (diffMs > 0) {
+ accumulatedMinutes += Math.floor(diffMs / 60000);
+ }
+ }
+ });
+
+ // Build a string showing all feeding types used during the session
+ const feedingTypesSet = new Set();
+ sessionEntries.forEach(entry => {
+ if (entry.feeding_type) {
+ feedingTypesSet.add(entry.feeding_type);
+ }
+ });
+ const feedingTypesString = Array.from(feedingTypesSet)
+ .map(formatFeedingType)
+ .join(', ');
+
+ // Compose a session object
+ lastFeeding = {
+ child_id: sessionChildId,
+ feeding_type: feedings[0].feeding_type,
+ start_time: sessionStart,
+ end_time: sessionEnd,
+ entries: sessionEntries,
+ child_name: feedings[0].child_name,
+ accumulated_minutes: accumulatedMinutes,
+ feeding_types_string: feedingTypesString,
+ };
}
}
@@ -570,11 +628,13 @@
? `