Skip to content

Commit

Permalink
Merge pull request #50 from alexzurbonsen/co2js-allow-config-as-input
Browse files Browse the repository at this point in the history
feat(lib): co2js: allow config and globalConfig parameters as input
  • Loading branch information
jmcook1186 authored Jun 14, 2024
2 parents c4a5749 + cac94b2 commit f4e7367
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 127 deletions.
55 changes: 33 additions & 22 deletions src/__tests__/unit/lib/co2js/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ describe('lib/co2js: ', () => {

describe('execute(): ', () => {
it('returns a result when `network/data/bytes` is provided in the input.', async () => {
const config = {type: '1byte', 'green-web-host': true};
const config = {type: '1byte'};
const inputs = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': true,
},
];
const result = await output.execute(inputs, config);
Expand All @@ -55,19 +56,21 @@ describe('lib/co2js: ', () => {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': true,
'carbon-operational': 0.023195833333333332,
},
]);
});

it('returns the same input data when the co2 model returns undefined for the `type`.', async () => {
process.env.WRONG_MODEL = 'true';
const config = {type: '1byte', 'green-web-host': true};
const config = {type: '1byte'};
const inputs = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': true,
},
];
const result = await output.execute(inputs, config);
Expand All @@ -79,17 +82,19 @@ describe('lib/co2js: ', () => {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': true,
},
]);
});

it('returns a result when `network/data` is provided.', async () => {
const config = {type: '1byte', 'green-web-host': true};
const config = {type: '1byte'};
const inputs = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data': 10,
'green-web-host': true,
},
];
const result = await output.execute(inputs, config);
Expand All @@ -101,18 +106,20 @@ describe('lib/co2js: ', () => {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data': 10,
'green-web-host': true,
'carbon-operational': 2319.583333333333,
},
]);
});

it('returns a result when `green-web-host` is false.', async () => {
const config = {type: '1byte', 'green-web-host': false};
const config = {type: '1byte'};
const inputs = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': false,
},
];
const result = await output.execute(inputs, config);
Expand All @@ -124,19 +131,21 @@ describe('lib/co2js: ', () => {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': false,
'carbon-operational': 0.029081299999999994,
},
]);
});

it('returns a result when `type` has `swg` value in the config.', async () => {
process.env.SWD_TYPE = 'true';
const config = {type: 'swd', 'green-web-host': true};
const config = {type: 'swd'};
const inputs = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': true,
},
];

Expand All @@ -149,35 +158,28 @@ describe('lib/co2js: ', () => {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': true,
'carbon-operational': 0.023208995205000006,
},
]);
});

it('returns a result when provided `options` in the global config.', async () => {
it('returns a result when `green-web-host` and `options` are provided in input.', async () => {
process.env.SWD_TYPE = 'true';
const config = {type: 'swd', 'green-web-host': false};
const output = Co2js({
options: {
dataReloadRatio: 0.6,
firstVisitPercentage: 0.9,
returnVisitPercentage: 0.1,
gridIntensity: {
device: 560.98,
dataCenter: 50,
networks: 437.66,
},
},
});

const config = {type: 'swd'};
const inputs = [
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': false,
options: {
dataReloadRatio: 0.6,
firstVisitPercentage: 0.9,
returnVisitPercentage: 0.1,
},
},
];

const result = await output.execute(inputs, config);

expect.assertions(1);
Expand All @@ -187,7 +189,13 @@ describe('lib/co2js: ', () => {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'carbon-operational': 0.034497244224,
'carbon-operational': 0.034032441600000005,
'green-web-host': false,
options: {
dataReloadRatio: 0.6,
firstVisitPercentage: 0.9,
returnVisitPercentage: 0.1,
},
},
]);
});
Expand All @@ -200,6 +208,7 @@ describe('lib/co2js: ', () => {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': true,
},
];

Expand All @@ -223,6 +232,7 @@ describe('lib/co2js: ', () => {
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'network/data/bytes': 100000,
'green-web-host': true,
},
];

Expand All @@ -242,6 +252,7 @@ describe('lib/co2js: ', () => {
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'green-web-host': true,
},
];

Expand Down
103 changes: 55 additions & 48 deletions src/lib/co2js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
# Parameters

## Plugin global config
## Plugin node config

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

## Inputs

