Skip to content

Commit

Permalink
FSR-823 | FFOI check using DTS table (#722)
Browse files Browse the repository at this point in the history
* https://eaflood.atlassian.net/browse/FSR-823

* swap forecast check to display time series table check

* updated and fixed unit tests

* fixed and updated failing unit tests

* fix failing unit tests

* update variable name for forecast flag in station-csv.js

* Refactor station-csv.js to improve code readability and maintainability

* chore: Add fakeNonForecastFlag.json test data and update fakeForecastFlag function

* Refactor station-csv.js to improve code readability and maintainability

---------

Co-authored-by: nikiwycherley <[email protected]>
  • Loading branch information
nikiwycherley and nikiwycherley committed Aug 19, 2024
1 parent 438b845 commit 15eaa7f
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 174 deletions.
6 changes: 3 additions & 3 deletions server/models/views/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ViewModel {
this.station.riverNavigation = river
this.id = station.id
this.river = river
const isForecast = forecast ? forecast.forecastFlag.display_time_series : false

this.station.trend = river.trend

Expand Down Expand Up @@ -364,9 +365,8 @@ class ViewModel {
// Forecast Data Calculations

let forecastData
if (forecast) {
const { thresholds } = forecast
this.isFfoi = thresholds.length > 0
if (isForecast) {
this.isFfoi = isForecast
if (this.isFfoi) {
forecastData = new Forecast(forecast, this.station.isCoastal, this.station.recentValue)
this.isForecast = forecastData.hasForecastData
Expand Down
6 changes: 3 additions & 3 deletions server/routes/station-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ module.exports = {

const stationName = station.external_name.replace(/[^a-zA-Z0-9]+/g, '-')

const [rawTelemetry, thresholds] = await Promise.all([
const [rawTelemetry, forecastFlag] = await Promise.all([
floodService.getStationTelemetry(id, direction),
floodService.getStationForecastThresholds(id)
floodService.getForecastFlag(id, direction)
])

const telemetry = rawTelemetry.map(item => ({
Expand All @@ -42,7 +42,7 @@ module.exports = {
}))

// Forecast station
const includeForecast = !!thresholds.length
const includeForecast = forecastFlag.display_time_series
const truncateDateHoursToAdd = 36

if (includeForecast && telemetry.length) {
Expand Down
10 changes: 6 additions & 4 deletions server/routes/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ module.exports = {
// batching all the service calls together, greatly improves page performance
const [
telemetry,
thresholds,
forecastFlag,
imtdThresholds,
impacts,
warningsAlerts,
river
] = await Promise.all([
request.server.methods.flood.getStationTelemetry(id, direction),
request.server.methods.flood.getStationForecastThresholds(id),
request.server.methods.flood.getForecastFlag(id, direction),
request.server.methods.flood.getStationImtdThresholds(id, direction),
request.server.methods.flood.getImpactData(station.rloi_id),
request.server.methods.flood.getWarningsAlertsWithinStationBuffer(station.rloi_id),
Expand All @@ -60,11 +60,13 @@ module.exports = {
return h.view('station', { model })
}

const hasForecast = forecastFlag.display_time_series

// Check if it's a forecast station
if (thresholds.length && station.status !== 'Suspended') {
if (hasForecast && station.status !== 'Suspended') {
// Forecast station
const values = await request.server.methods.flood.getStationForecastData(station.wiski_id)
const forecast = { thresholds, values }
const forecast = { forecastFlag, values }
const model = new ViewModel({ station, telemetry, forecast, imtdThresholds, impacts, river, warningsAlerts })
return h.view('station', { model })
} else {
Expand Down
8 changes: 4 additions & 4 deletions server/services/flood.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ module.exports = {
return util.getJson(`${serviceUrl}/stations`)
},

getForecastFlag (id, direction) {
return util.getJson(`${serviceUrl}/forecast-station/${id}/${direction}`)
},

getStationsWithin (bbox) {
const xyCoordsPath = bbox.join('/')
return util.getJson(`${serviceUrl}/stations-within/${xyCoordsPath}`)
Expand Down Expand Up @@ -72,10 +76,6 @@ module.exports = {
return util.getJson(`${serviceUrl}/station/${id}/${direction}/telemetry`)
},

getStationForecastThresholds (id) {
return util.getJson(`${serviceUrl}/station/${id}/forecast/thresholds`)
},

getStationImtdThresholds (id, direction) {
return util.getJson(`${serviceUrl}/station/${id}/${direction}/imtd-thresholds`)
},
Expand Down
16 changes: 8 additions & 8 deletions server/services/server-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ module.exports = server => {
}
})

server.method('flood.getForecastFlag', floodServices.getForecastFlag, {
cache: {
cache: cacheType,
expiresIn: minutes(1),
generateTimeout: seconds(10)
}
})

server.method('flood.getStations', floodServices.getStations, {
cache: {
cache: cacheType,
Expand Down Expand Up @@ -124,14 +132,6 @@ module.exports = server => {
}
})

server.method('flood.getStationForecastThresholds', floodServices.getStationForecastThresholds, {
cache: {
cache: cacheType,
expiresIn: minutes(1),
generateTimeout: seconds(10)
}
})

server.method('flood.getStationImtdThresholds', floodServices.getStationImtdThresholds, {
cache: {
cache: cacheType,
Expand Down
6 changes: 6 additions & 0 deletions test/data/fakeNonForecastFlag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"station_display_time_series_id": "94280",
"station_id": "1001",
"direction": "u",
"display_time_series": false
}
6 changes: 6 additions & 0 deletions test/data/forecastFlag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"station_display_time_series_id": "94280",
"station_id": "8208",
"direction": "u",
"display_time_series": true
}
20 changes: 13 additions & 7 deletions test/plugins/rate-limit-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ lab.experiment('rate-limit plugin disabled test', () => {
percentile_5: '3.5',
percentile_95: '0.15',
comments: '',
status: 'Closed',
status: 'Active',
status_reason: '',
status_date: null,
coordinates: '{"type":"Point","coordinates":[-2.68044442027032,53.7529105624953]}',
Expand All @@ -83,19 +83,25 @@ lab.experiment('rate-limit plugin disabled test', () => {
}

const fakeTelemetryData = () => []

const fakeThresholdData = () => []
const fakeImpactsData = () => []
const fakeThresholdsData = () => []
const fakeWarningsAlertsData = () => []
const fakeRiverStationData = () => []
const fakeStationThresholdData = () => [0, 0]

sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
const fakeForecastFlag = () => {
return {
station_display_time_series_id: '94280',
station_id: '8208',
direction: 'u',
display_time_series: false
}
}
sandbox.stub(floodService, 'getStationTelemetry').callsFake(fakeTelemetryData)
sandbox.stub(floodService, 'getForecastFlag').callsFake(fakeForecastFlag)
sandbox.stub(floodService, 'getStationImtdThresholds').callsFake(fakeThresholdData)
sandbox.stub(floodService, 'getImpactData').callsFake(fakeImpactsData)
sandbox.stub(floodService, 'getStationForecastThresholds').callsFake(fakeThresholdsData)
sandbox.stub(floodService, 'getStationImtdThresholds').callsFake(fakeStationThresholdData)
sandbox.stub(floodService, 'getWarningsAlertsWithinStationBuffer').callsFake(fakeWarningsAlertsData)
sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
sandbox.stub(floodService, 'getRiverStationByStationId').callsFake(fakeRiverStationData)

const stationPlugin = {
Expand Down
17 changes: 13 additions & 4 deletions test/plugins/rate-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ lab.experiment('rate-limit plugin test', () => {
percentile_5: '3.5',
percentile_95: '0.15',
comments: '',
status: 'Closed',
status: 'Active',
status_reason: '',
status_date: null,
coordinates: '{"type":"Point","coordinates":[-2.68044442027032,53.7529105624953]}',
Expand All @@ -89,17 +89,26 @@ lab.experiment('rate-limit plugin test', () => {
}

const fakeTelemetryData = () => []
const fakeThresholdData = () => []

const fakeImpactsData = () => []
const fakeThresholdsData = () => []
const fakeForecastFlag = () => {
return {
station_display_time_series_id: '94280',
station_id: '8208',
direction: 'u',
display_time_series: false
}
}
const fakeWarningsAlertsData = () => []
const fakeRiverStationData = () => []

sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
sandbox.stub(floodService, 'getStationTelemetry').callsFake(fakeTelemetryData)
sandbox.stub(floodService, 'getForecastFlag').callsFake(fakeForecastFlag)
sandbox.stub(floodService, 'getStationImtdThresholds').callsFake(fakeThresholdData)
sandbox.stub(floodService, 'getImpactData').callsFake(fakeImpactsData)
sandbox.stub(floodService, 'getStationForecastThresholds').callsFake(fakeThresholdsData)
sandbox.stub(floodService, 'getWarningsAlertsWithinStationBuffer').callsFake(fakeWarningsAlertsData)
sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
sandbox.stub(floodService, 'getRiverStationByStationId').callsFake(fakeRiverStationData)

const stationPlugin = {
Expand Down
35 changes: 12 additions & 23 deletions test/routes/station-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,13 @@ lab.experiment('Routes test - station-csv', () => {
}
]

const fakeThresholdsData = () => [
{
ffoi_station_threshold_id: 133,
ffoi_station_id: 19,
fwis_code: '031WAF213',
fwis_type: 'FW ACTCON FAL',
value: 2,
fwa_name: 'River Leadon Catchment',
fwa_type: 'a',
fwa_severity: -1
}
]
const fakeForecastFlag = () => data.forecastFlag

const fakeForecastData = () => data.csvForecastData

sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
sandbox.stub(floodService, 'getStationTelemetry').callsFake(fakeTelemetryData)
sandbox.stub(floodService, 'getStationForecastThresholds').callsFake(fakeThresholdsData)
sandbox.stub(floodService, 'getForecastFlag').callsFake(fakeForecastFlag)
sandbox.stub(floodService, 'getStationForecastData').callsFake(fakeForecastData)

const response = await server.inject(options)
Expand Down Expand Up @@ -153,11 +142,11 @@ lab.experiment('Routes test - station-csv', () => {
}
]

const fakeThresholdsData = () => []
const fakeForecastFlag = () => { return { } }

sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
sandbox.stub(floodService, 'getStationTelemetry').callsFake(fakeTelemetryData)
sandbox.stub(floodService, 'getStationForecastThresholds').callsFake(fakeThresholdsData)
sandbox.stub(floodService, 'getForecastFlag').callsFake(fakeForecastFlag)

const response = await server.inject(options)
Code.expect(response.statusCode).to.equal(200)
Expand Down Expand Up @@ -226,11 +215,11 @@ lab.experiment('Routes test - station-csv', () => {
}
]

const fakeThresholdsData = () => []
const fakeForecastFlag = () => { return { } }

sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
sandbox.stub(floodService, 'getStationTelemetry').callsFake(fakeTelemetryData)
sandbox.stub(floodService, 'getStationForecastThresholds').callsFake(fakeThresholdsData)
sandbox.stub(floodService, 'getForecastFlag').callsFake(fakeForecastFlag)

const response = await server.inject(options)
Code.expect(response.statusCode).to.equal(200)
Expand Down Expand Up @@ -299,11 +288,11 @@ lab.experiment('Routes test - station-csv', () => {
}
]

const fakeThresholdsData = () => []
const fakeForecastFlag = () => { return { } }

sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
sandbox.stub(floodService, 'getStationTelemetry').callsFake(fakeTelemetryData)
sandbox.stub(floodService, 'getStationForecastThresholds').callsFake(fakeThresholdsData)
sandbox.stub(floodService, 'getForecastFlag').callsFake(fakeForecastFlag)

const response = await server.inject(options)
Code.expect(response.headers['content-disposition']).to.equal('attachment; filename=Pilling-Broadfleet-height-data.csv')
Expand Down Expand Up @@ -331,11 +320,11 @@ lab.experiment('Routes test - station-csv', () => {
}
]

const fakeThresholdsData = () => []
const fakeForecastFlag = () => { return { } }

sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
sandbox.stub(floodService, 'getStationTelemetry').callsFake(fakeTelemetryData)
sandbox.stub(floodService, 'getStationForecastThresholds').callsFake(fakeThresholdsData)
sandbox.stub(floodService, 'getForecastFlag').callsFake(fakeForecastFlag)

const response = await server.inject(options)
Code.expect(response.statusCode).to.equal(400)
Expand All @@ -361,11 +350,11 @@ lab.experiment('Routes test - station-csv', () => {
}
]

const fakeThresholdsData = () => []
const fakeForecastFlag = () => { return { } }

sandbox.stub(floodService, 'getStationById').callsFake(fakeStationData)
sandbox.stub(floodService, 'getStationTelemetry').callsFake(fakeTelemetryData)
sandbox.stub(floodService, 'getStationForecastThresholds').callsFake(fakeThresholdsData)
sandbox.stub(floodService, 'getForecastFlag').callsFake(fakeForecastFlag)

const response = await server.inject(options)
Code.expect(response.statusCode).to.equal(400)
Expand Down
Loading

0 comments on commit 15eaa7f

Please sign in to comment.