Skip to content

Commit

Permalink
feat: fetch battery history from 8 hours ago
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Dec 19, 2022
1 parent 2c43b8d commit 93f4b1f
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lambda/chartSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type Summary = {
bat?: Readings
temp?: Readings
solBat?: Readings
solBat8hrs?: Reading
solGain?: Readings
base: Date
}
Expand Down Expand Up @@ -131,5 +132,46 @@ export const createChartSummary = async ({
groupResult(summaries, 'temp', temp, now)
groupResult(summaries, 'solBat', solBat, now)
groupResult(summaries, 'solGain', solGain, now)

// Get battery values from 8 hours ago
const solBat8hrs = (
await Promise.all(
Object.entries(summaries)
.filter(([, summary]) => 'solBat' in summary)
.map(async ([deviceId]) => {
const res = await timestream.send(
new QueryCommand({
QueryString: [
`SELECT measure_value::double as v, time as ts`,
`FROM "${historicaldataDatabaseName}"."${historicaldataTableName}"`,
`WHERE measure_name = 'bat'`,
`AND time < date_add('hour', -8, now())`,
`AND deviceId = '${deviceId}'`,
`ORDER BY time DESC`,
`LIMIT 1`,
].join(' '),
}),
)
return {
deviceId,
historyBat: parseResult<{ v: number; ts: Date }>(res)[0],
}
}),
)
).reduce((solBat8hrs, { deviceId, historyBat }) => {
if (historyBat === undefined) return solBat8hrs
const { v, ts } = historyBat
return {
...solBat8hrs,
[deviceId]: <Reading>[
v,
Math.max(0, Math.floor((now.getTime() - ts.getTime()) / 1000)),
],
}
}, {} as Record<string, Reading>)
for (const [deviceId, reading] of Object.entries(solBat8hrs)) {
;(summaries[deviceId] as Summary).solBat8hrs = reading
}

return summaries
}

0 comments on commit 93f4b1f

Please sign in to comment.