Skip to content

Commit d12a33e

Browse files
majesticioGabriel Fosse
andauthored
Fix/trinidad tobago parsing (#1063)
* fixed parsing bug and added error handling * clean up --------- Co-authored-by: Gabriel Fosse <[email protected]>
1 parent b2f175d commit d12a33e

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

src/adapters/trinidadtobago.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
import {
1212
unifyParameters,
13-
unifyMeasurementUnits
13+
unifyMeasurementUnits,
1414
} from '../lib/utils.js';
15+
import log from '../lib/logger.js';
1516
import { REQUEST_TIMEOUT } from '../lib/constants.js';
1617
import _ from 'lodash';
1718
import { DateTime } from 'luxon';
@@ -28,7 +29,7 @@ const timestamp = DateTime.now().toMillis();
2829
* @param {function} cb A callback of the form cb(err, data)
2930
*/
3031

31-
export async function fetchData (source, cb) {
32+
export async function fetchData(source, cb) {
3233
// Fetch both the measurements and meta-data about the locations
3334
// List of keys for parameters used in url [3: 'CO', 1465: 'NO2', 2130: 'O3', 18: 'PM-10', 20: 'PM-2.5', 23: 'SO2']
3435
const parameterIDs = ['3', '1465', '2130', '18', '20', '23'];
@@ -43,7 +44,10 @@ export async function fetchData (source, cb) {
4344
.replace('$parameter', parameterIDs[i]) + timestamp;
4445
const task = async function () {
4546
try {
46-
const { body } = await got(sourceURL, { cookieJar: new tough.CookieJar(), timeout: { request: REQUEST_TIMEOUT } });
47+
const { body } = await got(sourceURL, {
48+
cookieJar: new tough.CookieJar(),
49+
timeout: { request: REQUEST_TIMEOUT },
50+
});
4751
return { meta: e, values: body };
4852
} catch (err) {
4953
throw new Error(err.response.body);
@@ -54,7 +58,7 @@ export async function fetchData (source, cb) {
5458
});
5559

5660
try {
57-
const results = await Promise.all(tasks.map(task => task()));
61+
const results = await Promise.all(tasks.map((task) => task()));
5862
// Format the data
5963
const data = formatData(results);
6064
if (data === undefined) {
@@ -90,15 +94,31 @@ const formatData = function (results) {
9094
}
9195
return data;
9296
};
93-
// Loops through all items
97+
const validParameters = ['CO', 'NO2', 'O3', 'PM-10', 'PM-2.5', 'SO2'];
98+
9499
results.forEach((item) => {
95100
item = parseToJSON(item);
96-
// If values are empty or something fails, dont run
101+
97102
if (item !== undefined) {
103+
let parameter = validParameters.find((p) =>
104+
item.values.hasOwnProperty(p)
105+
);
106+
107+
if (
108+
!parameter ||
109+
item.values[parameter].length !== item.values.xlabels.length
110+
) {
111+
log.info(
112+
'Parameter mismatch or length mismatch between readings and labels.',
113+
item
114+
);
115+
return;
116+
}
117+
98118
const template = {
99119
city: item.meta.city,
100120
location: item.meta.location,
101-
parameter: Object.keys(item.values)[0],
121+
parameter: parameter.toLowerCase(),
102122
coordinates: {
103123
latitude: parseFloat(item.meta.latitude),
104124
longitude: parseFloat(item.meta.longitude),
@@ -110,18 +130,13 @@ const formatData = function (results) {
110130
},
111131
],
112132
averagingPeriod: { unit: 'hours', value: 1 },
133+
unit: parameter === 'CO' ? 'mg/m3' : 'ug/m3',
113134
};
114-
// Units are mostly ug/m3, but CO is mg/m3, according to site
115-
template.unit = template.parameter === 'CO' ? 'mg/m3' : 'ug/m3';
116-
// Loops through the latest data for 24 hours, data is hourly
117-
for (let i in item.values[template.parameter]) {
118-
// Do not add values if values are Null
119-
if (item.values[template.parameter][i] !== null) {
120-
let m = Object.assign(
121-
{ value: item.values[template.parameter][i] },
122-
template
123-
);
124-
// Adds the formated date
135+
136+
item.values[parameter].forEach((value, i) => {
137+
if (value !== null) {
138+
let m = Object.assign({ value: value }, template);
139+
125140
const dateMoment = DateTime.fromFormat(
126141
item.values.xlabels[i],
127142
'yyyy-MM-dd HH',
@@ -133,17 +148,16 @@ const formatData = function (results) {
133148
.toISO({ suppressMilliseconds: true }),
134149
local: dateMoment.toISO({ suppressMilliseconds: true }),
135150
};
136-
// unifies parameters and measurement units
151+
137152
m = unifyParameters(m);
138153
m = unifyMeasurementUnits(m);
139154
measurements.push(m);
140155
}
141-
}
156+
});
142157
}
143158
});
144-
// corrects the parameter names
159+
145160
measurements = correctMeasurementParameter(measurements);
146-
// filters out the measurements that are not the latest
147161
measurements = getLatestMeasurements(measurements);
148162
return {
149163
name: 'unused',

0 commit comments

Comments
 (0)