Skip to content

Commit f569f90

Browse files
committed
wip: calculate emissions from page speed results
1 parent 7895c1f commit f569f90

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"node": ">=14.0.0"
2323
},
2424
"dependencies": {
25+
"@tgwf/co2": "^0.16.9",
2526
"archiver": "^5.3.1"
26-
},
27-
"devDependencies": {}
28-
}
27+
}
28+
}

src/core/CarbonCalculator.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { co2 } from "@tgwf/co2";
2+
3+
// TODO: This function currently returns emissions for both the OneByte and SWD models.
4+
// Not sure which one we ultimately want to use.
5+
export const calculateEmissionsFromPageSpeedResults = (pageSpeedResults) => {
6+
const totalByteWeight = pageSpeedResults.lighthouseResult.audits['total-byte-weight'].numericValue;
7+
// this appears to be the same number as totalByteWeight:
8+
// const totalByteWeightSummed = pageSpeedResults.lighthouseResult.audits['network-requests'].details.items.reduce((total, item) => total + item.transferSize, 0);
9+
10+
// OneByte model
11+
// https://developers.thegreenwebfoundation.org/co2js/models/#using-the-onebyte-model
12+
const perByte = new co2({ model: "1byte" });
13+
const emissionsOneByte = perByte.perByte(totalByteWeight);
14+
15+
// Sustainable Web Design (SWD) model
16+
// https://developers.thegreenwebfoundation.org/co2js/models/#using-the-sustainable-web-design-model-default-v0110
17+
const swd = new co2({ model: "swd" });
18+
const emissionsSWD = swd.perVisit(totalByteWeight);
19+
20+
return { emissionsOneByte, emissionsSWD };
21+
}
22+
23+
// TODO: The code below is for testing purposes only.
24+
// It should be removed once it's able to be integrated with https://sparkbox.atlassian.net/browse/BLDL-9
25+
// tempdata.json should also be removed.
26+
(async () => {
27+
const pageSpeedResults = (await import("./tempdata.json", { with: { type: "json" } })).default;
28+
const results = calculateEmissionsFromPageSpeedResults(pageSpeedResults)
29+
console.log("Emissions Calculation Results:", results);
30+
})();
31+

src/core/tempdata.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)