Skip to content

Commit cac94b2

Browse files
committed
feat(lib): allow config and globalConfig parameters as input
Signed-off-by: alexzurbonsen <alexander.zur.bonsen@tngtech.com>
1 parent 5eba722 commit cac94b2

File tree

3 files changed

+127
-127
lines changed

3 files changed

+127
-127
lines changed

src/__tests__/unit/lib/co2js/index.test.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ describe('lib/co2js: ', () => {
3838

3939
describe('execute(): ', () => {
4040
it('returns a result when `network/data/bytes` is provided in the input.', async () => {
41-
const config = {type: '1byte', 'green-web-host': true};
41+
const config = {type: '1byte'};
4242
const inputs = [
4343
{
4444
timestamp: '2021-01-01T00:00:00Z',
4545
duration: 3600,
4646
'network/data/bytes': 100000,
47+
'green-web-host': true,
4748
},
4849
];
4950
const result = await output.execute(inputs, config);
@@ -55,19 +56,21 @@ describe('lib/co2js: ', () => {
5556
timestamp: '2021-01-01T00:00:00Z',
5657
duration: 3600,
5758
'network/data/bytes': 100000,
59+
'green-web-host': true,
5860
'carbon-operational': 0.023195833333333332,
5961
},
6062
]);
6163
});
6264

6365
it('returns the same input data when the co2 model returns undefined for the `type`.', async () => {
6466
process.env.WRONG_MODEL = 'true';
65-
const config = {type: '1byte', 'green-web-host': true};
67+
const config = {type: '1byte'};
6668
const inputs = [
6769
{
6870
timestamp: '2021-01-01T00:00:00Z',
6971
duration: 3600,
7072
'network/data/bytes': 100000,
73+
'green-web-host': true,
7174
},
7275
];
7376
const result = await output.execute(inputs, config);
@@ -79,17 +82,19 @@ describe('lib/co2js: ', () => {
7982
timestamp: '2021-01-01T00:00:00Z',
8083
duration: 3600,
8184
'network/data/bytes': 100000,
85+
'green-web-host': true,
8286
},
8387
]);
8488
});
8589

8690
it('returns a result when `network/data` is provided.', async () => {
87-
const config = {type: '1byte', 'green-web-host': true};
91+
const config = {type: '1byte'};
8892
const inputs = [
8993
{
9094
timestamp: '2021-01-01T00:00:00Z',
9195
duration: 3600,
9296
'network/data': 10,
97+
'green-web-host': true,
9398
},
9499
];
95100
const result = await output.execute(inputs, config);
@@ -101,18 +106,20 @@ describe('lib/co2js: ', () => {
101106
timestamp: '2021-01-01T00:00:00Z',
102107
duration: 3600,
103108
'network/data': 10,
109+
'green-web-host': true,
104110
'carbon-operational': 2319.583333333333,
105111
},
106112
]);
107113
});
108114

109115
it('returns a result when `green-web-host` is false.', async () => {
110-
const config = {type: '1byte', 'green-web-host': false};
116+
const config = {type: '1byte'};
111117
const inputs = [
112118
{
113119
timestamp: '2021-01-01T00:00:00Z',
114120
duration: 3600,
115121
'network/data/bytes': 100000,
122+
'green-web-host': false,
116123
},
117124
];
118125
const result = await output.execute(inputs, config);
@@ -124,19 +131,21 @@ describe('lib/co2js: ', () => {
124131
timestamp: '2021-01-01T00:00:00Z',
125132
duration: 3600,
126133
'network/data/bytes': 100000,
134+
'green-web-host': false,
127135
'carbon-operational': 0.029081299999999994,
128136
},
129137
]);
130138
});
131139

132140
it('returns a result when `type` has `swg` value in the config.', async () => {
133141
process.env.SWD_TYPE = 'true';
134-
const config = {type: 'swd', 'green-web-host': true};
142+
const config = {type: 'swd'};
135143
const inputs = [
136144
{
137145
timestamp: '2021-01-01T00:00:00Z',
138146
duration: 3600,
139147
'network/data/bytes': 100000,
148+
'green-web-host': true,
140149
},
141150
];
142151

@@ -149,35 +158,28 @@ describe('lib/co2js: ', () => {
149158
timestamp: '2021-01-01T00:00:00Z',
150159
duration: 3600,
151160
'network/data/bytes': 100000,
161+
'green-web-host': true,
152162
'carbon-operational': 0.023208995205000006,
153163
},
154164
]);
155165
});
156166

