From 1d1e38be68d5de2f0ebdb1748865c1dc2aa17d81 Mon Sep 17 00:00:00 2001 From: alexzurbonsen Date: Thu, 28 Nov 2024 00:35:02 +0100 Subject: [PATCH] chore(co2js): fix failing tests co2js plugin reports the carbon operational value with a hilarious number of decimal places. There are so many assumptions involved that the error is must be quite high. Not sure how high, but restricting to 3 decimal places max. Also remove any type for the model. Signed-off-by: alexzurbonsen --- src/__tests__/unit/lib/co2js/index.test.ts | 12 +++++------ src/lib/co2js/index.ts | 25 +++++++++++++--------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/__tests__/unit/lib/co2js/index.test.ts b/src/__tests__/unit/lib/co2js/index.test.ts index 1ca8e08..21c316c 100644 --- a/src/__tests__/unit/lib/co2js/index.test.ts +++ b/src/__tests__/unit/lib/co2js/index.test.ts @@ -79,7 +79,7 @@ describe('lib/co2js: ', () => { duration: 3600, 'network/data/bytes': 100000, 'green-web-host': true, - 'carbon-operational': 0.023195833333333332, + 'carbon-operational': 0.023, }, ]); }); @@ -131,7 +131,7 @@ describe('lib/co2js: ', () => { duration: 3600, 'network/data': 10, 'green-web-host': true, - 'carbon-operational': 2319.583333333333, + 'carbon-operational': 2319.583, }, ]); }); @@ -157,7 +157,7 @@ describe('lib/co2js: ', () => { duration: 3600, 'network/data/bytes': 100000, 'green-web-host': false, - 'carbon-operational': 0.029081299999999994, + 'carbon-operational': 0.029, }, ]); }); @@ -186,7 +186,7 @@ describe('lib/co2js: ', () => { duration: 3600, 'network/data/bytes': 100000, 'green-web-host': true, - 'carbon-operational': 0.0254956723875, + 'carbon-operational': 0.025, }, ]); }); @@ -215,7 +215,7 @@ describe('lib/co2js: ', () => { duration: 3600, 'network/data/bytes': 100000, 'green-web-host': true, - 'carbon-operational': 0.012103000000000001, + 'carbon-operational': 0.012, }, ]); }); @@ -246,7 +246,7 @@ describe('lib/co2js: ', () => { timestamp: '2021-01-01T00:00:00Z', duration: 3600, 'network/data/bytes': 100000, - 'carbon-operational': 0.037453104, + 'carbon-operational': 0.037, 'green-web-host': false, options: { dataReloadRatio: 0.6, diff --git a/src/lib/co2js/index.ts b/src/lib/co2js/index.ts index ae7425c..880b86a 100644 --- a/src/lib/co2js/index.ts +++ b/src/lib/co2js/index.ts @@ -21,7 +21,7 @@ export const Co2js = PluginFactory({ configValidation: ( config: ConfigParams, // eslint-disable-next-line @typescript-eslint/no-unused-vars - _input: PluginParams | undefined, + _input: PluginParams | undefined ) => { const {validateConfig} = Co2jsUtils(); return validateConfig(config); @@ -41,7 +41,7 @@ export const Co2js = PluginFactory({ return result ? { ...input, - 'carbon-operational': result, + 'carbon-operational': roundToDecimalPlaces(result, 3), } : input; }); @@ -80,7 +80,7 @@ const Co2jsUtils = () => { }, { message: '`version` can only be provided with `type` swd', - }, + } ); return validate>(schema, config); @@ -129,7 +129,7 @@ const Co2jsUtils = () => { data['green-web-host'] !== undefined, { message: `\`green-web-host\` is provided neither in config nor in input.\nConfig: ${config}\nInput: ${input}`, - }, + } ) .refine( data => @@ -137,7 +137,7 @@ const Co2jsUtils = () => { data['green-web-host'] === undefined, { message: `\`green-web-host\` is provided in config and in input. Please only provide once.\nConfig: ${config}\nInput: ${input}`, - }, + } ); return validate>(inputSchema, input); @@ -148,21 +148,21 @@ const Co2jsUtils = () => { */ const calculateResultByParams = ( inputWithConfig: PluginParams, - model: any, + model: co2 ) => { const greenhosting = inputWithConfig['green-web-host'] === true; const options = inputWithConfig['options']; const GBinBytes = inputWithConfig['network/data'] * 1000 * 1000 * 1000; const bytes = inputWithConfig['network/data/bytes'] || GBinBytes; - const paramType: {[key: string]: () => string} = { + const paramType: {[key: string]: () => number} = { swd: () => { return options - ? model.perVisitTrace(bytes, greenhosting, options).co2 - : model.perVisit(bytes, greenhosting); + ? (model.perVisitTrace(bytes, greenhosting, options).co2 as number) + : (model.perVisit(bytes, greenhosting) as number); }, '1byte': () => { - return model.perByte(bytes, greenhosting); + return model.perByte(bytes, greenhosting) as number; }, }; @@ -175,3 +175,8 @@ const Co2jsUtils = () => { calculateResultByParams, }; }; + +const roundToDecimalPlaces = (num: number, decimalPlaces: number) => { + const factor = Math.pow(10, decimalPlaces); + return Math.round(num * factor) / factor; +};