Skip to content

Commit

Permalink
testing daqi implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ulysses-cognizant committed Apr 15, 2024
1 parent 1430011 commit 53ee781
Show file tree
Hide file tree
Showing 11 changed files with 1,296 additions and 715 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.3.0
v20.11.1
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG PARENT_VERSION=2.2.0-node20.3.0
ARG PARENT_VERSION=2.2.2-node20.11.1
ARG PORT=3000
ARG PORT_DEBUG=9229

Expand Down
1,684 changes: 1,050 additions & 634 deletions package-lock.json

Large diffs are not rendered by default.

55 changes: 30 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "CDP Frontend Template",
"main": "src/index.js",
"engines": {
"node": "20.3.0"
"node": ">=20.11.1"
},
"scripts": {
"build": "run-s build:frontend build:server",
Expand Down Expand Up @@ -35,63 +35,68 @@
"author": "Defra DDTS",
"license": "OGL-UK-3.0",
"dependencies": {
"@babel/runtime": "7.23.9",
"@babel/runtime": "7.24.4",
"@elastic/ecs-pino-format": "1.5.0",
"@hapi/cookie": "12.0.1",
"@hapi/hapi": "21.3.3",
"@hapi/hapi": "21.3.9",
"@hapi/inert": "7.1.0",
"@hapi/vision": "7.0.3",
"@hapi/yar": "11.0.1",
"axios": "1.6.7",
"babel-plugin-module-resolver": "5.0.0",
"convict": "6.2.4",
"date-fns": "3.3.1",
"date-fns": "3.6.0",
"dotenv": "16.4.5",
"govuk-frontend": "5.1.0",
"geolib": "3.3.4",
"govuk-frontend": "5.3.0",
"hapi-pino": "12.1.0",
"https-proxy-agent": "7.0.2",
"https-proxy-agent": "7.0.4",
"lodash": "4.17.21",
"moment": "2.30.1",
"node-fetch": "2.7.0",
"mt-osgridref": "0.1.0",
"node-fetch": "3.3.2",
"nunjucks": "3.2.4",
"pino": "8.18.0",
"undici": "6.6.2"
"pino": "8.20.0",
"proj4": "2.9.0",
"undici": "6.12.0"
},
"devDependencies": {
"@babel/cli": "7.23.9",
"@babel/core": "7.23.9",
"@babel/cli": "7.24.1",
"@babel/core": "7.24.4",
"@babel/node": "7.23.9",
"@babel/preset-env": "7.23.9",
"@babel/preset-env": "7.24.4",
"babel-jest": "29.7.0",
"babel-loader": "9.1.3",
"cheerio": "1.0.0-rc.12",
"clean-webpack-plugin": "4.0.0",
"css-loader": "6.10.0",
"eslint": "8.56.0",
"css-loader": "7.1.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-config-standard": "17.1.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-n": "16.6.2",
"eslint-plugin-jest": "28.2.0",
"eslint-plugin-jest-formatting": "3.1.0",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-prettier": "5.1.3",
"eslint-plugin-promise": "6.1.1",
"husky": "9.0.10",
"husky": "9.0.11",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"json-server": "0.17.4",
"mini-css-extract-plugin": "2.8.0",
"nodemon": "3.0.3",
"mini-css-extract-plugin": "2.8.1",
"nodemon": "3.1.0",
"npm-run-all": "4.1.5",
"pino-pretty": "10.3.1",
"pino-pretty": "11.0.0",
"prettier": "3.2.5",
"resolve-url-loader": "5.0.0",
"sass": "1.70.0",
"sass-loader": "14.1.0",
"sass": "1.74.1",
"sass-loader": "14.1.1",
"source-map-loader": "5.0.0",
"style-loader": "3.3.4",
"stylelint": "16.2.1",
"style-loader": "4.0.0",
"stylelint": "16.3.1",
"stylelint-config-gds": "2.0.0",
"webpack": "5.90.1",
"webpack-assets-manifest": "5.1.0",
"webpack": "5.91.0",
"webpack-assets-manifest": "5.2.1",
"webpack-cli": "5.1.4"
}
}
2 changes: 1 addition & 1 deletion src/server/common/components/toggletip/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</span>
</button>
<span class="defra-toggletip__info" role="status" style="">
<span class="defra-toggletip__text" id={{text}} aria-hidden="true" role="tooltip" aria-live="polite">{{tooltiptext}}</span>
<span class="defra-toggletip__text" id={{text}} aria-hidden="true" role="tooltip" aria-live="polite" data-info-text={{tooltiptext}}>{{tooltiptext}}</span>
<span class="defra-toggletip__arrow" id="x2" style=""></span>
</span>
</span>
Expand Down
23 changes: 12 additions & 11 deletions src/server/data/monitoring-sites.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prettier/prettier */
export const monitoringSites = [
{
site_name: 'Glazebury',
Expand Down Expand Up @@ -88,7 +89,7 @@ export const monitoringSites = [
{
site_name: 'Bury Whitefield Roadside',
distance: 7.6,
site_type: 'traffic-site',
site_type: 'Traffic Urban',
pollutants: [
{
type: 'nitrogen-dioxide',
Expand Down Expand Up @@ -121,27 +122,27 @@ export const monitoringSites = [
// Pollutant reference data

export const pollutantTypes = {
'nitrogen-dioxide': {
NO2: {
title: 'Nitrogen dioxide',
href: '/aqie-front-end/pollutants/nitrogen-dioxide',
low_range: '0 to 200'
},
'particulate-matter10': {
PM10: {
title: 'PM10',
href: '/aqie-front-end/pollutants/particulate-matter-10',
low_range: '0 to 50'
},
'particulate-matter2': {
PM25: {
title: 'PM2.5',
href: '/aqie-front-end/pollutants/particulate-matter-25',
low_range: '0 to 35'
},
ozone: {
O3: {
title: 'Ozone',
href: '/aqie-front-end/pollutants/ozone',
low_range: '0 to 100'
},
'sulphur-dioxide': {
SO2: {
title: 'Sulphur dioxide',
href: '/aqie-front-end/pollutants/sulphur-dioxide',
low_range: '0 to 100'
Expand All @@ -151,16 +152,16 @@ export const pollutantTypes = {
// Used to populate toggletips on monitoring sites

export const siteTypeDescriptions = {
'urban-site':
'Background Urban':
'This monitoring site is based in an urban area. An urban area is a city or a town, where there are many 2-storey buildings. For the measurement of ozone, an urban area includes parks, residential houses and commercial buildings.',
'suburban-site':
'Background Suburban':
'This monitoring site is based in a suburban location. Suburban areas are located either on their own or on the outskirts of a city or urban area. They are areas of different-sized buildings that may be close to farms, ales and woods.',
'traffic-site':
'Traffic Urban':
'This monitoring site is based near vehicle traffic. It is located close to a road, motorway or highway. This means we know that recorded pollutants come from this source.',
'rural-site':
'Background Rural':
'This monitoring site is in rural location. A rural site can be in a small settlement or area with natural ecosystems, forests or crops. The sites are more than 20 kilometres from cities and towns and more than 5 kilometres away from industrial sites, motorways and major roads.',
'industrial-site':
'This monitoring site is based in an industrial location. The pollutants are measured downwind of the industrial source and the nearest residential area.',
'background-site':
'Industrial Urban':
'This monitoring site is based in a background location. It is located to make sure pollutant measurements do not come from one specific source. The site is upwind from pollution sources in cities, industrial sources and rural areas.'
}
5 changes: 4 additions & 1 deletion src/server/home/controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { config } from 'dotenv'

config()
const homeController = {
handler: (request, h) => {
if (request.auth.isAuthenticated) {
Expand All @@ -22,7 +25,7 @@ const homeController = {
const loginController = {
handler: (request, h) => {
// test
if (request.payload.password === 'n1tr0g3n') {
if (request.payload.password === process.env.AIR_QUALITY_PASSWORD) {
request.cookieAuth.set({ password: request.payload.password })
return h.redirect('/aqie-front-end/check-local-air-quality')
} else {
Expand Down
83 changes: 63 additions & 20 deletions src/server/locations/controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import axios from 'axios'
import {
monitoringSites,
siteTypeDescriptions,
pollutantTypes
} from '../data/monitoring-sites.js'
import * as airQualityData from '../data/air-quality.js'
import { getAirQuality } from '../data/air-quality.js'
import { createLogger } from '~/src/server/common/helpers/logging/logger'
import { getNearestLocation } from './helpers/get-nearest-location.js'
import { config } from 'dotenv'

config()

const logger = createLogger()

const symbolsArr = ['%', '$', '&', '#', '!', '¬', '`']
const getLocationDataController = {
Expand Down Expand Up @@ -93,8 +99,14 @@ const getLocationDataController = {
request.yar.set('locationType', 'ni-location')
return h.redirect('/aqie-front-end/search-location')
}
const forecastsAPIurl = process.env.FORECAST_API_URL
const measurementsAPIurl = process.env.MEASUREMENTS_API_URL
const airQuality = getAirQuality(request.payload.aq)

const forecastSummaryUrl = process.env.FORECAST_SUMMARY_URL
const forecastSummaryRes = await axios.get(forecastSummaryUrl)
const forecastSummary = forecastSummaryRes.data.today
const { data: forecasts } = await axios.get(forecastsAPIurl)
const { data: measurements } = await axios.get(measurementsAPIurl)
if (locationType === 'uk-location') {
const filters = [
'LOCAL_TYPE:City',
Expand All @@ -105,14 +117,17 @@ const getLocationDataController = {
'LOCAL_TYPE:Airport'
].join('+')

const apiUrl = `https://api.os.uk/search/names/v1/find?query=${encodeURIComponent(
const osPlacesApiUrl = `${process.env.OS_PLACES_API_URL}${encodeURIComponent(
userLocation
)}&fq=${encodeURIComponent(filters)}&key=vvR3FiaNjSWCnFzSKBst23TX6efl0oL9`
)}&fq=${encodeURIComponent(filters)}&key=${process.env.OS_PLACES_API_KEY}`

const shouldCallApi = symbolsArr.some((symbol) =>
userLocation.includes(symbol)
)
const response = !shouldCallApi ? await axios.get(apiUrl) : { data: [] }

const response = !shouldCallApi
? await axios.get(osPlacesApiUrl)
: { data: [] }
//
const { results } = response?.data

if (!results || results.length === 0) {
Expand All @@ -136,9 +151,20 @@ const getLocationDataController = {
locationNameOrPostcode.toUpperCase() // Set the name to the partial postcode
matches = [matches[0]]
}

request.yar.set('locationData', { data: matches })

const { forecastNum, nearestLocationsRange } = getNearestLocation(
matches,
forecasts.forecasts,
measurements.measurements,
'uk-location'
)
request.yar.set('locationData', {
data: matches,
rawForecasts: forecasts.forecasts,
forecastNum,
forecastSummary,
nearestLocationsRange
})
//
if (matches.length === 1) {
const locationDetails = matches[0]
let title = ''
Expand All @@ -152,24 +178,27 @@ const getLocationDataController = {
title = locationDetails.GAZETTEER_ENTRY.DISTRICT_BOROUGH
}
}
//
const airQuality = getAirQuality(forecastNum[0])
return h.view('locations/location', {
result: matches[0],
airQuality,
airQualityData: airQualityData.commonMessages,
monitoringSites,
monitoringSites: nearestLocationsRange,
siteTypeDescriptions,
pollutantTypes,
displayBacklink: true,
pageTitle: title,
serviceName: 'Check local air quality'
serviceName: 'Check local air quality',
forecastSummary
})
} else if (matches.length > 1 && locationNameOrPostcode.length > 3) {
return h.view('locations/multiple-locations', {
results: matches,
userLocation: locationNameOrPostcode,
airQuality,
airQualityData: airQualityData.commonMessages,
monitoringSites,
monitoringSites: nearestLocationsRange,
siteTypeDescriptions,
pollutantTypes,
pageTitle: `Locations matching ${userLocation}`,
Expand All @@ -182,7 +211,7 @@ const getLocationDataController = {
})
}
} else if (locationType === 'ni-location') {
const postcodeApiUrl = `https://api.postcodes.io/postcodes?q=${encodeURIComponent(userLocation)}`
const postcodeApiUrl = `${process.env.NORTHERN_IRELAND_POSTCODE_URL}${encodeURIComponent(userLocation)}`
const response = await axios.get(postcodeApiUrl)
const { result } = response.data

Expand All @@ -195,25 +224,39 @@ const getLocationDataController = {
const locationData = {
GAZETTEER_ENTRY: {
NAME1: result[0].postcode,
DISTRICT_BOROUGH: result[0].admin_district
DISTRICT_BOROUGH: result[0].admin_district,
LONGITUDE: result[0].longitude,
LATITUDE: result[0].latitude
}
}
const { forecastNum, nearestLocationsRange } = getNearestLocation(
result,
forecasts.forecasts,
measurements.measurements,
'Ireland'
)
let title = ''
if (locationData) {
title =
locationData.GAZETTEER_ENTRY.NAME1 +
', ' +
locationData.GAZETTEER_ENTRY.DISTRICT_BOROUGH
}
logger.info(
`coordinates latitude: ${locationData.GAZETTEER_ENTRY.LATITUDE} longitude: ${locationData.GAZETTEER_ENTRY.LONGITUDE}`
)
const airQuality = getAirQuality(forecastNum[0])
return h.view('locations/location', {
result: locationData,
airQuality,
airQualityData: airQualityData.commonMessages,
monitoringSites,
monitoringSites: locationData.nearestLocationsRange,
siteTypeDescriptions,
pollutantTypes,
pageTitle: title,
displayBacklink: true
displayBacklink: true,
forecastSummary,
nearestLocationsRange
})
}
} catch (error) {
Expand Down Expand Up @@ -243,17 +286,17 @@ const getLocationDetailsController = {
} else {
title = locationDetails.GAZETTEER_ENTRY.DISTRICT_BOROUGH
}
const airQuality =
getAirQuality(/* Retrieved from session or another source */)
const airQuality = getAirQuality(locationData.forecastNum[0])
return h.view('locations/location', {
result: locationDetails,
airQuality,
airQualityData: airQualityData.commonMessages,
monitoringSites,
monitoringSites: locationData.nearestLocationsRange,
siteTypeDescriptions,
pollutantTypes,
pageTitle: title,
displayBacklink: true
displayBacklink: true,
forecastSummary: locationData.forecastSummary
})
} else {
return h.view('location-not-found')
Expand Down
Loading

0 comments on commit 53ee781

Please sign in to comment.