syncthing-relays-landing/relays.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

const mine = {
'colorful-shadow': '194.32.107.224',
'hidden-ocean': '89.105.197.88',
};
function humanByteSize(input) {
if (input <= 0) {
return '0 B';
}
const i = Math.floor(Math.log(input) / Math.log(1024));
return (input / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
}
// Convert seconds to human friendly time, with days
function humanTime(input) {
const days = Math.floor(input / 86400);
const hours = Math.floor(input % 86400 / 3600);
const minutes = Math.floor(input % 3600 / 60);
return `${days}d ${hours}h ${minutes}m`;
}
async function getRelayData() {
const response = await fetch('https://relays.syncthing.net/endpoint');
if (!response.ok) {
return;
}
const data = await response.json();
const relays = data.relays;
for (const mineName in mine) {
const ip = mine[mineName];
const relay = relays.find(current => current.url.includes(ip));
if (!relay) {
console.log(mineName, 'not found');
continue;
}
const { stats } = relay;
const element = document.querySelector(`#${mineName} .stats`);
if (!element) {
continue;
}
const text = `${humanTime(stats.uptimeSeconds)} / ${humanByteSize(stats.bytesProxied)}`;
element.textContent = text;
}
}
window.addEventListener('DOMContentLoaded', getRelayData);