Skip to content

Commit

Permalink
Merge pull request #653 from ethersphere/fix-10-15-24
Browse files Browse the repository at this point in the history
correction to cost calculations
  • Loading branch information
NoahMaizels authored Oct 14, 2024
2 parents d57e877 + 058f599 commit 80f4881
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/components/RedundancyCalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export default function UploadCostCalc() {
const formatNumberCustom = (num) => {
const isScientific = Math.abs(num) > 0 && Math.abs(num) < 0.0001;
let formattedNum = isScientific ? num.toExponential(2) : num.toFixed(2);

return formattedNum;
};

Expand All @@ -43,6 +42,7 @@ export default function UploadCostCalc() {
setUnit(e.target.value);
};


const calculateCost = () => {
setErrorMessage("");
setResult([]);
Expand All @@ -51,8 +51,15 @@ export default function UploadCostCalc() {
setErrorMessage("Please select a redundancy level.");
return;
}

let parityDataInGb = 0;
let totalChunks, sizeInKb, sizeInGb;

let totalChunks, sizeInKb, sizeInGb, parityDataInGb;
if (!dataSize || isNaN(parseFloat(dataSize))) {
setErrorMessage("Please enter a valid data size.");
return;
}

if (unit === "kb") {
const kbValue = parseFloat(dataSize);
if (isNaN(kbValue) || kbValue <= 0) {
Expand Down Expand Up @@ -83,23 +90,29 @@ export default function UploadCostCalc() {
const redundancyLevels = { Medium: 0, Strong: 1, Insane: 2, Paranoid: 3 };
const redundancyLevel = redundancyLevels[redundancy];

const quotient = isEncrypted ? Math.floor(totalChunks / maxChunksEncrypted[redundancyLevel]) : Math.floor(totalChunks / maxChunks[redundancyLevel]);
const remainder = isEncrypted ? totalChunks % maxChunksEncrypted[redundancyLevel] : totalChunks % maxChunks[redundancyLevel];
const remainderIndex = (remainder - 1 < 0) ? 0 : (remainder - 1);
const selectedParities = isEncrypted ? paritiesEncrypted : parities;
const remainderParities = selectedParities[redundancyLevel][remainderIndex] || 0;
const totalParities = (quotient * maxParities[redundancyLevel]) + remainderParities;
const quotient = isEncrypted
? Math.floor(totalChunks / maxChunksEncrypted[redundancyLevel])
: Math.floor(totalChunks / maxChunks[redundancyLevel]);
const remainder = isEncrypted
? totalChunks % maxChunksEncrypted[redundancyLevel]
: totalChunks % maxChunks[redundancyLevel];

let remainderParities = 0;
if (remainder > 0) {
const remainderIndex = remainder - 1 < 0 ? 0 : remainder - 1;
const selectedParities = isEncrypted ? paritiesEncrypted : parities;
remainderParities = selectedParities[redundancyLevel][remainderIndex] || 0;
}

const totalParities = quotient * maxParities[redundancyLevel] + remainderParities;
const totalDataWithParity = totalChunks + totalParities;
const percentDifference = ((totalDataWithParity - totalChunks) / totalChunks) * 100;
const parityDataInKb = (totalParities * (2 ** 12)) / 1024;

// Convert parity data size to GB if input unit is GB
if (unit === "gb") {
parityDataInGb = parityDataInKb / (1024 * 1024); // Convert KB to GB
}

const formatNumber = (num) => new Intl.NumberFormat('en-US').format(num);

const resultsArray = [
{
name: "Source data size",
Expand All @@ -111,28 +124,29 @@ export default function UploadCostCalc() {
},
{
name: "Source data in chunks",
value: formatNumber(totalChunks)
value: formatNumberCustom(totalChunks)
},
{
name: "Additional parity chunks",
value: formatNumber(totalParities)
value: formatNumberCustom(totalParities)
},
{
name: "Percent cost increase",
value: `${percentDifference.toFixed(2)}%`
},
{
name: "Selected redundancy level",
value: `${redundancy}` },
value: `${redundancy}`
},
{
name: "Error tolerance",
value: errorTolerances[redundancy]
}
},
];

setResult(resultsArray);
};


const styles = {
container: { padding: '20px', fontFamily: 'Arial', maxWidth: '650px', margin: '0 auto' },
Expand Down

0 comments on commit 80f4881

Please sign in to comment.