thegreenwebfoundation / co2.js

An npm module for accessing the green web API, and estimating the carbon emissions from using digital services
Other
375 stars 47 forks source link

SWDM v4 returns `returnVisitCO2e` segment value when no `returnVisitPercentage` is specified #211

Closed fershad closed 1 month ago

fershad commented 1 month ago

Describe the bug Using Sustainable Web Design version 4, and returning segment results sees the returnVisitCO2e value returned even if no custom returnVisitPercentage was passed into the method. This should not occur, as if there's no returnVisitPercentage set, it should be expected that value is 0 and thus the returnVisitCO2e would also be 0.

To Reproduce

import { co2 } from '@tgwf/co2'
const estimate = new co2({ model: "swd", results: "segment", version: 4)}
const bytesSent = 1000 * 1000 * 1000; // 1GB expressed in bytes

const estimatedCO2 = estimate.perVisitTrace(bytesSent);

This returns

{
  dataCenterOperationalCO2e: 27.17,
  networkOperationalCO2e: 29.145999999999997,
  consumerDeviceOperationalCO2e: 39.52,
  dataCenterEmbodiedCO2e: 5.928,
  networkEmbodiedCO2e: 6.422,
  consumerDeviceEmbodiedCO2e: 40.014,
  totalEmbodiedCO2e: 52.364000000000004,
  totalOperationalCO2e: 95.83600000000001,
  dataCenterCO2e: 33.098,
  networkCO2e: 35.568,
  consumerDeviceCO2e: 79.534,
  firstVisitCO2e: 148.20000000000002,
  returnVisitCO2e: 148.20000000000002,
  total: 148.20000000000002
}

Notice that both the firstVisitCO2e and returnVisitCO2e is returned with values > 0.

Expected behavior When no returnVisitPercentage is specified, it should be implied that the value is 0 and therefore the returnVisitCO2e should also be 0.

fershad commented 1 month ago

This is as expected, since the returnVisitCO2e value only reflects the impact of dataReloadRatio on emissions. That is, it shows what the reduction in emissions would be as a result of caching applied when a page is loaded. However, since the values returned are for a single page load, there is no need use the returnVisitorPercentage in calculating the returnVisitCO2e value. As a result, when dataReloadRatio is 0, the returnVisitCO2e value will be the same as the firstVisitCO2e value.

Adjusting the dataReloadRatio but keeping the returnVisitorPercentage as 0 will see the returnVisitCO2e result change, but the total CO2e result remain the same as firstVisitCO2e.

import { co2 } from '@tgwf/co2'
const estimate = new co2({ model: "swd", results: "segment", version: 4})
const bytesSent = 1000 * 1000 * 1000; // 1GB expressed in bytes

const estimatedCO2 = estimate.perVisitTrace(bytesSent, false, {
    dataReloadRatio: 0.5,
});

/* Returns
co2: {
    dataCenterOperationalCO2e: 27.17,
    networkOperationalCO2e: 29.145999999999997,
    consumerDeviceOperationalCO2e: 39.52,
    dataCenterEmbodiedCO2e: 5.928,
    networkEmbodiedCO2e: 6.422,
    consumerDeviceEmbodiedCO2e: 40.014,
    totalEmbodiedCO2e: 52.364000000000004,
    totalOperationalCO2e: 95.83600000000001,
    dataCenterCO2e: 33.098,
    networkCO2e: 35.568,
    consumerDeviceCO2e: 79.534,
    firstVisitCO2e: 148.20000000000002,
    returnVisitCO2e: 74.10000000000001,
    total: 148.20000000000002
  }, ...
*/

Also changing the returnVisitorPercentage will then see a change in the total value. This is because the total is adjusted for the returnVisitorPercentage and firstVisitorPercentage variables, while the individual returnVisitCO2e and firstVisitCO2e values are not.

import { co2 } from '@tgwf/co2'
const estimate = new co2({ model: "swd", results: "segment", version: 4})
const bytesSent = 1000 * 1000 * 1000; // 1GB expressed in bytes

const estimatedCO2 = estimate.perVisitTrace(bytesSent, false, {
    dataReloadRatio: 0.5,
    returnVisitPercentage: 0.4,
    firstVisitPercentage: 0.6,
});

/* Returns
co2: {
    dataCenterOperationalCO2e: 27.17,
    networkOperationalCO2e: 29.145999999999997,
    consumerDeviceOperationalCO2e: 39.52,
    dataCenterEmbodiedCO2e: 5.928,
    networkEmbodiedCO2e: 6.422,
    consumerDeviceEmbodiedCO2e: 40.014,
    totalEmbodiedCO2e: 52.364000000000004,
    totalOperationalCO2e: 95.83600000000001,
    dataCenterCO2e: 33.098,
    networkCO2e: 35.568,
    consumerDeviceCO2e: 79.534,
    firstVisitCO2e: 148.20000000000002,
    returnVisitCO2e: 74.10000000000001,
    total: 118.56
  },...
*/