From 3c0828b73d04408290ae7ff4ef8e0d80651af41e Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Thu, 26 Dec 2024 07:27:08 +0100 Subject: [PATCH] Read co2 information once at startup (#4352) * read at startup * better logs --- lib/plugins/sustainable/index.js | 37 +++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/plugins/sustainable/index.js b/lib/plugins/sustainable/index.js index 71b41fadef..1f664ea092 100644 --- a/lib/plugins/sustainable/index.js +++ b/lib/plugins/sustainable/index.js @@ -79,7 +79,22 @@ export default class SustainablePlugin extends SitespeedioPlugin { DEFAULT_METRICS_PAGE_SUMMARY, 'sustainable.pageSummary' ); + + const greenDomainJSONpath = path.join( + __dirname, + 'data', + 'url2green.json.gz' + ); + + if ( + !this.sustainableOptions.disableHosting && + !this.sustainableOptions.useGreenWebHostingAPI + ) { + log.info('Reading green domain data from disk'); + this.data = await loadJSON(greenDomainJSONpath); + } } + async processMessage(message, queue) { const make = this.make; const aggregator = this.aggregator; @@ -118,23 +133,21 @@ export default class SustainablePlugin extends SitespeedioPlugin { case 'pagexray.run': { // We got data for a URL, lets calculate co2, check green servers etc const listOfDomains = Object.keys(message.data.domains); - const greenDomainJSONpath = path.join( - __dirname, - 'data', - 'url2green.json.gz' - ); let hostingGreenCheck; if (this.sustainableOptions.disableHosting === true) { hostingGreenCheck = []; } else { - hostingGreenCheck = - this.sustainableOptions.useGreenWebHostingAPI === true - ? await hosting.check(listOfDomains, undefined, 'sitespeed.io') - : await hosting.check( - listOfDomains, - await loadJSON(greenDomainJSONpath) - ); + if (this.sustainableOptions.useGreenWebHostingAPI === true) { + log.info('Fetch hosting information from The Green Web Foundation'); + hostingGreenCheck = await hosting.check( + listOfDomains, + undefined, + 'sitespeed.io' + ); + } else { + hostingGreenCheck = await hosting.check(listOfDomains, this.data); + } } const configuration = { model: this.sustainableOptions.model };