Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read co2 information once at startup #4352

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading