|
| 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 | + |
0 commit comments