Skip to content

Commit

Permalink
Pacific Power Recent Data GET Endpoint (#309)
Browse files Browse the repository at this point in the history
* 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
solderq35 authored May 8, 2024
1 parent faf5b5a commit 95f7ef3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
12 changes: 11 additions & 1 deletion backend/app/meter.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ exports.upload = async (event, context) => {
if (meter_type === 'solar') {
query_string = `INSERT INTO Solar_Meters (\`time\`, \`time_seconds\`, \`energy_change\`, \`MeterID\`, \`MeterName\`) VALUES ('${meter_data.time}', '${meter_data.time_seconds}', '${meter_data.totalYieldYesterday}', '${meter_data.meterID}', '${meter_data.meterName}');`
} else if (meter_type === 'pacific_power') {
query_string = `INSERT INTO pacific_power_data (\`time\`, \`time_seconds\`, \`accumulated_real\`, \`pacific_power_meter_id\`) VALUES ('${meter_data.time}', '${meter_data.time_seconds}', '${meter_data.usage_kwh}', '${meter_data.pp_meter_id}');`
let final_redundant_check = await DB.query(
'SELECT * FROM pacific_power_data WHERE pacific_power_meter_id = ? AND time_seconds = ?',
[meter_data.pp_meter_id, meter_data.time_seconds]
)
if (final_redundant_check.length === 0) {
query_string = `INSERT INTO pacific_power_data (\`time\`, \`time_seconds\`, \`accumulated_real\`, \`pacific_power_meter_id\`) VALUES ('${meter_data.time}', '${meter_data.time_seconds}', '${meter_data.usage_kwh}', '${meter_data.pp_meter_id}');`
} else {
response.statusCode = 400
response.body = 'redundant upload detected, skipping'
return response
}
}

try {
Expand Down
9 changes: 9 additions & 0 deletions backend/app/pacific_power_recent.js
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 backend/dependencies/nodejs/models/pacific_power_recent.js
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
15 changes: 15 additions & 0 deletions backend/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,21 @@ Resources:
Properties:
Path: /ppupload
Method: post
ppRecent:
Type: AWS::Serverless::Function
Properties:
MemorySize: 128
CodeUri: app/
Handler: pacific_power_recent.get
Layers:
- !Ref LambdaCommonLayer
- !Ref EnergyModelLayer
Events:
Building:
Type: Api
Properties:
Path: /pprecent
Method: get
EnergyModelLayer:
Type: AWS::Serverless::LayerVersion
Properties:
Expand Down

0 comments on commit 95f7ef3

Please sign in to comment.