Skip to content

Commit

Permalink
docs: adding documentation for co2js model
Browse files Browse the repository at this point in the history
Signed-off-by: Gnanakeethan Balasubramaniam <[email protected]>
  • Loading branch information
gnanakeethan committed Dec 6, 2023
1 parent a4e41c8 commit 1ea353d
Showing 1 changed file with 139 additions and 0 deletions.
139 changes: 139 additions & 0 deletions src/lib/co2js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# CO2.JS

> [!NOTE]
> `CO2.JS` is a community model, not part of the IF standard library. This means the IF core team are not closely monitoring these models to keep them up to date. You should do your own research before implementing them!

# Parameters

## model config

- `type`: supported models by the library, `swd` or `1byte`

## observations

- `bytes`: the number of bytes transferred
- `green-web-host`: true if the website is hosted on a green web host, false otherwise
- `duration`: the amount of time the observation covers, in seconds
- `timestamp`: a timestamp for the observation

## Returns

- `operational-carbon`: carbon emissions from the operation of the website, in grams of CO2e


# IF Implementation

IF Utlilises the CO2JS Framework to calculate the carbon emissions of a website. The CO2JS Framework is a collection of models that calculate the carbon emissions of a website based on different parameters. The CO2JS Framework is a community model, not part of the IF standard library. This means the IF core team are not closely monitoring these models to keep them up to date. You should do your own research before implementing them!


## Usage
In IEF the model is called from an `impl`. An `impl` is a `.yaml` file that contains configuration metadata and usage inputs. This is interpreted by the command line tool, `impact-engine`. There, the model's `configure` method is called first. The model config should define a `type` either `swd` or `1byte` supported by the CO2.JS library. Each input is expected to contain `bytes`,`green-web-host`,`duration` and `timestamp` fields.
## IMPL

The following is an example of how CO2.JS can be invoked using an `impl`.
```yaml
name: co2js-demo
description: example impl invoking CO2.JS model
initialize:
models:
- name: co2js
model: Co2JsModel
path: '@grnsft/if-unofficial-models'

graph:
children:
child:
pipeline:
- co2js
config:
co2js:
type: swd
inputs:
- timestamp: 2023-07-06T00:00 # [KEYWORD] [NO-SUBFIELDS] time when measurement occurred
duration: 1
bytes: 1000000
green-web-host: true
```
This impl is run using `impact-engine` using the following command, run from the project root:

```sh
npm i -g @grnsft/if
npm i -g @grnsft/if-unofficial-models
impact-engine --impl ./examples/impls/co2js-test.yml --ompl ./examples/ompls/co2js-test.yml
```

This yields a result that looks like the following (saved to `/ompls/co2js-test.yml`):

```yaml
name: co2js-demo
description: example impl invoking CO2.JS model
initialize:
models:
- name: co2js
model: Co2JsModel
path: '@grnsft/if-unofficial-models'
graph:
children:
child:
pipeline:
- co2js
config:
co2js:
type: swd
inputs:
- timestamp: 2023-07-06T00:00
duration: 1
bytes: 1000000
green-web-host: true
outputs:
- timestamp: 2023-07-06T00:00
operational-carbon: 0.02
duration: 1
bytes: 1000000
green-web-host: true
```


## TypeScript

You can see example Typescript invocations for each model below.

### SWD

```typescript
import {Co2jsModel} from '@grnsft/if-unofficial-models';
const co2js = await (new Co2jsModel()).configure({
type: 'swd'
})
const results = co2js.execute([
{
duration: 3600, // duration institute
timestamp: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
bytes: 1000000, // bytes transferred
'green-web-host': true // true if the website is hosted on a green web host, false otherwise
}
])
```


### 1byte

```typescript
import {Co2jsModel} from '@grnsft/if-unofficial-models';
const co2js = await (new Co2jsModel()).configure({
type: '1byte'
})
const results = co2js.execute([
{
duration: 3600, // duration institute
timestamp: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
bytes: 1000000, // bytes transferred
'green-web-host': true // true if the website is hosted on a green web host, false otherwise
}
])
```

0 comments on commit 1ea353d

Please sign in to comment.