Skip to content

Commit

Permalink
FSR-1159 | refactor. sonar issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash committed Mar 21, 2024
1 parent a1b3688 commit 2c1f209
Showing 1 changed file with 41 additions and 24 deletions.
65 changes: 41 additions & 24 deletions lib/models/rloi.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,39 @@ function removePostfix (name) {
}
}

async function fetchStation (s3, bucket, key) {
return await s3.getObject({
Bucket: bucket,
Key: key
})
}

async function fetchSlsTelemetryStation (client, item) {
// Convert ngr (national grid reference eg SK078993) to bng (British national grid eg 407800 399300)
const point = item.$.ngr && ngrToBng(item.$.ngr)

const telemetryStation = [
item.$.stationReference,
item.$.region,
removePostfix(item.$.stationName),
item.$.ngr,
point?.easting,
point?.northing
]

await client.query('slsTelemetryStation', telemetryStation)
}

function setValueItem (index, res, setOfValues) {
return {
telemetry_value_parent_id: res.rows[0].telemetry_value_parent_id,
value: parseFloat(setOfValues.Value[index]._),
processed_value: parseFloat(setOfValues.Value[index]._),
value_timestamp: (new Date(`${setOfValues.Value[index].$.date}T${setOfValues.Value[index].$.time}Z`)).toJSON(),
error: false
}
}

module.exports = {
async save (value, bucket, key, client, s3) {
let processed = 0
Expand All @@ -32,6 +65,7 @@ module.exports = {
if (valuesCount === 0) {
return
}

for (const item of value.EATimeSeriesDataExchangeFormat.Station) {
if (item.SetofValues) {
// Update region to match station region as telemetry file region slightly differs,
Expand All @@ -41,12 +75,10 @@ module.exports = {

await directly(3, item.SetofValues.map(setOfValues => async () => {
let station

try {
station = await s3.getObject({
Bucket: bucket,
Key: `rloi/${item.$.region}/${item.$.stationReference}/station.json`
})
station = JSON.parse(station.Body)
const result = await fetchStation(s3, bucket, `rloi/${item.$.region}/${item.$.stationReference}/station.json`)
station = JSON.parse(result.Body)
} catch (err) {
// the console log is commented out so as not to spam the cloudwatch lambda
// logging, as the s3.getObject throws an error when it can't find the object, and there
Expand All @@ -66,18 +98,8 @@ module.exports = {
Post_Process: 'n',
Station_Type: 'R'
}
// Convert ngr (national grid reference eg SK078993) to bng (British national grid eg 407800 399300)
const point = item.$.ngr && ngrToBng(item.$.ngr)

const telemetryStation = [
item.$.stationReference,
item.$.region,
removePostfix(item.$.stationName),
item.$.ngr,
point && point.easting,
point && point.northing
]
await client.query('slsTelemetryStation', telemetryStation)

await fetchSlsTelemetryStation(client, item)
}

// Store parent details in sls_telemetry_value_parent
Expand Down Expand Up @@ -106,13 +128,8 @@ module.exports = {
// console.log(`Loaded parent: ${station.RLOI_ID} | ${setOfValues.$.parameter} | ${setOfValues.$.qualifier}`)

for (let i = 0; i < setOfValues.Value.length; i++) {
values[i] = {
telemetry_value_parent_id: res.rows[0].telemetry_value_parent_id,
value: parseFloat(setOfValues.Value[i]._),
processed_value: parseFloat(setOfValues.Value[i]._),
value_timestamp: (new Date(`${setOfValues.Value[i].$.date}T${setOfValues.Value[i].$.time}Z`)).toJSON(),
error: false
}
values[i] = setValueItem(i, res, setOfValues)

// Process values if they're Water Level
if (setOfValues.$.parameter === 'Water Level') {
// Subtract value if post process required
Expand Down

0 comments on commit 2c1f209

Please sign in to comment.