Skip to content

Instantly share code, notes, and snippets.

@lukacat10
Last active January 1, 2023 11:00
Show Gist options
  • Save lukacat10/cc357e08ea19d779bceaab4f42534c13 to your computer and use it in GitHub Desktop.
Save lukacat10/cc357e08ea19d779bceaab4f42534c13 to your computer and use it in GitHub Desktop.
Group azure spot instances by region

Instructions:

  1. Visit: https://azure.microsoft.com/en-us/pricing/spot-advisor/#pricing
  2. Open the console via F12
  3. Paste the script
  4. Move between the different regions in the dropdown menu (click the dropdown and use the arrow keys to move between the regions quickly).
  5. Run getPlans(), or getPlansFromCheapestToMostExpensive(), or getRegionsFromCheapestToMostExpensive(), or getPlansFilteredByPlanTypeFromCheapestToMostExpensive(), or getRegionsFilteredByPlanTypeFromCheapestToMostExpensive().
  6. There is a better way to do that, I'm sure and ashamed of myself.
let tableBody = document.querySelector(".data-table__table.data-table__table--pricing tbody");
let regionSelector = document.querySelector("#pricing > div > div.row.row-size3 > div:nth-child(2) > div > select");
function extractJsonFromTable() {
let currentRow = {
instance: "",
vcpu: "",
ram: "",
tempStorage: "",
payAsYouGo: "",
spot: "",
region: ""
}
let collectedRows = [];
for(let row of tableBody.children) {
currentRow = {};
let columns = row.children;
currentRow.instance = columns[0].innerText;
currentRow.vcpu = columns[1].innerText;
currentRow.ram = columns[2].innerText;
currentRow.tempStorage = columns[3].innerText;
currentRow.payAsYouGo = parseFloat(columns[4].innerText.split("/month")[0].substr(1).replace(",",""));
currentRow.spot = parseFloat(columns[5].innerText.split("/month")[0].substr(1).replace(",",""));;
currentRow.region = regionSelector.value;
collectedRows.push(currentRow);
}
return collectedRows;
}
let allRows = [];
setInterval(() => {
if(allRows.find((row) => row.region === regionSelector.value) === undefined){
allRows = allRows.concat(extractJsonFromTable());
console.log("appended");
}
}, 200)
function getPlans() {
return allRows;
}
function getPlansFromCheapestToMostExpensive() {
return getPlans().sort((row1, row2) => row1.spot > row2.spot ? 1 : -1);
}
function getRegionsFromCheapestToMostExpensive() {
return getPlansFromCheapestToMostExpensive().map((row) => row.region);
}
function getPlansFilteredByPlanTypeFromCheapestToMostExpensive() {
return getPlans().filter((row) => row.instance === "D4s v4").sort((row1, row2) => row1.spot > row2.spot ? 1 : -1);
}
function getRegionsFilteredByPlanTypeFromCheapestToMostExpensive() {
return getPlansFilteredByPlanTypeFromCheapestToMostExpensive().map((row) => row.region);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment