Skip to content

Commit 58d6fc7

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

File tree

3 files changed

+174
-116
lines changed

3 files changed

+174
-116
lines changed

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

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,21 @@ describe('lib/co2js: ', () => {
8686
]);
8787
});
8888

89-
it('returns a result when provided `options` in the global config.', async () => {
90-
const config = {type: 'swd', 'green-web-host': false};
91-
const output = Co2js({
92-
options: {
93-
dataReloadRatio: 0.6,
94-
firstVisitPercentage: 0.9,
95-
returnVisitPercentage: 0.1,
96-
gridIntensity: {
97-
device: 560.98,
98-
dataCenter: 50,
99-
networks: 437.66,
100-
},
101-
},
102-
});
103-
89+
it('returns a result when `green-web-host` and `options` are provided in input.', async () => {
90+
const config = {type: 'swd'};
10491
const inputs = [
10592
{
10693
timestamp: '2021-01-01T00:00:00Z',
10794
duration: 3600,
10895
'network/data/bytes': 100000,
96+
'green-web-host': false,
97+
options: {
98+
dataReloadRatio: 0.6,
99+
firstVisitPercentage: 0.9,
100+
returnVisitPercentage: 0.1,
101+
},
109102
},
110103
];
111-
112104
const result = await output.execute(inputs, config);
113105

114106
expect.assertions(1);
@@ -118,7 +110,13 @@ describe('lib/co2js: ', () => {
118110
timestamp: '2021-01-01T00:00:00Z',
119111
duration: 3600,
120112
'network/data/bytes': 100000,
121-
'carbon-operational': 0.034497244224,
113+
'carbon-operational': 0.034032441600000005,
114+
'green-web-host': false,
115+
options: {
116+
dataReloadRatio: 0.6,
117+
firstVisitPercentage: 0.9,
118+
returnVisitPercentage: 0.1,
119+
},
122120
},
123121
]);
124122
});
@@ -169,5 +167,54 @@ describe('lib/co2js: ', () => {
169167
}
170168
});
171169
});
170+
171+
it('throws an error if `green-web-host` is neither provided in config nor in input.', async () => {
172+
const config = {type: '1byte'};
173+
const inputs = [
174+
{
175+
timestamp: '2021-01-01T00:00:00Z',
176+
duration: 3600,
177+
'network/data/bytes': 100000,
178+
},
179+
];
180+
181+
expect.assertions(2);
182+
183+
try {
184+
await output.execute(inputs, config);
185+
} catch (error) {
186+
expect(error).toEqual(
187+
new InputValidationError(
188+
`\`green-web-host\` is provided neither in config nor in input.\nConfig: ${config}\nInput: ${inputs[0]}`
189+
)
190+
);
191+
expect(error).toBeInstanceOf(InputValidationError);
192+
}
193+
});
194+
195+
it('throws an error if `green-web-host` is provided in config and input.', async () => {
196+
const config = {type: '1byte', 'green-web-host': true};
197+
const inputs = [
198+
{
199+
timestamp: '2021-01-01T00:00:00Z',
200+
duration: 3600,
201+
'network/data/bytes': 100000,
202+
'green-web-host': true,
203+
},
204+
];
205+
206+
expect.assertions(2);
207+
208+
try {
209+
await output.execute(inputs, config);
210+
} catch (error) {
211+
expect(error).toEqual(
212+
new InputValidationError(
213+
`\`green-web-host\` is provided in config and in input. Please only provide once.\nConfig: ${config}\nInput: ${inputs[0]}`
214+
)
215+
);
216+
expect(error).toBeInstanceOf(InputValidationError);
217+
}
218+
});
172219
});
173220
});

src/lib/co2js/README.md

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@
44
55
# Parameters
66

7-
## Plugin global config
8-
9-
- `options`: **SWD Plugin Only** an object containing any Sustainable Web Design specific variables to the changed. All keys are optional.
10-
- `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.
11-
- `returnVisitPercentage` - a value between 0 and 1 representing the percentage of returning visitors.
12-
- `gridIntensity` - an object that can contain the following optional keys:
13-
- `device` - 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.
14-
- `dataCenter` - 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.
15-
- `networks` - 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.
16-
17-
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.
18-
197
## Plugin node config
208

219
- `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
10+
- `green-web-host`: true if the website is hosted on a green web host, false otherwise (has to be provided either as config or input)
2311

2412
## Inputs
2513

2614
- `network/data/bytes`: the number of bytes transferred or `network/data` if the number is in GB
15+
- `green-web-host`: true if the website is hosted on a green web host, false otherwise (has to be provided either as config or input)
2716
- `duration`: the amount of time the observation covers, in seconds
2817
- `timestamp`: a timestamp for the observation
18+
- `options`: **SWD Plugin Only** an object containing any Sustainable Web Design specific variables to the changed. All keys are optional.
19+
- `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.
20+
- `returnVisitPercentage` - a value between 0 and 1 representing the percentage of returning visitors.
21+
- `gridIntensity` - an object that can contain the following optional keys:
22+
- `device` - 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.
23+
- `dataCenter` - 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.
24+
- `networks` - 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.
25+
26+
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.
27+
2928

3029
## Returns
3130

@@ -58,15 +57,6 @@ initialize:
5857
co2js:
5958
method: Co2js
6059
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'
7060
tree:
7161
children:
7262
child:
@@ -80,6 +70,14 @@ tree:
8070
- timestamp: 2023-07-06T00:00
8171
duration: 1
8272
network/data/bytes: 1000000
73+
options:
74+
dataReloadRatio: 0.6
75+
firstVisitPercentage: 0.9
76+
returnVisitPercentage: 0.1
77+
gridIntensity:
78+
device: 560.98
79+
dataCenter:
80+
country: 'TWN'
8381
```
8482
8583
You can run this by passing it to `ie`. Run impact using the following command run from the project root:
@@ -101,15 +99,6 @@ initialize:
10199
co2js:
102100
path: '@grnsft/if-unofficial-plugins'
103101
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
113102
tree:
114103
children:
115104
child:
@@ -123,11 +112,28 @@ tree:
123112
- timestamp: 2023-07-06T00:00
124113
duration: 1
125114
network/data/bytes: 1000000
115+
options:
116+
dataReloadRatio: 0.6
117+
firstVisitPercentage: 0.9
118+
returnVisitPercentage: 0.1
119+
gridIntensity:
120+
device: 560.98
121+
dataCenter:
122+
country: TWN
126123
outputs:
127124
- timestamp: 2023-07-06T00:00
128125
duration: 1
129126
network/data/bytes: 1000000
130127
carbon-operational: 0.34497244224000007
128+
green-web-host: true
129+
options:
130+
dataReloadRatio: 0.6
131+
firstVisitPercentage: 0.9
132+
returnVisitPercentage: 0.1
133+
gridIntensity:
134+
device: 560.98
135+
dataCenter:
136+
country: TWN
131137
```
132138

133139
## TypeScript
@@ -139,26 +145,24 @@ You can see example Typescript invocations for each plugin below.
139145
```typescript
140146
import {Co2js} from '@grnsft/if-unofficial-plugins';
141147
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);
148+
const co2js = Co2js();
156149
const results = await co2js.execute(
157150
[
158151
{
159152
duration: 3600, // duration institute
160153
timestamp: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
161154
'network/data/bytes': 1000000, // bytes transferred
155+
options: {
156+
// Optional
157+
dataReloadRatio: 0.6,
158+
firstVisitPercentage: 0.9,
159+
returnVisitPercentage: 0.1,
160+
gridIntensity: {
161+
device: 560.98,
162+
dataCenter: 50,
163+
networks: 437.66,
164+
},
165+
},
162166
},
163167
],
164168
{

0 commit comments

Comments
 (0)