Skip to content

Commit

Permalink
Added basic error handling to data fetch in emissions script
Browse files Browse the repository at this point in the history
  • Loading branch information
fershad authored Oct 23, 2024
2 parents 668d8e4 + 7891414 commit e6847e5
Showing 1 changed file with 98 additions and 88 deletions.
186 changes: 98 additions & 88 deletions data/functions/generate_average_co2.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,109 +51,119 @@ const type = "average";
// Save a minified version of the JS file to the src/data folder

(async () => {
const response = await fetch(sourceURL);
const data = await response.json();

/**
* Groups the API data by country code.
* @type {Object.<string, Array>}
*/
const groupedData = await data.reduce((acc, item) => {
const key =
item.country_code === "" ? item.country_or_region : item.country_code;
if (!acc[key]) {
acc[key] = [];
try {
const response = await fetch(sourceURL);
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
acc[key].push(item);
return acc;
}, {});

/**
* Extracts the latest year's data for each country.
* @type {Object.<string, Object>}
*/
const latestData = await Object.keys(groupedData).reduce((acc, key) => {
// Find the last year in the array with emissions intensity data
const latestYear = groupedData[key].reduce((acc, item, index) => {
const data = await response.json();

/**
* Groups the API data by country code.
* @type {Object.<string, Array>}
*/
const groupedData = await data.reduce((acc, item) => {
const key =
item.country_code === "" ? item.country_or_region : item.country_code;
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(item);
return acc;
}, {});

/**
* Extracts the latest year's data for each country.
* @type {Object.<string, Object>}
*/
const latestData = await Object.keys(groupedData).reduce((acc, key) => {
// Find the last year in the array with emissions intensity data
const latestYear = groupedData[key].reduce((acc, item, index) => {
if (
item.emissions_intensity_gco2_per_kwh === null ||
item.emissions_intensity_gco2_per_kwh === ""
) {
return acc;
}
return index;
}, 0);

acc[key] = groupedData[key][latestYear];
return acc;
}, {});

// Loop through the data and extract the emissions intensity data
// Save it to the gridIntensityResults object with the country code as the key
Object.values(latestData).forEach((row) => {
if (
item.emissions_intensity_gco2_per_kwh === null ||
item.emissions_intensity_gco2_per_kwh === ""
row.emissions_intensity_gco2_per_kwh === null ||
row.emissions_intensity_gco2_per_kwh === ""
) {
return acc;
return;
}
return index;
}, 0);

acc[key] = groupedData[key][latestYear];
return acc;
}, {});

// Loop through the data and extract the emissions intensity data
// Save it to the gridIntensityResults object with the country code as the key
Object.values(latestData).forEach((row) => {
if (
row.emissions_intensity_gco2_per_kwh === null ||
row.emissions_intensity_gco2_per_kwh === ""
) {
return;
}

const country =
row.country_code === "" ? row.country_or_region : row.country_code;
const country =
row.country_code === "" ? row.country_or_region : row.country_code;

gridIntensityResults[country.toUpperCase()] =
row.emissions_intensity_gco2_per_kwh;
gridIntensityResults[country.toUpperCase()] =
row.emissions_intensity_gco2_per_kwh;

generalResults[country] = {
country_code: row.country_code,
country_or_region: row.country_or_region,
year: row.year,
emissions_intensity_gco2_per_kwh: row.emissions_intensity_gco2_per_kwh,
};
});
generalResults[country] = {
country_code: row.country_code,
country_or_region: row.country_or_region,
year: row.year,
emissions_intensity_gco2_per_kwh: row.emissions_intensity_gco2_per_kwh,
};
});

const jsDocComments = `/**
// Ensure directories exist
fs.mkdirSync("data/output", { recursive: true });
fs.mkdirSync("src/data", { recursive: true });

const jsDocComments = `/**
* @fileoverview Minified average CO2 emissions intensity data for countries.
* @generated Generated by generate_average_co2.js
* @version 1.0.0
*/
/**
* @constant {Object.<string, number>} data - Average CO2 emissions intensity data for various countries.
* @constant {string} type - Type of data being represented.
*/`;

/**
* Saves the country code and emissions data for use in the CO2.js library.
* @type {void}
*/
fs.writeFileSync(
"data/output/average-intensities.js",
`
const data = ${JSON.stringify(gridIntensityResults, null, " ")};
const type = "${type}";
export { data, type };
export default { data, type };
`
);
/**
* Saves a minified version of the data for easy import into the library.
* @type {void}
*/
fs.writeFileSync(
"src/data/average-intensities.min.js",
`${jsDocComments}
const data = ${JSON.stringify(
gridIntensityResults
)}; const type = "${type}"; export { data, type }; export default { data, type };`
);

/**
* Saves the full data set as a JSON file for reference.
* @type {void}
*/
fs.writeFileSync(
"data/output/average-intensities.json",
JSON.stringify(generalResults, null, " ")
);
/**
* Saves the country code and emissions data for use in the CO2.js library.
* @type {void}
*/
fs.writeFileSync(
"data/output/average-intensities.js",
`const data = ${JSON.stringify(gridIntensityResults, null, " ")};
const type = "${type}";
export { data, type };
export default { data, type };
`
);

/**
* Saves a minified version of the data for easy import into the library.
* @type {void}
*/
fs.writeFileSync(
"src/data/average-intensities.min.js",
`${jsDocComments}
const data = ${JSON.stringify(
gridIntensityResults
)}; const type = "${type}"; export { data, type }; export default { data, type };`
);

/**
* Saves the full data set as a JSON file for reference.
* @type {void}
*/
fs.writeFileSync(
"data/output/average-intensities.json",
JSON.stringify(generalResults, null, " ")
);
} catch (error) {
console.error("Error fetching or processing data:", error);
}
})();

0 comments on commit e6847e5

Please sign in to comment.