Created
September 20, 2015 14:29
storj tracker
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
// INSTRUCTIONS: | |
// 1. Update settings & addresses (line 14) | |
// 2. Generate a bookmarklet using http://chriszarate.github.io/bookmarkleter/ | |
// 3. use it while on http://driveshare.org/status.html | |
// MAIN CODE: | |
// Settings | |
// | |
// Address to watch. | |
// Either via array: | |
// var myAddresses = ['1ANFwqjfqkwrqiuwr12412', '1IOWRoiqwmrqlwrfasfasf', '1WORIqoiwrqlwwetwet']; | |
// var myAddresses = ['1ANFwqjfqkwrqiuwr12412']; | |
// Or as an object with addresses as keys and labels as values | |
var myAddresses = { | |
'1ANFwqjfqkwrqiuwr12412asfjqnwrKWRw': 'Toshiba-1TB', | |
'1IOWRoiqwmrqlwrfasfasfRWUIqrwrqkar': 'WD-2TB', | |
'1JqTCrxzH2Pk1W2ohc3QUmvZrJXimtUDaH': 'Nuc-0' | |
}; | |
var checkIntervalMinutes = 2; | |
var autoCloseNotificationSeconds = 5; | |
// Running vars | |
var lastCapDiffs = []; | |
var prevCapacities = {}; | |
var lastUpdate = 0; | |
// Notifications | |
var notify = function() {}; | |
if ("Notification" in window) { | |
notify = function(title, opts) { | |
var _opts = opts || {}; | |
Notification.requestPermission(function (permission) { | |
if (permission === 'granted') { | |
var n = new Notification(title, _opts); | |
n.onshow = function() { | |
setTimeout(n.close.bind(n), autoCloseNotificationSeconds * 1000); | |
} | |
} | |
}); | |
} | |
} | |
// Code | |
function addrName(addr) { | |
if ((myAddresses && myAddresses.length) || typeof myAddresses[addr] === 'undefined') { | |
return 'Addr: ' + addr; | |
} | |
return 'D(' + myAddresses[addr] + ')'; | |
} | |
function isWatchingAddr(addr) { | |
if (myAddresses && myAddresses.length) { | |
return (myAddresses.indexOf(addr) !== -1); | |
} else { | |
return (typeof myAddresses[addr] !== 'undefined'); | |
} | |
} | |
function calcCapDiffs(currentFarmer) { | |
var storageSize = (currentFarmer.height*128/1024).toFixed(2); | |
if (prevCapacities[currentFarmer.payout_addr]) { | |
var diffN = Number(storageSize) - Number(prevCapacities[currentFarmer.payout_addr]); | |
prevCapacities[currentFarmer.payout_addr] = storageSize; | |
var diff = Number(diffN).toFixed(2); | |
var t = (new Date()).toTimeString().split(' ')[0]; | |
var txt = addrName(currentFarmer.payout_addr) + ' got '; | |
var diffPlain = (diffN < 0 ? '-' : '+' ) + diff + 'GB'; | |
var diffHtml = '<b style="color:#' + (diffN < 0 ? '9A4444' : '509A44') + '">' + (diffN < 0 ? '-' : '+') + diff + 'GB</b>'; | |
var notifBody = txt + diffPlain + ' with total: ' + Number(storageSize).toFixed(1) + 'GB' | |
if (diffN > 0) { | |
lastCapDiffs.unshift(t + ' - ' + txt + diffHtml); | |
notify('Storj Client increased space', { body: notifBody }); | |
} else if (diffN < 0) { | |
lastCapDiffs.unshift(t + ' - ' + txt + diffHtml); | |
notify('Storj Client decreased space', { body: notifBody }); | |
} else { | |
return '-'; | |
} | |
return diffHtml; | |
} | |
prevCapacities[currentFarmer.payout_addr] = storageSize; | |
return ''; | |
} | |
function refreshData() { | |
if (Date.now() - lastUpdate < 10 * 1000) { | |
console.log('Throttling...'); | |
return; | |
} | |
lastUpdate = Date.now(); | |
$.getJSON('//status.driveshare.org/api/online/json', function(json){ | |
var rows = []; | |
var astrRaw = []; | |
for (var i = 0; i < json.farmers.length; i++) { | |
var currentFarmer = json.farmers[i]; | |
if (!isWatchingAddr(currentFarmer.payout_addr)) { | |
continue; | |
} | |
var storageSize = (currentFarmer.height*128/1024).toFixed(2); | |
var storageUnit = 'GB'; | |
var diffSpace = calcCapDiffs(currentFarmer); | |
var cells = ['#' + (i+1), addrName(currentFarmer.payout_addr), storageSize+' '+storageUnit, diffSpace, currentFarmer.last_seen+' seconds']; | |
astrRaw.push('#' + (i+1) + ' - ' + addrName(currentFarmer.payout_addr) + ' - Space: '+storageSize+' '+storageUnit+ ' '+diffSpace+' - Last: '+currentFarmer.last_seen+' seconds'); | |
rows.push('<tr><td>' + cells.join('</td><td>') + '</td></tr>'); | |
} | |
// Update cLog | |
astrRaw.push('Last update: ' + String(Date())); | |
var t = astrRaw.join("\n"); | |
console.log(t); | |
// Render table | |
rows.push('<tr><td colspan="5">Last update: ' + String(Date()) + '</td></tr>'); | |
var h = '<table class="table table-striped table-hover">' + | |
'<thead><tr><td>Rank</td><td>Addr</td><td>Space</td><td>Diff</td><td>Last seen</td></tr></thead>' + | |
'<tbody>' + rows.join('') + '</tbody></table>'; | |
lastCapDiffs = lastCapDiffs.slice(0,5); | |
h += (lastCapDiffs.length > 0 ? 'Latest:<br/>' : '') + lastCapDiffs.join('<br/>'); | |
if ($('#myminers').length === 0) { | |
$('body').append('<div id="myminers" style="padding:10px;text-align:left;position:fixed;z-index:10;top:10px;left:10px;border:1px solid #ccc;background:#fff;border-radius:3px;"></div>'); | |
} | |
btnHtml = '<br/><button class="btn btn-green btn-sm" onclick="refreshBrmkletData()">Refresh</button>'; | |
$('#myminers').html(h + btnHtml); | |
}); | |
} | |
window.refreshBrmkletData = refreshData; | |
setInterval(refreshData, checkIntervalMinutes * 60 * 1000); | |
refreshData(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment