From ef5b49882596479acd2192a67d92ba60d637fb9d Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 8 Apr 2025 13:06:27 +0530 Subject: [PATCH 1/2] changes in consumption --- src/controllers/tanksController.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index e53dc116..e3819a28 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1203,16 +1203,30 @@ exports.consumption = async (request, reply) => { let consumption; -const isSameTime = start.getTime() === end.getTime(); -const isToday = moment(start).isSame(moment(), 'day'); -if (isSameTime && !isToday) { - // Same date & time and NOT today => use only records + + const isSameTime = start.getTime() === end.getTime(); +const isEndToday = moment(end).isSame(moment(), 'day'); + +if (isSameTime || !isEndToday) { + // If single timestamp OR end is not today, only use records consumption = total_consumption_from_records; } else { - // Normal case => use full calculation + // If end is today, add real-time values consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; } + + +// const isSameTime = start.getTime() === end.getTime(); +// const isToday = moment(start).isSame(moment(), 'day'); + +// if (isSameTime && !isToday) { +// // Same date & time and NOT today => use only records +// consumption = total_consumption_from_records; +// } else { +// // Normal case => use full calculation +// consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; +// } // Add to the total consumption and capacities based on water type if (tank.typeOfWater === "bore" || tank.typeOfWater === "Bore Water") { From 0c8719a3c50683bf9f32bc13719fd98262e30f7c Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 8 Apr 2025 13:14:24 +0530 Subject: [PATCH 2/2] changes in tanks consumption --- src/controllers/tanksController.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index e3819a28..30cdffc9 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1205,18 +1205,37 @@ exports.consumption = async (request, reply) => { - const isSameTime = start.getTime() === end.getTime(); +// const isSameTime = start.getTime() === end.getTime(); +// const isEndToday = moment(end).isSame(moment(), 'day'); + +// if (isSameTime || !isEndToday) { +// // If single timestamp OR end is not today, only use records +// consumption = total_consumption_from_records; +// } else { +// // If end is today, add real-time values +// consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; +// } + +const isSameTime = start.getTime() === end.getTime(); const isEndToday = moment(end).isSame(moment(), 'day'); -if (isSameTime || !isEndToday) { - // If single timestamp OR end is not today, only use records +if (isSameTime && isEndToday) { + // Start and End are same AND it's today => only realtime tank calc + consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel; +} else if (isSameTime && !isEndToday) { + // Same time and NOT today => only record data consumption = total_consumption_from_records; -} else { - // If end is today, add real-time values +} else if (!isSameTime && isEndToday) { + // Range query ending today => combine record + realtime consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; +} else { + // Range query not ending today => only record data + consumption = total_consumption_from_records; } + + // const isSameTime = start.getTime() === end.getTime(); // const isToday = moment(start).isSame(moment(), 'day');