Skip to content

Commit

Permalink
FSR-1159 | refactor function. sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash committed Mar 22, 2024
1 parent c45beba commit 544b64e
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions lib/models/rloi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const directly = require('directly')
// telemetry parameters to import (Water level is if rloi station exists)
const parameters = ['Rainfall']

const processedValueDecimalPlace = 3

function getValuesCount (stations) {
return stations.reduce((count, station) => count + (station.SetofValues || []).length, 0)
}
Expand All @@ -28,30 +30,42 @@ async function fetchStation (s3, bucket, key) {
})
}

async function fetchSlsTelemetryStation (client, item) {
function setSlsTelemetryStation (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 = [
return [
item.$.stationReference,
item.$.region,
removePostfix(item.$.stationName),
item.$.ngr,
point?.easting,
point?.northing
]

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

function setSlsTelemetryValueItem (index, res, setOfValues) {
return {
function setSlsTelemetryValueItem (index, res, station, setOfValues) {
const value = {
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
}

// Process values if they're Water Level
if (setOfValues.$.parameter === 'Water Level') {
// Subtract value if post process required
if (station.Post_Process.toLowerCase() === 'y' || station.Post_Process.toLowerCase() === 'yes') {
value.processed_value = station.Subtract ? parseFloat(util.toFixed(value.value - parseFloat(station.Subtract), processedValueDecimalPlace)) : value.value
}
if (!util.isNumeric(value.processed_value)) {
value.processed_value = null
value.error = true
}
}

return value
}

function setSlsTelemetryParent (key, station, item, setOfValues) {
Expand Down Expand Up @@ -80,7 +94,6 @@ module.exports = {
async save (value, bucket, key, client, s3) {
let processed = 0
const concurrence = 3
const processedValueDecimalPlace = 3

const valuesCount = getValuesCount(value.EATimeSeriesDataExchangeFormat.Station)

Expand Down Expand Up @@ -123,7 +136,9 @@ module.exports = {
Station_Type: 'R'
}

await fetchSlsTelemetryStation(client, item)
const telemetryStation = setSlsTelemetryStation(item)

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

// Store parent details in sls_telemetry_value_parent
Expand All @@ -134,19 +149,7 @@ module.exports = {
// console.log(`Loaded parent: ${station.RLOI_ID} | ${setOfValues.$.parameter} | ${setOfValues.$.qualifier}`)

for (let i = 0; i < setOfValues.Value.length; i++) {
values[i] = setSlsTelemetryValueItem(i, res, setOfValues)

// Process values if they're Water Level
if (setOfValues.$.parameter === 'Water Level') {
// Subtract value if post process required
if (station.Post_Process.toLowerCase() === 'y' || station.Post_Process.toLowerCase() === 'yes') {
values[i].processed_value = station.Subtract ? parseFloat(util.toFixed(values[i].value - parseFloat(station.Subtract), processedValueDecimalPlace)) : values[i].value
}
if (!util.isNumeric(values[i].processed_value)) {
values[i].processed_value = null
values[i].error = true
}
}
values[i] = setSlsTelemetryValueItem(i, res, station, setOfValues)
}

// Note: this previously passed a single parameter as a query built using sql-node
Expand Down

0 comments on commit 544b64e

Please sign in to comment.