thegreenwebfoundation / co2.js

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

Segment level results output #113

Closed fershad closed 1 year ago

fershad commented 1 year ago

This is currently very rough around the edges, but it makes a start on #101. The aim is to give developers a way in which they can return more detailed results when using CO2.js. Results would be returned as an object with CO2 estimates for each system segment, as well as a total CO2 value.

The proposed implementation would see developers pass a results parameter as part of the options object when initiating CO2.js. This then gets passed along to the method being called, and results are returned as a JSON object.

import tgwf from '@tgwf/co2';
const co2 = new tgwf.co2({model: 'swd', results: 'detailed' })
const results = co2.perByte(1000000)

The code above to generate the results below:

{
    consumerDeviceEnergy: 0.1861704,
    networkEnergy: 0.05012280000000001,
    productionEnergy: 0.06802380000000001,
    dataCenterEnergy: 0.053703
}

Todos:

fershad commented 1 year ago

Release CO2.js v0.12.0

fershad commented 1 year ago

@mrchrisadams this PR is ready for review I think. A quick explainer of what it does, and where to look (merged from main which has polluted the changes a bit).

What this PR add/changes This PR adds a new output option for the SWD model, where results can be returned as an containing a breakdown of CO2 emissions by system segment. This can be done by passing in the results: 'segment' parameter into the options object.

import tgwf from '@tgwf/co2';
const co2 = new tgwf.co2({model: 'swd', results: 'segment' })
const results = co2.perByte(1000000)

// Returns
{
        consumerDeviceCO2: 0.1861704,
        networkCO2: 0.05012280000000001,
        productionCO2: 0.06802380000000001,
        dataCenterCO2: 0.006075,
        total: 0.31039200000000006
}

I've gone with this approach as it leaves us a bit of freedom to add more result types later down the line.

This change only applies to the SWD model. The 1byte model is unchanged. Passing results: 'segment' into a new 1byte Object will not have any impact, and the parameter is ignored.

Files that have changed

fershad commented 1 year ago

@mrchrisadams I know we talked about adding a "trace" result here which shows the CO2 object that's being used to generate results. I think it makes more sense for that to go in with the new functions we've created in #126.

Perhaps something that can be added to the v0.13 release, in those new functions only so that it's not breaking for existing users.

If that's okay by you, I'll also merge this PR & prepare the release.