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

Remove Page X-ray specific functions #182

Closed
fershad opened this issue Dec 19, 2023 · 4 comments
Closed

Remove Page X-ray specific functions #182

fershad opened this issue Dec 19, 2023 · 4 comments
Milestone

Comments

@fershad
Copy link
Contributor

fershad commented Dec 19, 2023

Describe the bug
There's a bunch of Page X-ray specific functions in the CO2.js library. This isn't specific to any model. I am not aware of any other user (besides sitespeedio/sitespeed.io) who are using these functions.

@soulgalore would it be possible to move these functions into the Sitespeed sustainbility plugin itself? That would help reduce the code we ship with the library (#121) as well as reduce the size of our tests.

We would not make this change for a few months, and would socialise it with the community to try and ensure no other projects are impacted.

Additional context

co2.js/src/co2.js

Lines 190 to 290 in 51b85e2

perDomain(pageXray, greenDomains) {
const co2PerDomain = [];
for (let domain of Object.keys(pageXray.domains)) {
let co2;
if (greenDomains && greenDomains.indexOf(domain) > -1) {
co2 = this.perByte(pageXray.domains[domain].transferSize, true);
} else {
co2 = this.perByte(pageXray.domains[domain].transferSize);
}
co2PerDomain.push({
domain,
co2,
transferSize: pageXray.domains[domain].transferSize,
});
}
co2PerDomain.sort((a, b) => b.co2 - a.co2);
return co2PerDomain;
}
perPage(pageXray, green) {
// Accept an xray object, and if we receive a boolean as the second
// argument, we assume every request we make is sent to a server
// running on renwewable power.
// if we receive an array of domains, return a number accounting the
// reduced CO2 from green hosted domains
const domainCO2 = this.perDomain(pageXray, green);
let totalCO2 = 0;
for (let domain of domainCO2) {
totalCO2 += domain.co2;
}
return totalCO2;
}
perContentType(pageXray, greenDomains) {
const co2PerContentType = {};
for (let asset of pageXray.assets) {
const domain = new URL(asset.url).domain;
const transferSize = asset.transferSize;
const co2ForTransfer = this.perByte(
transferSize,
greenDomains && greenDomains.indexOf(domain) > -1
);
const contentType = asset.type;
if (!co2PerContentType[contentType]) {
co2PerContentType[contentType] = { co2: 0, transferSize: 0 };
}
co2PerContentType[contentType].co2 += co2ForTransfer;
co2PerContentType[contentType].transferSize += transferSize;
}
// restructure and sort
const all = [];
for (let type of Object.keys(co2PerContentType)) {
all.push({
type,
co2: co2PerContentType[type].co2,
transferSize: co2PerContentType[type].transferSize,
});
}
all.sort((a, b) => b.co2 - a.co2);
return all;
}
dirtiestResources(pageXray, greenDomains) {
const allAssets = [];
for (let asset of pageXray.assets) {
const domain = new URL(asset.url).domain;
const transferSize = asset.transferSize;
const co2ForTransfer = this.perByte(
transferSize,
greenDomains && greenDomains.indexOf(domain) > -1
);
allAssets.push({ url: asset.url, co2: co2ForTransfer, transferSize });
}
allAssets.sort((a, b) => b.co2 - a.co2);
return allAssets.slice(0, allAssets.length > 10 ? 10 : allAssets.length);
}
perParty(pageXray, greenDomains) {
let firstParty = 0;
let thirdParty = 0;
// calculate co2 per first/third party
const firstPartyRegEx = pageXray.firstPartyRegEx;
for (let d of Object.keys(pageXray.domains)) {
if (!d.match(firstPartyRegEx)) {
thirdParty += this.perByte(
pageXray.domains[d].transferSize,
greenDomains && greenDomains.indexOf(d) > -1
);
} else {
firstParty += this.perByte(
pageXray.domains[d].transferSize,
greenDomains && greenDomains.indexOf(d) > -1
);
}
}
return { firstParty, thirdParty };
}

@fershad fershad added this to the 0.16 milestone Dec 19, 2023
@soulgalore
Copy link
Contributor

Hi @fershad yes let's move them, I can do that the coming week, I'll ping the issue when I'm done or if I get into trouble.

@fershad
Copy link
Contributor Author

fershad commented Dec 19, 2023

Thanks @soulgalore. No rush to get this in, I don't see us making the change at our end until late Jan or Feb 2024.

soulgalore added a commit to sitespeedio/sitespeed.io that referenced this issue Dec 21, 2023
* Move functionality from co2 to sitespeed.io.

See thegreenwebfoundation/co2.js#182

* lint
@soulgalore
Copy link
Contributor

@fershad that is merged in sitespeed.io, going out in a release before Christmas. I think it actually would be cool if co2 provided that data but you could collect it directly from a HAR file to get rid of the PageXray dependency.

@mrchrisadams
Copy link
Member

hey @soulgalore , @fershad - I think what Peter raises is a good point that we might touch on in future.

We know right now that different calculators make different assumptions how they aggregate figures per domain - this likely goes some way to explaining why Ecograder, Sitespeed, and Webpagetest can have slightly different figures for the same page.

If this kind of functionality isn't in CO2.js, it might be worth at least us documenting the different approaches used to provide the per-domain figures.

I think some calculators use a global annual co2 intensity figure, and others use a national annual figure, and some even use national hourly figure from a source like electricity maps.

I've created #183 to capture this discussion for future reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants