Add quick add-reading homepage, photo take/choose picker, carry-forward estimate rate
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
68dab06b0a
commit
847fe49acd
6 changed files with 180 additions and 7 deletions
|
|
@ -167,9 +167,19 @@ export async function getTrailingDailyRate(meterId, windowDays) {
|
|||
windowStart.setDate(windowStart.getDate() - windowDays)
|
||||
const windowStartStr = windowStart.toISOString().slice(0, 10)
|
||||
|
||||
const before = await readingOnOrBefore(meterId, windowStartStr)
|
||||
let before = await readingOnOrBefore(meterId, windowStartStr)
|
||||
if (!before || before.reading_date >= latest.reading_date) {
|
||||
return { daily_rate: null, latest }
|
||||
// Nothing old enough to fill the window (readings are sparse) — fall back
|
||||
// to the average between the last two readings so the estimate carries
|
||||
// forward instead of dropping to zero until a new reading is added.
|
||||
const { rows } = await pool.query(
|
||||
`SELECT reading_value, reading_date FROM readings
|
||||
WHERE meter_id = $1 AND reading_date < $2
|
||||
ORDER BY reading_date DESC, id DESC LIMIT 1`,
|
||||
[meterId, latest.reading_date]
|
||||
)
|
||||
before = rows[0] || null
|
||||
if (!before) return { daily_rate: null, latest }
|
||||
}
|
||||
const days = daysBetween(before.reading_date, latest.reading_date)
|
||||
if (days <= 0) return { daily_rate: null, latest }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue