lecturehalls-dashboard/public/script.js

48 lines
1.7 KiB
JavaScript

const template = document.getElementById('itembox');
const oks = document.querySelector('oks');
const errors = document.querySelector('errors');
const now = Date.now();
fetch('./get.php')
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
return response.json();
})
.then((data) => {
names = Object.keys(data);
names.sort();
for(var i = 0; i < names.length; ++i) {
const key = names[i];
const item = data[key];
const instance = template.content.cloneNode(true);
const name = instance.querySelector('name');
const checktime = instance.querySelector('checktime');
const status = instance.querySelector('status');
const detail = instance.querySelector('detail');
name.textContent = key.replace(/\.dsv\.local\.su\.se/, '');
checktime.textContent = formatTime(now/1000 - item.time);
if(item.status == '0') {
status.parentNode.removeChild(status);
detail.parentNode.removeChild(detail);
instance.firstElementChild.classList.add('good');
oks.appendChild(instance);
} else {
status.textContent = item.status;
detail.textContent = item.detail;
instance.firstElementChild.classList.add('bad');
errors.appendChild(instance);
}
}
});
function formatTime(secondsAgo) {
if (secondsAgo < 60) {
return secondsAgo.toFixed(0) + " seconds ago";
} else {
return (secondsAgo/60).toFixed(0) + " minutes ago";
}
}