diff --git a/lib/models/rloi.js b/lib/models/rloi.js index 98f598c..6101158 100644 --- a/lib/models/rloi.js +++ b/lib/models/rloi.js @@ -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) } @@ -28,11 +30,11 @@ 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), @@ -40,18 +42,30 @@ async function fetchSlsTelemetryStation (client, item) { 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) { @@ -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) @@ -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 @@ -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