Last active
August 25, 2024 04:57
-
-
Save MorningLightMountain713/7a5cd1973c56267557826ecb8bc1a08e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Start upgrading x routers per day, double that number until max router upgrades reached. | |
Eg: With starting upgrade number of 200 routers, total of 5000 and max per day of 1000: | |
Day 1 upgrade 200 routers, | |
Day 2 uprade 400 routers, | |
Day 3 upgrade 800 routers, | |
Day 4 upgrade 1000 routers | |
Day 5 upgrade 1000 routers | |
Day 6 upgrade 1000 routers | |
Day 7 upgrade 600 routers and finish. | |
A start day in the future will return 0 chance of router upgrade | |
*/ | |
var STARTING_UPGRADE_AMOUNT = 200 | |
var MAX_UPGRADES_PER_DAY = 1000 | |
// number of routers that are informing (in a 2 hour period for example) | |
var TOTAL_DEVICES_TO_UPDATE = 10000 | |
var FIRMWARE_VERSION = 'IMAGE FILENAME HERE' | |
class RouterCalculator { | |
/** A class to manage router upgrade probability for Genieacs */ | |
constructor(maxUpgradesPerDay, startingUpgradeAmount, totalDevicesToUpdate) { | |
this.maxUpgradesPerDay = maxUpgradesPerDay; | |
this.startingUpgradeAmount = startingUpgradeAmount; | |
this.totalDevicesToUpdate = totalDevicesToUpdate; | |
} | |
_todayAtMidnight() { | |
let now = new Date(); | |
now.setHours(0, 0, 0, 0); | |
let unixTime = now.getTime(); | |
return unixTime; | |
} | |
_getPercentCompleted(startPercentage, maxPercentage, daysCompleted) { | |
let percentCompleted = 0; | |
for (let x = 0; x < daysCompleted; x++) { | |
percentCompleted += (startPercentage * Math.pow(2, x) < maxPercentage) ? startPercentage * Math.pow(2, x) : maxPercentage | |
} | |
return percentCompleted < 100 ? percentCompleted : 100; | |
} | |
daysToComplete() { | |
let daysCompleted = 0; | |
let maxPercentage = (this.maxUpgradesPerDay / this.totalDevicesToUpdate) * 100; | |
let startPercentage = (this.startingUpgradeAmount / this.totalDevicesToUpdate) * 100; | |
while (this._getPercentCompleted(startPercentage, maxPercentage, daysCompleted) < 100) { | |
daysCompleted++ | |
} | |
return daysCompleted | |
} | |
calculateProbability(startDay) { | |
let todayDate = this._todayAtMidnight() | |
if (todayDate < startDay) { | |
return 0; | |
} else { | |
let daysCompleted = (todayDate - startDay) / 86400 / 1000; | |
let maxPercentage = (this.maxUpgradesPerDay / this.totalDevicesToUpdate) * 100; | |
let startPercentage = (this.startingUpgradeAmount / this.totalDevicesToUpdate) * 100; | |
let percentCompleted = this._getPercentCompleted(startPercentage, maxPercentage, daysCompleted); | |
let percentRemaining = (100 - percentCompleted) < 0 ? 0 : (100 - percentCompleted); | |
let upgradePercent = (startPercentage * Math.pow(2, daysCompleted) < maxPercentage) ? startPercentage * Math.pow(2, daysCompleted) : maxPercentage; | |
let upgradeProbability = upgradePercent / 100; | |
let actualUpgradeProb = (1 / (percentRemaining / 100)) * upgradeProbability > 1 ? 1 : (1 / (percentRemaining / 100)) * upgradeProbability; | |
return actualUpgradeProb; | |
} | |
} | |
generateStartDay(daysInTheFuture = 0) { | |
// some validation might be nice | |
daysInTheFuture = Math.floor(daysInTheFuture); | |
daysInTheFuture = -daysInTheFuture > 0 ? -daysInTheFuture : daysInTheFuture; | |
let now = new Date(); | |
now.setHours(0, 0, 0, 0); | |
let unixTime = now.getTime(); | |
return unixTime + (daysInTheFuture * 86400 * 1000); | |
} | |
} | |
/////////////////////////////////////////// | |
function xmur3(str) { | |
for (var i = 0, h = 1779033703 ^ str.length; i < str.length; i++) | |
h = Math.imul(h ^ str.charCodeAt(i), 3432918353), | |
h = h << 13 | h >>> 19; | |
return function () { | |
h = Math.imul(h ^ h >>> 16, 2246822507); | |
h = Math.imul(h ^ h >>> 13, 3266489909); | |
return (h ^= h >>> 16) >>> 0; | |
} | |
} | |
function mulberry32(a) { | |
return function () { | |
var t = a += 0x6D2B79F5; | |
t = Math.imul(t ^ t >>> 15, t | 1); | |
t ^= t + Math.imul(t ^ t >>> 7, t | 61); | |
return ((t ^ t >>> 14) >>> 0) / 4294967296; | |
} | |
} | |
let rc = new RouterCalculator(MAX_UPGRADES_PER_DAY, STARTING_UPGRADE_AMOUNT, TOTAL_DEVICES_TO_UPDATE); | |
// let startDate = rc.generateStartDay(); | |
let upgradeProbability = rc.calculateProbability(1582455600000); | |
// let daysToComplete = rc.daysToComplete(); | |
// console.log("Start date generated " + startDate); | |
// console.log("Probability required to hit upgrade target: " + upgradeProbability); | |
// console.log("Total days required to complete router upgrades (once started): " + daysToComplete); | |
var date = new Date(); | |
date.setHours(0, 0, 0, 0); | |
var dateIntSeed = date.getTime(); | |
// Math.random is seeded with device. (returns same every time) | |
var deviceSeed = Math.random(); | |
var seedString = Math.round(deviceSeed * dateIntSeed).toString(); | |
var seed = xmur3(seedString); | |
var rand = mulberry32(seed()); | |
var r = rand(); | |
if (r <= upgradeProbability) { | |
console.log("Device probabilty: " + r + " , is less than " + upgradeProbability); | |
// declare("Downloads.[FileType:1 Firmware Upgrade Image]", { path: 1 }, { path: 1 }); | |
// declare("Downloads.[FileType:1 Firmware Upgrade Image].FileName", { value: 1 }, { value: FIRMWARE_VERSION }); | |
// declare("Downloads.[FileType:1 Firmware Upgrade Image].Download", { value: 1 }, { value: Date.now() }); | |
// log("Firmware updated"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment