Skip to content

Commit

Permalink
get all data within past 7 days of pp meters (unique values) works
Browse files Browse the repository at this point in the history
  • Loading branch information
solderq35 committed May 7, 2024
1 parent 5a0b2b3 commit 16df611
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions backend/dependencies/nodejs/models/pacific_power_recent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@ class PacificPowerRecent {
await DB.connect()
const currentDate = new Date()
const date = new Date(currentDate)
date.setDate(date.getDate() - 7)
date.setUTCHours(23)
date.setUTCMinutes(59)
date.setUTCSeconds(59)
date.setUTCHours(0)
date.setUTCMinutes(0)
date.setUTCSeconds(0)
date.setUTCMilliseconds(0)
const timestamp = Math.floor(date.getTime() / 1000) // Convert milliseconds to seconds
const endTimestamp = Math.floor(date.getTime() / 1000) // End of today in seconds

date.setDate(date.getDate() - 7)
const startTimestamp = Math.floor(date.getTime() / 1000) // 7 days ago in seconds

return DB.query(
`SELECT time, time_seconds, pacific_power_meter_id
FROM (
SELECT time, time_seconds, pacific_power_meter_id,
ROW_NUMBER() OVER (PARTITION BY pacific_power_meter_id ORDER BY time_seconds DESC) AS rn
FROM pacific_power_data
WHERE time_seconds > ?
) AS ranked_data
WHERE rn = 1
ORDER BY time_seconds ASC;`,
[timestamp, timestamp]
`SELECT MAX(time) as time, MAX(time_seconds) as time_seconds, pacific_power_meter_id
FROM pacific_power_data
WHERE time_seconds >= ? AND time_seconds <= ?
GROUP BY pacific_power_meter_id, DATE(FROM_UNIXTIME(time_seconds))
ORDER BY pacific_power_meter_id, time_seconds ASC;`,
[startTimestamp, endTimestamp]
)
}
}
Expand Down

0 comments on commit 16df611

Please sign in to comment.