Skip to content

Commit

Permalink
separate options parsing for byteTrace and visitTrace functions
Browse files Browse the repository at this point in the history
Signed-off-by: alexzurbonsen <alexander.zur.bonsen@tngtech.com>
  • Loading branch information
alexzurbonsen committed Sep 8, 2024
1 parent 870e621 commit 3d4fec7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
19 changes: 13 additions & 6 deletions src/co2.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ import SustainableWebDesignV3 from "./sustainable-web-design-v3.js";
import SustainableWebDesignV4 from "./sustainable-web-design-v4.js";

import {
GLOBAL_GRID_INTENSITY,
RENEWABLES_GRID_INTENSITY,
} from "./constants/index.js";
import { parseOptions } from "./helpers/index.js";
parseByteTraceOptions,
parseVisitTraceOptions,
} from "./helpers/index.js";

class CO2 {
constructor(options) {
Expand Down Expand Up @@ -152,7 +151,11 @@ class CO2 {
* @return {CO2EstimateTraceResultPerByte} the amount of CO2 in grammes
*/
perByteTrace(bytes, green = false, options = {}) {
const adjustments = parseOptions(options, this.model.version, green);
const adjustments = parseByteTraceOptions(
options,
this.model.version,
green
);

// Filter out the trace items that aren't relevant to this function.
const { gridIntensity, ...traceVariables } = adjustments;
Expand Down Expand Up @@ -197,7 +200,11 @@ class CO2 {
*/
perVisitTrace(bytes, green = false, options = {}) {
if (this.model?.perVisit) {
const adjustments = parseOptions(options, this.model.version, green);
const adjustments = parseVisitTraceOptions(
options,
this.model.version,
green
);
const { gridIntensity, ...variables } = adjustments;

return {
Expand Down
70 changes: 41 additions & 29 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const formatNumber = (num) => parseFloat(num.toFixed(2));

const lessThanEqualTo = (num, limit) => num <= limit;

function parseOptions(options = {}, version = 3, green = false) {
function parseByteTraceOptions(options = {}, version = 3, green = false) {
const globalGridIntensity =
version === 4 ? SWDM4_GLOBAL_GRID_INTENSITY : SWDM3_GLOBAL_GRID_INTENSITY;
// CHeck that it is an object
Expand Down Expand Up @@ -131,6 +131,44 @@ function parseOptions(options = {}, version = 3, green = false) {
};
}

if (
options?.greenHostingFactor ||
(options.greenHostingFactor === 0 && version === 4)
) {
if (typeof options.greenHostingFactor === "number") {
if (options.greenHostingFactor >= 0 && options.greenHostingFactor <= 1) {
adjustments.greenHostingFactor = options.greenHostingFactor;
} else {
adjustments.greenHostingFactor = 0;
console.warn(
`The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \nFalling back to default value.`
);
}
} else {
adjustments.greenHostingFactor = 0;
console.warn(
`The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \nFalling back to default value.`
);
}
} else if (version === 4) {
adjustments.greenHostingFactor = 0;
}

if (green) {
adjustments.greenHostingFactor = 1;
}

return adjustments;
}

function parseVisitTraceOptions(options = {}, version = 3, green = false) {
// CHeck that it is an object
if (typeof options !== "object") {
throw new Error("Options must be an object");
}

const adjustments = parseByteTraceOptions(options, version, green);

if (options?.dataReloadRatio || options.dataReloadRatio === 0) {
if (typeof options.dataReloadRatio === "number") {
if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) {
Expand Down Expand Up @@ -215,33 +253,6 @@ function parseOptions(options = {}, version = 3, green = false) {
);
}

if (
options?.greenHostingFactor ||
(options.greenHostingFactor === 0 && version === 4)
) {
if (typeof options.greenHostingFactor === "number") {
if (options.greenHostingFactor >= 0 && options.greenHostingFactor <= 1) {
adjustments.greenHostingFactor = options.greenHostingFactor;
} else {
adjustments.greenHostingFactor = 0;
console.warn(
`The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \nFalling back to default value.`
);
}
} else {
adjustments.greenHostingFactor = 0;
console.warn(
`The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \nFalling back to default value.`
);
}
} else if (version === 4) {
adjustments.greenHostingFactor = 0;
}

if (green) {
adjustments.greenHostingFactor = 1;
}

return adjustments;
}

Expand Down Expand Up @@ -299,7 +310,8 @@ function outputRating(co2e, swdmVersion) {

export {
formatNumber,
parseOptions,
parseByteTraceOptions,
parseVisitTraceOptions,
getApiRequestHeaders,
lessThanEqualTo,
outputRating,
Expand Down

0 comments on commit 3d4fec7

Please sign in to comment.