Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/lib/flood-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@ export function proxyFetch (url, options = {}) {
* Fetch station details by RLOI ID (Check for Flooding ID)
*/
export async function getStation (stationId) {
const url = `${API_BASE_URL}/id/stations?RLOIid=${stationId}`
try {
const response = await proxyFetch(`${API_BASE_URL}/id/stations?RLOIid=${stationId}`)
console.log(`Fetching station from: ${url}`)
const response = await proxyFetch(url)
console.log(`Station API response status: ${response.status} ${response.statusText}`)

if (!response.ok) {
throw new Error(`Failed to fetch station: ${response.statusText}`)
throw new Error(`Failed to fetch station: ${response.status} ${response.statusText}`)
}
const data = await response.json()
// The API returns an array of stations in items
if (data.items && data.items.length > 0) {
console.log(`Station data retrieved successfully for ${stationId}`)
return data.items[0]
}
console.log(`No station items found for ${stationId}`)
return null
} catch (error) {
console.error('Error fetching station:', error)
console.error(`Error fetching station from ${url}:`, error.message, error.cause)
return null
}
}
Expand All @@ -49,14 +55,18 @@ export async function getStation (stationId) {
* Fetch station readings/measurements
*/
export async function getStationReadings (stationId, since = null) {
const stationUrl = `${API_BASE_URL}/id/stations?RLOIid=${stationId}`
try {
// First get the station to find its measures
const stationResponse = await proxyFetch(`${API_BASE_URL}/id/stations?RLOIid=${stationId}`)
console.log(`Fetching station for readings from: ${stationUrl}`)
const stationResponse = await proxyFetch(stationUrl)
if (!stationResponse.ok) {
console.error(`Station fetch for readings failed: ${stationResponse.status} ${stationResponse.statusText}`)
throw new Error('Station not found')
}
const stationData = await stationResponse.json()
if (!stationData.items || stationData.items.length === 0) {
console.log(`No station items found for readings: ${stationId}`)
return []
}

Expand Down
2 changes: 2 additions & 0 deletions src/plugins/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Inert from '@hapi/inert'
import { health } from '../routes/health.js'
import { healthCheck } from '../routes/health-check.js'
import { index } from '../routes/index.js'
import { station } from '../routes/station.js'
import { serveStaticFiles } from '../common/helpers/serve-static-files.js'
Expand All @@ -10,6 +11,7 @@ export const router = {
async register (server) {
await server.register([Inert])
await server.route(health)
await server.route(healthCheck)
await server.route(index)
await server.route(station)
await server.register([serveStaticFiles])
Expand Down
47 changes: 47 additions & 0 deletions src/routes/health-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Extended health check endpoint that also tests external API connectivity
*/
export const healthCheck = {
method: 'GET',
path: '/health/connectivity',
handler: async function (request, h) {
const results = {
service: 'ok',
timestamp: new Date().toISOString(),
externalApis: {}
}

// Test Environment Agency API connectivity
try {
const testUrl = 'https://environment.data.gov.uk/flood-monitoring/id/stations?RLOIid=8085'
request.logger.info(`Testing connectivity to: ${testUrl}`)

const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 5000)

const response = await fetch(testUrl, { signal: controller.signal })
clearTimeout(timeoutId)

results.externalApis.environmentAgency = {
url: testUrl,
status: response.status,
statusText: response.statusText,
reachable: response.ok
}

if (response.ok) {
const data = await response.json()
results.externalApis.environmentAgency.itemsCount = data.items?.length || 0
}
} catch (error) {
request.logger.error('Environment Agency API connectivity test failed:', error)
results.externalApis.environmentAgency = {
reachable: false,
error: error.message,
errorType: error.name
}
}

return h.response(results).code(200)
}
}
3 changes: 3 additions & 0 deletions src/routes/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ export const station = {
const { dataType, stationType, stationId = '8085' } = request.query

try {
request.logger.info(`Fetching station data for ID: ${stationId}`)

// Fetch real data from Environment Agency API
const [stationData, readings] = await Promise.all([
getStation(stationId),
getStationReadings(stationId)
])

if (!stationData) {
request.logger.warn(`Station not found or API call failed for ID: ${stationId}`)
return h.view('error.njk', {
error: 'Station not found',
message: `Could not find station with ID: ${stationId}`
Expand Down