- `network/data/bytes`: the number of bytes transferred or `network/data` if the number is in GB
- `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
- `options`: **SWD Plugin Only** an object containing any Sustainable Web Design specific variables to the changed. All keys are optional.
- `dataReloadRatio` - a value between 0 and 1 representing the percentage of data that is downloaded by return visitors. -`firstVisitPercentage` - a value between 0 and 1 representing the percentage of new visitors.
- `returnVisitPercentage` - a value between 0 and 1 representing the percentage of returning visitors.
Expand All @@ -16,16 +24,6 @@

The value for `device`, `dataCenter`, or `networks` can be a number representing the carbon intensity for the given segment (in grams per kilowatt-hour). Or, an object, which contains a key of country and a value that is an Alpha-3 ISO country code.

## Plugin node config

- `type`: supported plugins by the library, `swd` or `1byte`
- `green-web-host`: true if the website is hosted on a green web host, false otherwise

## Inputs

- `network/data/bytes`: the number of bytes transferred or `network/data` if the number is in GB
- `duration`: the amount of time the observation covers, in seconds
- `timestamp`: a timestamp for the observation

## Returns

Expand Down Expand Up @@ -58,15 +56,8 @@ initialize:
co2js:
method: Co2js
path: '@grnsft/if-unofficial-plugins'
global-config:
options:
dataReloadRatio: 0.6
firstVisitPercentage: 0.9
returnVisitPercentage: 0.1
gridIntensity:
device: 560.98
dataCenter:
country: 'TWN'
outputs:
- yaml
tree:
children:
child:
Expand All @@ -75,11 +66,19 @@ tree:
config:
co2js:
type: swd
green-web-host: true
inputs:
- timestamp: 2023-07-06T00:00
duration: 1
network/data/bytes: 1000000
green-web-host: true
options:
dataReloadRatio: 0.6
firstVisitPercentage: 0.9
returnVisitPercentage: 0.1
gridIntensity:
device: 560.98
dataCenter:
country: 'TWN'
```
You can run this by passing it to `ie`. Run impact using the following command run from the project root:
Expand All @@ -101,15 +100,8 @@ initialize:
co2js:
path: '@grnsft/if-unofficial-plugins'
method: Co2js
global-config:
options:
dataReloadRatio: 0.6
firstVisitPercentage: 0.9
returnVisitPercentage: 0.1
gridIntensity:
device: 560.98
dataCenter:
country: TWN
outputs:
- yaml
tree:
children:
child:
Expand All @@ -118,15 +110,32 @@ tree:
config:
co2js:
type: swd
green-web-host: true
inputs:
- timestamp: 2023-07-06T00:00
duration: 1
network/data/bytes: 1000000
green-web-host: true
options:
dataReloadRatio: 0.6
firstVisitPercentage: 0.9
returnVisitPercentage: 0.1
gridIntensity:
device: 560.98
dataCenter:
country: TWN
outputs:
- timestamp: 2023-07-06T00:00
duration: 1
network/data/bytes: 1000000
green-web-host: true
options:
dataReloadRatio: 0.6
firstVisitPercentage: 0.9
returnVisitPercentage: 0.1
gridIntensity:
device: 560.98
dataCenter:
country: TWN
carbon-operational: 0.34497244224000007
```

Expand All @@ -139,31 +148,29 @@ You can see example Typescript invocations for each plugin below.
```typescript
import {Co2js} from '@grnsft/if-unofficial-plugins';
const globalConfig = {
options: {
// Optional
dataReloadRatio: 0.6,
firstVisitPercentage: 0.9,
returnVisitPercentage: 0.1,
gridIntensity: {
device: 560.98,
dataCenter: 50,
networks: 437.66,
},
},
};
const co2js = Co2js(globalConfig);
const co2js = Co2js();
const results = await co2js.execute(
[
{
duration: 3600, // duration institute
timestamp: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
'network/data/bytes': 1000000, // bytes transferred
'green-web-host': true, // true if the website is hosted on a green web host, false otherwise
options: {
// Optional
dataReloadRatio: 0.6,
firstVisitPercentage: 0.9,
returnVisitPercentage: 0.1,
gridIntensity: {
device: 560.98,
dataCenter: 50,
networks: 437.66,
},
},
},
],
{
type: 'swd',
'green-web-host': true, // true if the website is hosted on a green web host, false otherwise
}
);
```
Expand All @@ -180,11 +187,11 @@ const results = await co2js.execute(
duration: 3600, // duration institute
timestamp: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
'network/data/bytes': 1000000, // bytes transferred
'green-web-host': true, // true if the website is hosted on a green web host, false otherwise
},
],
],
{
type: '1byte',
'green-web-host': true, // true if the website is hosted on a green web host, false otherwise
}
);
```
Loading

0 comments on commit f4e7367

Please sign in to comment.