syncthing-relays-landing/relays.js

56 lines
1.5 KiB
JavaScript

const mine = {
'colorful-shadow': '194.32.107.224',
'hidden-ocean': '89.105.197.88',
'moonlit-surf': '91.190.155.20',
'gorgeous-sunset': '188.93.140.100',
};
function humanByteSize(input) {
if (input <= 0) {
return '0 B';
}
const i = Math.floor(Math.log(input) / Math.log(1000));
return (input / Math.pow(1000, 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/full');
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);