From 6cc0c7910509461ed927c07a955e91b4887a21a9 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 new co2js version comes with updated average annual grid intensities. Tests fail. Fix by ignoring decimal places. co2js plugin reports the carbon operational value with a hilarious number of decimal places. There are so many assumptions involved that the error must be quite high. Not sure how high, but restricting to 3 decimal places seems more than enough. 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 | 17 +++++++++++------ 2 files changed, 17 insertions(+), 12 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..70ec7d6 100644 --- a/src/lib/co2js/index.ts +++ b/src/lib/co2js/index.ts @@ -41,7 +41,7 @@ export const Co2js = PluginFactory({ return result ? { ...input, - 'carbon-operational': result, + 'carbon-operational': roundToDecimalPlaces(result, 3), } : 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; +};