generated from Green-Software-Foundation/if-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
48 lines (41 loc) · 1.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {PluginInterface, PluginParams} from '../types/interface';
import {SECONDS_IN_YEAR} from '../constants';
import {z} from 'zod';
import {validate} from '../util/validations';
/**
* Based on Ramboll and Resilio paper available here: https://op.europa.eu/en/publication-detail/-/publication/d3b6c0a1-1171-11ee-b12e-01aa75ed71a1
*/
export const RambollResilio = (): PluginInterface => {
const metadata = {
kind: 'execute',
};
const kWhPerGb = 0.147;
/**
* Execute's strategy description here.
*/
const execute = async (inputs: PluginParams[]): Promise<PluginParams[]> => {
return inputs.map(input => {
const safeInput = validateInput(input);
const durationInYears = safeInput.duration / SECONDS_IN_YEAR;
const dataStoredGb = safeInput['storage/data-stored'];
return {
...input,
'storage/energy': durationInYears * dataStoredGb * kWhPerGb,
};
});
};
/**
* Checks for required fields in input.
*/
const validateInput = (input: PluginParams) => {
const schema = z.object({
duration: z.number().positive(),
'storage/data-stored': z.number().positive(),
});
return validate<z.infer<typeof schema>>(schema, input);
};
return {
metadata,
execute,
};
};