thegreenwebfoundation / co2.js

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

`perByteTrace` and `perVisitTrace` return incorrect information when using SWDM version 4 #212

Closed fershad closed 3 months ago

fershad commented 4 months ago

Describe the bug When using SWDM version 4, the perByteTrace and perVisitTrace return variables information for the variables object.

To Reproduce

import {co2} from "@tgwf/co2";

const co2Emission = new co2({ model: "swd", results: "segment", version: 4 });
const bytesSent = 1000 * 1000 * 1000; // 1GB expressed in bytes
const greenHost = false; // Is the data transferred from a green host?

const estimatedCO2 = co2Emission.perVisitTrace(bytesSent, greenHost);
console.log(estimatedCO2);

This 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: 148.20000000000002,
    total: 148.20000000000002
  },
  green: false,
  variables: {
    description: 'Below are the variables used to calculate this CO2 estimate.',
    bytes: 1000000000,
    gridIntensity: {
      description: 'The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.',
      network: 480.79,
      dataCenter: 480.79,
      production: 480.79,
      device: 480.79
    },
    dataReloadRatio: 0.02,
    firstVisitPercentage: 0.75,
    returnVisitPercentage: 0.25
  }
}

The returned variables show incorrect values for the default SWDM version 4 implementation that's used here.

Expected behavior

The expected variables object should be:

variables: {
    description: 'Below are the variables used to calculate this CO2 estimate.',
    bytes: 1000000000,
    gridIntensity: {
      description: 'The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.',
-     network: 480.79,
+     network: 494,
-     dataCenter: 480.79,
+     dataCenter: 494,
-      production: 480.79,
-      device: 480.79
+     device: 494
    },
-  dataReloadRatio: 0.02,
+  dataReloadRatio: 0,
-   firstVisitPercentage: 0.75,
+  firstVisitPercentage: 1,
-  returnVisitPercentage: 0.25
+  returnVisitPercentage: 0,
+  greenHostingFactor: 0
  }

Additional context (if applicable) This is likely because of the way the trace functions are coded at the moment. Most of the logic resides inside the CO2 class. A better solution would be to move this logic into the respective models themselves.