Skip to content

Commit

Permalink
chore(co2js): fix failing tests
Browse files Browse the repository at this point in the history
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 <alexander.zur.bonsen@tngtech.com>
  • Loading branch information
alexzurbonsen committed Nov 27, 2024
1 parent a6ce1da commit 6cc0c79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/__tests__/unit/lib/co2js/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
]);
});
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('lib/co2js: ', () => {
duration: 3600,
'network/data': 10,
'green-web-host': true,
'carbon-operational': 2319.583333333333,
'carbon-operational': 2319.583,
},
]);
});
Expand All @@ -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,
},
]);
});
Expand Down Expand Up @@ -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,
},
]);
});
Expand Down Expand Up @@ -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,
},
]);
});
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions src/lib/co2js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const Co2js = PluginFactory({
return result
? {
...input,
'carbon-operational': result,
'carbon-operational': roundToDecimalPlaces(result, 3),
}
: input;
});
Expand Down Expand Up @@ -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;
},
};

Expand All @@ -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;
};

0 comments on commit 6cc0c79

Please sign in to comment.