Skip to content

Commit

Permalink
Show evaluated commitments with a graph
Browse files Browse the repository at this point in the history
  • Loading branch information
plumdog committed Jun 6, 2024
1 parent c02c1dd commit 1aa9925
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ <h1>Optimal Commitment Calculator</h1>
<div class="result" id="result"></div>
<a class="result" id="link">Link</a>

<div style="width: 800px"><canvas id="chart"></canvas></div>

<script src="//cdn.jsdelivr.net/npm/chart.js"></script>
<script src="./main.js"></script>
</body>
</html>
Expand Down
35 changes: 35 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function getCurrencyDisplay(currencyCode) {
return '';
}

let chart = undefined;

function calculateCost(discountedRate, fullPrice, usageData, commitment) {
let totalCost = 0;
for (let usage of usageData) {
Expand Down Expand Up @@ -57,11 +59,18 @@ function calculateOptimalCommitment() {
let optimalCommitment = minCommitment;
let minTotalCost = Infinity;

let evaluatedCommitments = [];

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

let totalCost = _calculateCost(commitment);

evaluatedCommitments.push({
commitment,
totalCost,
});

if (totalCost < minTotalCost) {
minTotalCost = totalCost;
optimalCommitment = commitment;
Expand All @@ -81,6 +90,32 @@ function calculateOptimalCommitment() {
`One higher (${optimalCommitment + 1}) cost: ${currencyDisplay}${_calculateCost(optimalCommitment + 1).toFixed(2)}`,
].join("\n");

if (!chart) {
chart = new Chart(document.getElementById('chart'), {
type: 'scatter',
data: {
datasets: [{
label: 'Total cost',
data: evaluatedCommitments,
}],
},
options: {
parsing: {
xAxisKey: 'commitment',
yAxisKey: 'totalCost',
},
},
});
} else {
chart.data = {
datasets: [{
data: evaluatedCommitments,
}],
};
chart.update();
}


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

0 comments on commit 1aa9925

Please sign in to comment.