Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x | import {z} from 'zod';
import {validateArithmeticExpression} from '@grnsft/if-core/utils';
import {ConfigParams, PluginParams} from '@grnsft/if-core/types';
import {PluginFactory} from '@grnsft/if-core/interfaces';
export const SciEmbodied = PluginFactory({
metadata: {
inputs: {
vCPUs: {
description: 'number of CPUs allocated to an application',
unit: 'CPUs',
'aggregation-method': {
time: 'copy',
component: 'copy',
},
},
memory: {
description: 'RAM available for a resource, in GB',
unit: 'GB',
'aggregation-method': {
time: 'copy',
component: 'copy',
},
},
ssd: {
description: 'number of SSDs available for a resource',
unit: 'SSDs',
'aggregation-method': {
time: 'copy',
component: 'copy',
},
},
hdd: {
description: 'number of HDDs available for a resource',
unit: 'HDDs',
'aggregation-method': {
time: 'copy',
component: 'copy',
},
},
gpu: {
description: 'number of GPUs available for a resource',
unit: 'GPUs',
'aggregation-method': {
time: 'copy',
component: 'copy',
},
},
'usage-ratio': {
description:
'a scaling factor that can be used to describe the ratio of actual resource usage comapred to real device usage, e.g. 0.25 if you are using 2 out of 8 vCPUs, 0.1 if you are responsible for 1 out of 10 GB of storage, etc',
unit: 'dimensionless',
'aggregation-method': {
time: 'copy',
component: 'copy',
},
},
time: {
description:
'a time unit to scale the embodied carbon by, in seconds. If not provided,time defaults to the value of the timestep duration.',
unit: 'seconds',
'aggregation-method': {
time: 'copy',
component: 'copy',
},
},
},
outputs: {
'embodied-carbon': {
description: 'embodied carbon for a resource, scaled by usage',
unit: 'gCO2eq',
'aggregation-method': {
time: 'sum',
component: 'sum',
},
},
},
},
configValidation: z.object({
'baseline-vcpus': z.preprocess(
value => validateArithmeticExpression('baseline-vcpus', value, 'number'),
z.number().gte(0).default(1)
),
'baseline-memory': z.preprocess(
value => validateArithmeticExpression('baseline-memory', value, 'number'),
z.number().gte(0).default(16)
),
'baseline-emissions': z.preprocess(
value =>
validateArithmeticExpression('baseline-emissions', value, 'number'),
z.number().gte(0).default(1000000)
),
lifespan: z.preprocess(
value => validateArithmeticExpression('lifespan', value, 'number'),
z.number().gt(0).default(126144000)
),
'vcpu-emissions-constant': z.preprocess(
value =>
validateArithmeticExpression(
'vcpu-emissions-constant',
value,
'number'
),
z.number().gte(0).default(100000)
),
'memory-emissions-constant': z.preprocess(
value =>
validateArithmeticExpression(
'memory-emissions-constant',
value,
'number'
),
z
.number()
.gte(0)
.default(533 / 384)
),
'ssd-emissions-constant': z.preprocess(
value =>
validateArithmeticExpression('ssd-emissions-constant', value, 'number'),
z.number().gte(0).default(50000)
),
'hdd-emissions-constant': z.preprocess(
value =>
validateArithmeticExpression('hdd-emissions-constant', value, 'number'),
z.number().gte(0).default(100000)
),
'gpu-emissions-constant': z.preprocess(
value =>
validateArithmeticExpression('gpu-emissions-constant', value, 'number'),
z.number().gte(0).default(150000)
),
'output-parameter': z.string().optional(),
}),
inputValidation: z.object({
duration: z.number().gt(0),
vCPUs: z.number().gt(0).default(1),
memory: z.number().gt(0).default(16),
ssd: z.number().gte(0).default(0),
hdd: z.number().gte(0).default(0),
gpu: z.number().gte(0).default(0),
'usage-ratio': z.number().gt(0).default(1),
time: z.number().gt(0).optional(),
}),
implementation: async (inputs: PluginParams[], config: ConfigParams) => {
/**
* 1. Validates configuration and assigns defaults values if not provided.
* 2. Maps through observations and validates them.
* 3. Calculates total embodied carbon by substracting and the difference between baseline server and given one.
*/
return inputs.map(input => {
const cpuE =
(input.vCPUs - config['baseline-vcpus']) *
config['vcpu-emissions-constant'];
const memoryE =
(input.memory - config['baseline-memory']) *
((config['memory-emissions-constant'] * config['baseline-memory']) /
16) *
1000;
const hddE = input.hdd * config['hdd-emissions-constant'];
const gpuE = input.gpu * config['gpu-emissions-constant'];
const ssdE = input.ssd * config['ssd-emissions-constant'];
const time = input['time'] || input.duration;
const totalEmbodied =
config['baseline-emissions'] + cpuE + memoryE + ssdE + hddE + gpuE;
const totalEmbodiedScaledByUsage = totalEmbodied * input['usage-ratio'];
const totalEmbodiedScaledByUsageAndTime =
totalEmbodiedScaledByUsage * (time / config['lifespan']);
const embodiedCarbonKey = config['output-parameter'] || 'embodied-carbon';
return {
...input,
[embodiedCarbonKey]: totalEmbodiedScaledByUsageAndTime,
};
});
},
allowArithmeticExpressions: [
'baseline-vcpus',
'baseline-memory',
'baseline-emissions',
'lifespan',
'vcpu-emissions-constant',
'memory-emissions-constant',
'ssd-emissions-constant',
'hdd-emissions-constant',
'gpu-emissions-constant',
],
});
|