-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Hanoi ready for deployment with ~35 stations * removed unused packages and updated deprecated ones --------- Co-authored-by: Gabriel Fosse <[email protected]>
- Loading branch information
1 parent
9a2670b
commit 97081bf
Showing
10 changed files
with
18,575 additions
and
16,428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// /** | ||
// * This code is responsible for implementing all methods related to fetching | ||
// * and returning data for the Hanoi data sources. | ||
// */ | ||
|
||
import { DateTime } from 'luxon'; | ||
import client from '../lib/requests.js'; | ||
import { unifyParameters } from '../lib/utils.js'; | ||
|
||
import log from '../lib/logger.js'; | ||
|
||
export const name = 'hanoi'; | ||
|
||
export async function fetchData (source, cb) { | ||
try { | ||
const stations = await fetchStations(source); | ||
const measurements = transformData(stations); | ||
log.debug(measurements); | ||
cb(null, { | ||
name: 'unused', | ||
measurements, | ||
}); | ||
} catch (error) { | ||
log.error(error); | ||
cb(error); | ||
} | ||
} | ||
|
||
async function fetchStations (source) { | ||
try { | ||
const response = await client(source.url); | ||
const stations = JSON.parse(response.body); | ||
|
||
const stationDataPromises = stations.map((station) => | ||
fetchStationData(source.sourceURL, station.id) | ||
); | ||
|
||
const allStationData = await Promise.all(stationDataPromises); | ||
|
||
stations.forEach((station, index) => { | ||
station.measurements = allStationData[index]; | ||
}); | ||
|
||
return stations; | ||
} catch (error) { | ||
log.error('Error fetching stations:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function fetchStationData (baseURL, stationId) { | ||
try { | ||
const response = await client( | ||
baseURL + `public/dailystat/${stationId}` | ||
); | ||
const data = JSON.parse(response.body); | ||
|
||
const measurements = {}; | ||
const validParameters = [ | ||
'PM2.5', | ||
'PM10', | ||
'NO2', | ||
'CO', | ||
'SO2', | ||
'O3', | ||
]; | ||
validParameters.forEach((param) => { | ||
if (data[param]) { | ||
measurements[param] = data[param].slice(-3); | ||
} | ||
}); | ||
|
||
return measurements; | ||
} catch (error) { | ||
log.error(`Error fetching data for station ${stationId}:`, error); | ||
throw error; | ||
} | ||
} | ||
|
||
function transformData (stations) { | ||
let transformedData = []; | ||
|
||
stations.forEach((station) => { | ||
Object.keys(station.measurements).forEach((parameter) => { | ||
station.measurements[parameter].forEach((measurement) => { | ||
const hanoiTime = DateTime.fromFormat( | ||
measurement.time, | ||
'yyyy-MM-dd HH:mm', | ||
{ zone: 'Asia/Ho_Chi_Minh' } | ||
); | ||
const transformedMeasurement = { | ||
parameter, | ||
date: { | ||
utc: hanoiTime | ||
.toUTC() | ||
.toISO({ suppressMilliseconds: true }), | ||
local: hanoiTime.toISO({ suppressMilliseconds: true }) | ||
}, | ||
value: parseFloat(measurement.value), | ||
unit: 'µg/m³', | ||
location: station.name, | ||
city: 'Hanoi', | ||
coordinates: { | ||
latitude: station.latitude, | ||
longitude: station.longtitude | ||
}, | ||
attribution: [ | ||
{ | ||
name: 'Hanoi Air Quality Monitoring Network', | ||
url: 'https://moitruongthudo.vn.ae/' | ||
} | ||
], | ||
averagingPeriod: { unit: 'hours', value: 1 } | ||
}; | ||
transformedData.push(unifyParameters(transformedMeasurement)); | ||
}); | ||
}); | ||
}); | ||
transformedData = transformedData.filter( | ||
(measurement) => | ||
!isNaN(measurement.value) && measurement.value !== null | ||
); | ||
return transformedData; | ||
} |
Oops, something went wrong.