-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pacific Power Recent Data GET Endpoint (#309)
* pacific power recent data endpoint * try 7 days * only show one max timestamp per meter id in case of ties (redundant uploads) * update file names for recent * add human readable dates * get all data within past 7 days of pp meters (unique values) works * 400 code for duplicate upload * remove extra log
- Loading branch information
Showing
4 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const Response = require('/opt/nodejs/response.js') | ||
const PacificPowerRecent = require('/opt/nodejs/models/pacific_power_recent.js') | ||
|
||
// Get most recent Unix Timestamp and Pacific Power Meter ID for each Pacific Power Meter, within last 7 days | ||
exports.get = async (event, context) => { | ||
let response = new Response(event) | ||
response.body = JSON.stringify(await new PacificPowerRecent().get()) | ||
return response | ||
} |
29 changes: 29 additions & 0 deletions
29
backend/dependencies/nodejs/models/pacific_power_recent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const DB = require('/opt/nodejs/sql-access.js') | ||
|
||
class PacificPowerRecent { | ||
async get() { | ||
// Get Unix TimeStamp for 11:59:59 PM GMT of 7 days ago | ||
await DB.connect() | ||
const currentDate = new Date() | ||
const date = new Date(currentDate) | ||
date.setUTCHours(0) | ||
date.setUTCMinutes(0) | ||
date.setUTCSeconds(0) | ||
date.setUTCMilliseconds(0) | ||
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 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] | ||
) | ||
} | ||
} | ||
|
||
module.exports = PacificPowerRecent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters