Skip to content

Commit

Permalink
Add support for values equal to 0 in perByteTrace and perVisitTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
p-gerard committed Oct 6, 2023
1 parent b56091a commit a045b14
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
18 changes: 9 additions & 9 deletions src/co2.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ class CO2 {
description:
"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",
network:
adjustments?.gridIntensity?.network?.value || GLOBAL_GRID_INTENSITY,
adjustments?.gridIntensity?.network?.value ?? GLOBAL_GRID_INTENSITY,
dataCenter: green
? RENEWABLES_GRID_INTENSITY
: adjustments?.gridIntensity?.dataCenter?.value ||
: adjustments?.gridIntensity?.dataCenter?.value ??
GLOBAL_GRID_INTENSITY,
production: GLOBAL_GRID_INTENSITY,
device:
adjustments?.gridIntensity?.device?.value || GLOBAL_GRID_INTENSITY,
adjustments?.gridIntensity?.device?.value ?? GLOBAL_GRID_INTENSITY,
},
},
};
Expand Down Expand Up @@ -164,20 +164,20 @@ class CO2 {
description:
"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",
network:
adjustments?.gridIntensity?.network?.value ||
adjustments?.gridIntensity?.network?.value ??
GLOBAL_GRID_INTENSITY,
dataCenter: green
? RENEWABLES_GRID_INTENSITY
: adjustments?.gridIntensity?.dataCenter?.value ||
: adjustments?.gridIntensity?.dataCenter?.value ??
GLOBAL_GRID_INTENSITY,
production: GLOBAL_GRID_INTENSITY,
device:
adjustments?.gridIntensity?.device?.value ||
adjustments?.gridIntensity?.device?.value ??
GLOBAL_GRID_INTENSITY,
},
dataReloadRatio: adjustments?.dataReloadRatio || 0.02,
firstVisitPercentage: adjustments?.firstVisitPercentage || 0.75,
returnVisitPercentage: adjustments?.returnVisitPercentage || 0.25,
dataReloadRatio: adjustments?.dataReloadRatio ?? 0.02,
firstVisitPercentage: adjustments?.firstVisitPercentage ?? 0.75,
returnVisitPercentage: adjustments?.returnVisitPercentage ?? 0.25,
},
};
} else {
Expand Down
42 changes: 42 additions & 0 deletions src/co2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,46 @@ describe("co2", () => {
).toBeLessThan(MILLION_PERVISIT_GREY);
});
});

describe("Using values equal to 0", () => {
const co2 = new CO2();

it("expects perByteTrace to support values equal to 0", () => {
const perByteTraceResult = co2.perByteTrace(1000000, false, {
gridIntensity: {
dataCenter: 0,
network: 0,
device: 0,
},
});
const { dataCenter, network, device } =
perByteTraceResult.variables.gridIntensity;
expect(dataCenter).toBe(0);
expect(network).toBe(0);
expect(device).toBe(0);
});

it("expects perVisitTrace to support values equal to 0", () => {
const perVisitTraceResult = co2.perVisitTrace(1000000, false, {
dataReloadRatio: 0,
returnVisitPercentage: 0,
firstVisitPercentage: 0,
gridIntensity: {
dataCenter: 0,
network: 0,
device: 0,
},
});
const { dataReloadRatio, firstVisitPercentage, returnVisitPercentage } =
perVisitTraceResult.variables;
const { dataCenter, network, device } =
perVisitTraceResult.variables.gridIntensity;
expect(dataReloadRatio).toBe(0);
expect(firstVisitPercentage).toBe(0);
expect(returnVisitPercentage).toBe(0);
expect(dataCenter).toBe(0);
expect(network).toBe(0);
expect(device).toBe(0);
});
});
});
12 changes: 6 additions & 6 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function parseOptions(options) {
if (options?.gridIntensity) {
adjustments.gridIntensity = {};
const { device, dataCenter, network } = options.gridIntensity;
if (device) {
if (device || device === 0) {
if (typeof device === "object") {
if (!averageIntensity.data[device.country?.toUpperCase()]) {
console.warn(
Expand Down Expand Up @@ -47,7 +47,7 @@ function parseOptions(options) {
);
}
}
if (dataCenter) {
if (dataCenter || dataCenter === 0) {
if (typeof dataCenter === "object") {
if (!averageIntensity.data[dataCenter.country?.toUpperCase()]) {
console.warn(
Expand Down Expand Up @@ -76,7 +76,7 @@ function parseOptions(options) {
);
}
}
if (network) {
if (network || network === 0) {
if (typeof network === "object") {
if (!averageIntensity.data[network.country?.toUpperCase()]) {
console.warn(
Expand Down Expand Up @@ -107,7 +107,7 @@ function parseOptions(options) {
}
}

if (options?.dataReloadRatio) {
if (options?.dataReloadRatio || options.dataReloadRatio === 0) {
if (typeof options.dataReloadRatio === "number") {
if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) {
adjustments.dataReloadRatio = options.dataReloadRatio;
Expand All @@ -127,7 +127,7 @@ function parseOptions(options) {
}
}

if (options?.firstVisitPercentage) {
if (options?.firstVisitPercentage || options.firstVisitPercentage === 0) {
if (typeof options.firstVisitPercentage === "number") {
if (
options.firstVisitPercentage >= 0 &&
Expand All @@ -148,7 +148,7 @@ function parseOptions(options) {
}
}

if (options?.returnVisitPercentage) {
if (options?.returnVisitPercentage || options.returnVisitPercentage === 0) {
if (typeof options.returnVisitPercentage === "number") {
if (
options.returnVisitPercentage >= 0 &&
Expand Down

0 comments on commit a045b14

Please sign in to comment.