Skip to content

Commit

Permalink
Update getRainfallStation function to expect object rather than an array
Browse files Browse the repository at this point in the history
Update error handling on rainfall-station and rainfall-station-csv route
  • Loading branch information
Max Bladen-Clark committed Sep 26, 2023
1 parent 98a0dff commit 6fe99af
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 137 deletions.
17 changes: 10 additions & 7 deletions server/models/views/rainfall.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const util = require('../../util')

class ViewModel {
constructor (rainfallStationTelemetry, rainfallStation) {
const stationName = rainfallStation[0].station_name.replace(/(^\w|\s\w)(\S*)/g, (_, m1, m2) => m1.toUpperCase() + m2.toLowerCase())
const stationName = rainfallStation
.station_name
.replace(/(^\w|\s\w)(\S*)/g, (_, m1, m2) => m1.toUpperCase() + m2.toLowerCase())

Object.assign(this, {
stationName,
pageTitle: 'Rainfall at ' + stationName + ' gauge',
Expand All @@ -15,9 +18,9 @@ class ViewModel {
telemetry: rainfallStationTelemetry || [],
floodRiskUrl: config.floodRiskUrl,
bingMaps: config.bingKeyMaps,
stationId: rainfallStation[0].station_reference,
centroid: [rainfallStation[0].lon, rainfallStation[0].lat],
region: rainfallStation[0].region,
stationId: rainfallStation.station_reference,
centroid: [rainfallStation.lon, rainfallStation.lat],
region: rainfallStation.region,
getWarnings: 'Rainfall:Related-content:Get-warnings',
planAhead: 'Rainfall:Related-content:Plan-ahead-for-flooding',
whatToDo: 'Rainfall:Related-content:What-to-do-in-a-flood',
Expand All @@ -37,9 +40,9 @@ class ViewModel {
const rangeStartDateTime = fiveDaysAgo
const dataEndDateTime = now
const rangeEndDateTime = now
const latest1hr = util.formatValue(rainfallStation[0].one_hr_total)
const latest6hr = util.formatValue(rainfallStation[0].six_hr_total)
const latest24hr = util.formatValue(rainfallStation[0].day_total)
const latest1hr = util.formatValue(rainfallStation.one_hr_total)
const latest6hr = util.formatValue(rainfallStation.six_hr_total)
const latest24hr = util.formatValue(rainfallStation.day_total)
const valueDuration = this.telemetry[0].period === '15 min' ? 15 : 45
this.id = `${this.stationId}.${this.region}`
const latestHourDateTime = moment(latestDateTime).add(45, 'minutes').minutes(0).seconds(0).milliseconds(0).toDate()
Expand Down
19 changes: 16 additions & 3 deletions server/routes/rainfall-station-csv.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
const floodService = require('../services/flood')
const moment = require('moment-timezone')
const util = require('../../server/util')
const boom = require('@hapi/boom')
const floodService = require('../services/flood')

module.exports = {
method: 'GET',
path: '/rainfall-station-csv/{id}',
handler: async (request, h) => {
const { id } = request.params

const [rainfallStation, rainfallStationTelemetry] = await Promise.all([
const [
rainfallStation,
rainfallStationTelemetry
] = await Promise.all([
floodService.getRainfallStation(id),
floodService.getRainfallStationTelemetry(id)
])

const stationName = rainfallStation[0].station_name.replace(/(^\w|\s\w)(\S*)/g, (_, m1, m2) => m1.toUpperCase() + m2.toLowerCase())
if (!rainfallStation) {
return boom.notFound('Rainfall station not found')
}
if (!rainfallStationTelemetry) {
return boom.notFound('No rainfall station telemetry data found')
}

const stationName = rainfallStation
.station_name
.replace(/(^\w|\s\w)(\S*)/g, (_, m1, m2) => m1.toUpperCase() + m2.toLowerCase())

const valueDuration = rainfallStationTelemetry[0].period === '15 min' ? 15 : 45

Expand Down
16 changes: 10 additions & 6 deletions server/routes/rainfall-station.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ module.exports = {
handler: async (request, h) => {
const { id } = request.params

const rainfallStationTelemetry = await request.server.methods.flood.getRainfallStationTelemetry(id)
const rainfallStation = await request.server.methods.flood.getRainfallStation(id)
const [
rainfallStation,
rainfallStationTelemetry
] = await Promise.all([
request.server.methods.flood.getRainfallStation(id),
request.server.methods.flood.getRainfallStationTelemetry(id)
])

// Null rainfallStationTelemetry, but in this case service should return a 404 error so i don't think this ever gets hit, defensive programming though
if (!rainfallStation) {
return boom.notFound('Rainfall station not found')
}
if (!rainfallStationTelemetry) {
return boom.notFound('No rainfall station telemetry data found')
}
if (!rainfallStation) {
return boom.notFound('No rainfall station data found')
}

const model = new ViewModel(rainfallStationTelemetry, rainfallStation)
return h.view('rainfall-station', { model })
Expand Down
4 changes: 2 additions & 2 deletions test/data/hrRainfallStation.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[{
{
"telemetry_station_id": "735",
"station_reference": "E5732",
"region": "Anglian",
Expand All @@ -19,4 +19,4 @@
"type": "R",
"lat": 52.12387860645138,
"lon": 0.9573268999313961
}]
}
16 changes: 8 additions & 8 deletions test/models/rainfall.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ lab.experiment('Rainfall model test', () => {

lab.test('Test Rainfall viewModel puts stationName as Title Case', async () => {
const rainfallTelemetryData = data.rainfallStationTelemetry
const rainfallStationData = data.rainfallStation.filter(function (rainfallStation) { return rainfallStation.station_reference === 'E24195' })
const rainfallStationData = data.rainfallStation.find(item => item.station_reference === 'E24195')
const viewModel = new ViewModel(rainfallTelemetryData, rainfallStationData)
const Result = viewModel

Expand All @@ -30,7 +30,7 @@ lab.experiment('Rainfall model test', () => {
})
lab.test('Test Rainfall viewModel puts stationName as Title Case with multiple words in string', async () => {
const rainfallTelemetryData = data.rainfallStationTelemetry
const rainfallStationData = data.rainfallStation.filter(function (rainfallStation) { return rainfallStation.station_reference === 'N24195' })
const rainfallStationData = data.rainfallStation.find(item => item.station_reference === 'N24195')
const viewModel = new ViewModel(rainfallTelemetryData, rainfallStationData)

const Result = viewModel
Expand All @@ -41,7 +41,7 @@ lab.experiment('Rainfall model test', () => {
})
lab.test('Test Rainfall viewModel returns telemetryRainfall data', async () => {
const rainfallTelemetryData = data.rainfallStationTelemetry
const rainfallStationData = data.rainfallStation.filter(function (rainfallStation) { return rainfallStation.station_reference === 'E24195' })
const rainfallStationData = data.rainfallStation.find(item => item.station_reference === 'E24195')
const viewModel = new ViewModel(rainfallTelemetryData, rainfallStationData)

const Result = viewModel
Expand All @@ -59,7 +59,7 @@ lab.experiment('Rainfall model test', () => {
})
lab.test('Test dates are formatted correctly for the view', async () => {
const rainfallTelemetryData = data.rainfallStationTelemetry
const rainfallStationData = data.rainfallStation.filter(function (rainfallStation) { return rainfallStation.station_reference === 'E24195' })
const rainfallStationData = data.rainfallStation.find(item => item.station_reference === 'E24195')
const viewModel = new ViewModel(rainfallTelemetryData, rainfallStationData)

const Result = viewModel
Expand All @@ -69,7 +69,7 @@ lab.experiment('Rainfall model test', () => {
})
lab.test('Test station id constructed correctly', async () => {
const rainfallTelemetryData = data.rainfallStationTelemetry
const rainfallStationData = data.rainfallStation.filter(function (rainfallStation) { return rainfallStation.station_reference === 'E24195' })
const rainfallStationData = data.rainfallStation.find(item => item.station_reference === 'E24195')
const viewModel = new ViewModel(rainfallTelemetryData, rainfallStationData)

const Result = viewModel
Expand All @@ -78,7 +78,7 @@ lab.experiment('Rainfall model test', () => {
})
lab.test('Test lat/long are populated in centroid', async () => {
const rainfallTelemetryData = data.rainfallStationTelemetry
const rainfallStationData = data.rainfallStation.filter(function (rainfallStation) { return rainfallStation.station_reference === 'E24195' })
const rainfallStationData = data.rainfallStation.find(item => item.station_reference === 'E24195')
const viewModel = new ViewModel(rainfallTelemetryData, rainfallStationData)
const Result = viewModel

Expand All @@ -102,7 +102,7 @@ lab.experiment('Rainfall model test', () => {
})
lab.test('Test metadata and social details returned correctly', async () => {
const rainfallTelemetryData = data.rainfallStationTelemetry
const rainfallStationData = data.rainfallStation.filter(function (rainfallStation) { return rainfallStation.station_reference === 'E24195' })
const rainfallStationData = data.rainfallStation.find(item => item.station_reference === 'E24195')
const viewModel = new ViewModel(rainfallTelemetryData, rainfallStationData)

const Result = viewModel
Expand All @@ -113,7 +113,7 @@ lab.experiment('Rainfall model test', () => {
})
lab.test('Test getWarnings has appropriate Value', async () => {
const rainfallTelemetryData = data.rainfallStationTelemetry
const rainfallStationData = data.rainfallStation.filter((rainfallStation) => rainfallStation.station_reference === 'E24195')
const rainfallStationData = data.rainfallStation.find(item => item.station_reference === 'E24195')

const result = new ViewModel(rainfallTelemetryData, rainfallStationData)

Expand Down
Loading

0 comments on commit 6fe99af

Please sign in to comment.