Skip to content

Commit

Permalink
Read co2 information once at startup (#4352)
Browse files Browse the repository at this point in the history
* read at startup

* better logs
  • Loading branch information
soulgalore authored Dec 26, 2024
1 parent 1b009d2 commit 3c0828b
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions lib/plugins/sustainable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 };
Expand Down

0 comments on commit 3c0828b

Please sign in to comment.