Skip to content

Commit

Permalink
Add Hanoi (#1074)
Browse files Browse the repository at this point in the history
* Hanoi ready for deployment with ~35 stations

* removed unused packages and updated deprecated ones

---------

Co-authored-by: Gabriel Fosse <[email protected]>
  • Loading branch information
majesticio and Gabriel Fosse authored Jan 26, 2024
1 parent 9a2670b commit 97081bf
Show file tree
Hide file tree
Showing 10 changed files with 18,575 additions and 16,428 deletions.
14 changes: 0 additions & 14 deletions .eslintrc

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

Changes to adapters will be documented in this file.
## 11/26/2024
### Removed eslint
- Removed eslint from package.json
- Removed .eslintrc

## 11/22/2023
### Set active: false
- south-australia-epa (needs fix, url won't resolve)
Expand Down
17,378 changes: 6,289 additions & 11,089 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 4 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"description": "A tool to collect data for OpenAQ platform.",
"repository": "https://github.com/openaq/openaq-fetch",
"scripts": {
"lint": "eslint .",
"test": "npm run lint && mocha test/lib/ --require babel-register",
"test": "mocha test/lib/ --require babel-register",
"start": "node index.js",
"docker": "docker-compose --project-name openaq run --rm fetch"
},
Expand All @@ -15,10 +14,8 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.67.0",
"@aws-sdk/client-sqs": "^3.78.0",
"JSONStream": "^1.3.4",
"async": "^3.2.3",
"aws-sdk": "^2.1112.0",
"babel-register": "^6.26.0",
"aws-sdk": "^2.1544.0",
"bottleneck": "^2.19.5",
"byline": "^5.0.0",
"cheerio": "^1.0.0-rc.2",
Expand All @@ -27,14 +24,14 @@
"ftp": "^0.3.10",
"got": "^12.6.0",
"jsonschema": "^1.2.0",
"JSONStream": "^1.3.4",
"knex": "^1.0.6",
"lodash": "^4.17.10",
"luxon": "^3.0.4",
"moment-timezone": "^0.5.21",
"node-fetch": "^3.3.0",
"node-html-parser": "^6.1.5",
"proj4": "^2.8.0",
"proj4js-defs": "0.0.1",
"proj4js-defs": "^0.0.1",
"request": "^2.88.0",
"rss-parser": "^3.12.0",
"s3-upload-stream": "^1.0.7",
Expand All @@ -51,11 +48,6 @@
"@babel/preset-env": "^7.16.11",
"chai": "^4.1.2",
"depcheck": "^1.4.3",
"eslint": "^8.13.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.0.0",
"mocha": "^9.2.2"
}
}
2 changes: 1 addition & 1 deletion run-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'use strict';

// only ES5 is allowed in this file
require('babel-register')({
require('@babel-register')({
presets: [ 'es2015' ]
});

Expand Down
124 changes: 124 additions & 0 deletions src/adapters/hanoi.js
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;
}
Loading

0 comments on commit 97081bf

Please sign in to comment.