157-
it('returns a result when provided `options` in the global config.', async () => {
167+
it('returns a result when `green-web-host` and `options` are provided in input.', async () => {
158168
process.env.SWD_TYPE = 'true';
159-
const config = {type: 'swd', 'green-web-host': false};
160-
const output = Co2js({
161-
options: {
162-
dataReloadRatio: 0.6,
163-
firstVisitPercentage: 0.9,
164-
returnVisitPercentage: 0.1,
165-
gridIntensity: {
166-
device: 560.98,
167-
dataCenter: 50,
168-
networks: 437.66,
169-
},
170-
},
171-
});
172-
169+
const config = {type: 'swd'};
173170
const inputs = [
174171
{
175172
timestamp: '2021-01-01T00:00:00Z',
176173
duration: 3600,
177174
'network/data/bytes': 100000,
175+
'green-web-host': false,
176+
options: {
177+
dataReloadRatio: 0.6,
178+
firstVisitPercentage: 0.9,
179+
returnVisitPercentage: 0.1,
180+
},
178181
},
179182
];
180-
181183
const result = await output.execute(inputs, config);
182184

183185
expect.assertions(1);
@@ -187,7 +189,13 @@ describe('lib/co2js: ', () => {
187189
timestamp: '2021-01-01T00:00:00Z',
188190
duration: 3600,
189191
'network/data/bytes': 100000,
190-
'carbon-operational': 0.034497244224,
192+
'carbon-operational': 0.034032441600000005,
193+
'green-web-host': false,
194+
options: {
195+
dataReloadRatio: 0.6,
196+
firstVisitPercentage: 0.9,
197+
returnVisitPercentage: 0.1,
198+
},
191199
},
192200
]);
193201
});
@@ -200,6 +208,7 @@ describe('lib/co2js: ', () => {
200208
timestamp: '2021-01-01T00:00:00Z',
201209
duration: 3600,
202210
'network/data/bytes': 100000,
211+
'green-web-host': true,
203212
},
204213
];
205214

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

@@ -242,6 +252,7 @@ describe('lib/co2js: ', () => {
242252
{
243253
timestamp: '2021-01-01T00:00:00Z',
244254
duration: 3600,
255+
'green-web-host': true,
245256
},
246257
];
247258

src/lib/co2js/README.md

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
55
# Parameters
66

7-
## Plugin global config
7+
## Plugin node config
8+
9+
- `type`: supported plugins by the library, `swd` or `1byte`
810

11+
## Inputs
12+
13+
- `network/data/bytes`: the number of bytes transferred or `network/data` if the number is in GB
14+
- `green-web-host`: true if the website is hosted on a green web host, false otherwise
15+
- `duration`: the amount of time the observation covers, in seconds
16+
- `timestamp`: a timestamp for the observation
917
- `options`: **SWD Plugin Only** an object containing any Sustainable Web Design specific variables to the changed. All keys are optional.
1018
- `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.
1119
- `returnVisitPercentage` - a value between 0 and 1 representing the percentage of returning visitors.
@@ -16,16 +24,6 @@
1624

1725
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.
1826

19-
## Plugin node config
20-
21-
- `type`: supported plugins by the library, `swd` or `1byte`
22-
- `green-web-host`: true if the website is hosted on a green web host, false otherwise
23-
24-
## Inputs
25-
26-
- `network/data/bytes`: the number of bytes transferred or `network/data` if the number is in GB
27-
- `duration`: the amount of time the observation covers, in seconds
28-
- `timestamp`: a timestamp for the observation
2927

3028
## Returns
3129

@@ -58,15 +56,8 @@ initialize:
5856
co2js:
5957
method: Co2js
6058
path: '@grnsft/if-unofficial-plugins'
61-
global-config:
62-
options:
63-
dataReloadRatio: 0.6
64-
firstVisitPercentage: 0.9
65-
returnVisitPercentage: 0.1
66-
gridIntensity:
67-
device: 560.98
68-
dataCenter:
69-
country: 'TWN'
59+
outputs:
60+
- yaml
7061
tree:
7162
children:
7263
child:
@@ -75,11 +66,19 @@ tree:
7566
config:
7667
co2js:
7768
type: swd
78-
green-web-host: true
7969
inputs:
8070
- timestamp: 2023-07-06T00:00
8171
duration: 1
8272
network/data/bytes: 1000000
73+
green-web-host: true
74+
options:
75+
dataReloadRatio: 0.6
76+
firstVisitPercentage: 0.9
77+
returnVisitPercentage: 0.1
78+
gridIntensity:
79+
device: 560.98
80+
dataCenter:
81+
country: 'TWN'
8382
```
8483
8584
You can run this by passing it to `ie`. Run impact using the following command run from the project root:
@@ -101,15 +100,8 @@ initialize:
101100
co2js:
102101
path: '@grnsft/if-unofficial-plugins'
103102
method: Co2js
104-
global-config:
105-
options:
106-
dataReloadRatio: 0.6
107-
firstVisitPercentage: 0.9
108-
returnVisitPercentage: 0.1
109-
gridIntensity:
110-
device: 560.98
111-
dataCenter:
112-
country: TWN
103+
outputs:
104+
- yaml
113105
tree:
114106
children:
115107
child:
@@ -118,15 +110,32 @@ tree:
118110
config:
119111
co2js:
120112
type: swd
121-
green-web-host: true
122113
inputs:
123114
- timestamp: 2023-07-06T00:00
124115
duration: 1
125116
network/data/bytes: 1000000
117+
green-web-host: true
118+
options:
119+
dataReloadRatio: 0.6
120+
firstVisitPercentage: 0.9
121+
returnVisitPercentage: 0.1
122+
gridIntensity:
123+
device: 560.98
124+
dataCenter:
125+
country: TWN
126126
outputs:
127127
- timestamp: 2023-07-06T00:00
128128
duration: 1
129129
network/data/bytes: 1000000
130+
green-web-host: true
131+
options:
132+
dataReloadRatio: 0.6
133+
firstVisitPercentage: 0.9
134+
returnVisitPercentage: 0.1
135+
gridIntensity:
136+
device: 560.98
137+
dataCenter:
138+
country: TWN
130139
carbon-operational: 0.34497244224000007
131140
```
132141

@@ -139,31 +148,29 @@ You can see example Typescript invocations for each plugin below.
139148
```typescript
140149
import {Co2js} from '@grnsft/if-unofficial-plugins';
141150
142-
const globalConfig = {
143-
options: {
144-
// Optional
145-
dataReloadRatio: 0.6,
146-
firstVisitPercentage: 0.9,
147-
returnVisitPercentage: 0.1,
148-
gridIntensity: {
149-
device: 560.98,
150-
dataCenter: 50,
151-
networks: 437.66,
152-
},
153-
},
154-
};
155-
const co2js = Co2js(globalConfig);
151+
const co2js = Co2js();
156152
const results = await co2js.execute(
157153
[
158154
{
159155
duration: 3600, // duration institute
160156
timestamp: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
161157
'network/data/bytes': 1000000, // bytes transferred
158+
'green-web-host': true, // true if the website is hosted on a green web host, false otherwise
159+
options: {
160+
// Optional
161+
dataReloadRatio: 0.6,
162+
firstVisitPercentage: 0.9,
163+
returnVisitPercentage: 0.1,
164+
gridIntensity: {
165+
device: 560.98,
166+
dataCenter: 50,
167+
networks: 437.66,
168+
},
169+
},
162170
},
163171
],
164172
{
165173
type: 'swd',
166-
'green-web-host': true, // true if the website is hosted on a green web host, false otherwise
167174
}
168175
);
169176
```
@@ -180,11 +187,11 @@ const results = await co2js.execute(
180187
duration: 3600, // duration institute
181188
timestamp: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
182189
'network/data/bytes': 1000000, // bytes transferred
190+
'green-web-host': true, // true if the website is hosted on a green web host, false otherwise
183191
},
184-
],
192+
],
185193
{
186194
type: '1byte',
187-
'green-web-host': true, // true if the website is hosted on a green web host, false otherwise
188195
}
189196
);
190197
```

0 commit comments

Comments
 (0)