Skip to content

Commit

Permalink
Very basic but working
Browse files Browse the repository at this point in the history
  • Loading branch information
plumdog committed Jun 6, 2024
1 parent 5c96812 commit 6531f4f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ main() {
rm -rf build
mkdir -p build

cp index.html build/
cp index.html main.js build/
}

cd "$repo_dir" && main "$@"
49 changes: 10 additions & 39 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,57 +25,28 @@
<body>
<h1>Optimal Commitment Calculator</h1>
<form id="calculator-form">
<label for="currency">Currency:</label>
<select id="currency" name="currency">
<option value="USD">$</option>
<option value="GBP">£</option>
</select>

<label for="usage">Historical Usage (comma separated):</label>
<input type="text" id="usage" required>

<label for="discountedRate">Discounted Rate (£):</label>
<label for="discountedRate">Discounted Rate:</label>
<input type="number" id="discountedRate" step="0.01" required>

<label for="fullPrice">Full Price (£):</label>
<label for="fullPrice">Full Price:</label>
<input type="number" id="fullPrice" step="0.01" required>

<button type="button" onclick="calculateOptimalCommitment()">Calculate</button>
</form>

<div class="result" id="result"></div>
<a class="result" id="link">Link</a>

<script>
function calculateOptimalCommitment() {
// Get the inputs
const usageData = document.getElementById('usage').value.split(',').map(Number);
const discountedRate = parseFloat(document.getElementById('discountedRate').value);
const fullPrice = parseFloat(document.getElementById('fullPrice').value);

const minCommitment = Math.min(...usageData);
const maxCommitment = Math.max(...usageData);
const step = 10; // Adjust step size as needed

let optimalCommitment = minCommitment;
let minTotalCost = Infinity;

// Iterate through possible commitment levels
for (let commitment = minCommitment; commitment <= maxCommitment; commitment += step) {
let totalCost = 0;

for (let usage of usageData) {
if (usage <= commitment) {
totalCost += commitment * discountedRate;
} else {
totalCost += commitment * discountedRate + (usage - commitment) * fullPrice;
}
}

if (totalCost < minTotalCost) {
minTotalCost = totalCost;
optimalCommitment = commitment;
}
}

// Display the result
document.getElementById('result').innerText =
`Optimal Commitment Level: £${optimalCommitment.toFixed(2)}\nMinimum Total Cost: £${minTotalCost.toFixed(2)}`;
}
</script>
<script src="./main.js"></script>
</body>
</html>

Expand Down
84 changes: 84 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Populate the inputs from the querystring if set
addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(window.location.search);
if (params.has('usage')) {
document.getElementById('usage').value = params.get('usage');
}
if (params.has('discountedRate')) {
document.getElementById('discountedRate').value = params.get('discountedRate');
}
if (params.has('fullPrice')) {
document.getElementById('fullPrice').value = params.get('fullPrice');
}

if (params.has('currency')) {
document.getElementById('currency').value = params.get('currency');
}

calculateOptimalCommitment();
});

function getCurrencyDisplay(currencyCode) {
if (currencyCode === 'GBP') {
return '£';
} else if (currencyCode === 'USD') {
return '$';
}

return '';
}

function calculateOptimalCommitment() {
// Get the inputs
const usageData = document.getElementById('usage').value.split(',').map(Number);
const discountedRate = parseFloat(document.getElementById('discountedRate').value);
const fullPrice = parseFloat(document.getElementById('fullPrice').value);

const minCommitment = Math.min(...usageData);
const maxCommitment = Math.max(...usageData);
const step = 1;

let optimalCommitment = minCommitment;
let minTotalCost = Infinity;

// Iterate through possible commitment levels
for (let commitment = minCommitment; commitment <= maxCommitment; commitment += step) {
let totalCost = 0;

for (let usage of usageData) {
if (usage <= commitment) {
totalCost += commitment * discountedRate;
} else {
totalCost += commitment * discountedRate + (usage - commitment) * fullPrice;
}
}

if (totalCost < minTotalCost) {
minTotalCost = totalCost;
optimalCommitment = commitment;
}
}

let currency = document.getElementById('currency').value;
let currencyDisplay = getCurrencyDisplay(currency);

// Display the result
document.getElementById('result').innerText =
`Optimal Commitment Level: ${currencyDisplay}${optimalCommitment.toFixed(2)}\nMinimum Total Cost: ${currencyDisplay}${minTotalCost.toFixed(2)}`;

// And show the link to this result using the querystring
function showLink() {
const usage = document.getElementById('usage').value;
const discountedRate = document.getElementById('discountedRate').value;
const fullPrice = document.getElementById('fullPrice').value;

const url = new URL(window.location);
url.searchParams.set('usage', usage);
url.searchParams.set('discountedRate', discountedRate);
url.searchParams.set('fullPrice', fullPrice);

document.getElementById('link').href = url.toString();
}

showLink();
}

0 comments on commit 6531f4f

Please sign in to comment.