MediaWiki:Common.js: mudanças entre as edições

De WIKI Netnews
Ir para navegação Ir para pesquisar
Sem resumo de edição
Sem resumo de edição
Linha 1: Linha 1:
async function atualizarStatus(host, elementId) {
function atualizarStatus(host, elementId) {
     try {
     var xhr = new XMLHttpRequest();
        const response = await fetch('/noc/status.php?host=' + host);
    xhr.open("GET", "/noc/status.php?host=" + host, true);
        const data = await response.json();
        const el = document.getElementById(elementId);


         if (!el) return;
    xhr.onreadystatechange = function () {
         if (xhr.readyState === 4) {
            var el = document.getElementById(elementId);
            if (!el) return;


        let html = '';
            if (xhr.status === 200) {
                try {
                    var data = JSON.parse(xhr.responseText);
                    var html = "";


        if (data.status === "online") {
                    if (data.status === "online") {
            html = `🟢 Operacional<br>
                        html = "🟢 Operacional<br>" +
                    <small>Uptime: ${formatUptime(data.uptime)}</small><br>
                              "<small>Uptime: " + formatUptime(data.uptime) + "</small><br>" +
                    <small>Último check: ${data.lastcheck}</small>`;
                              "<small>Último check: " + (data.lastcheck ? data.lastcheck : "-") + "</small>";
            el.classList.remove("blink");
                        el.classList.remove("blink");
        } else if (data.status === "offline") {
 
            html = `🔴 Offline<br>
                    } else if (data.status === "offline") {
                    <small>Último check: ${data.lastcheck ?? '-'}</small>`;
                        html = "🔴 Offline<br>" +
            el.classList.add("blink");
                              "<small>Último check: " + (data.lastcheck ? data.lastcheck : "-") + "</small>";
        } else {
                        el.classList.add("blink");
            html = `🟡 Indefinido`;
 
                    } else {
                        html = "🟡 Indefinido";
                        el.classList.remove("blink");
                    }
 
                    el.innerHTML = html;
 
                } catch (e) {
                    el.innerHTML = "🟡 Erro";
                }
 
            } else {
                el.innerHTML = "🟡 Erro";
            }
         }
         }
    };


        el.innerHTML = html;
     xhr.send();
 
     } catch (e) {
        document.getElementById(elementId).innerHTML = "🟡 Erro";
    }
}
}


function formatUptime(seconds) {
function formatUptime(seconds) {
     if (!seconds) return '-';
     if (!seconds || isNaN(seconds)) return "-";


     const days = Math.floor(seconds / 86400);
     var days = Math.floor(seconds / 86400);
     const hours = Math.floor((seconds % 86400) / 3600);
     var hours = Math.floor((seconds % 86400) / 3600);
     const minutes = Math.floor((seconds % 3600) / 60);
     var minutes = Math.floor((seconds % 3600) / 60);


     return `${days}d ${hours}h ${minutes}m`;
     return days + "d " + hours + "h " + minutes + "m";
}
}


Linha 48: Linha 63:
}
}


document.addEventListener("DOMContentLoaded", function () {
if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", atualizarTodos);
} else {
     atualizarTodos();
     atualizarTodos();
    setInterval(atualizarTodos, 30000);
}
});
 
setInterval(atualizarTodos, 30000);

Edição das 17h15min de 18 de fevereiro de 2026

function atualizarStatus(host, elementId) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "/noc/status.php?host=" + host, true);

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            var el = document.getElementById(elementId);
            if (!el) return;

            if (xhr.status === 200) {
                try {
                    var data = JSON.parse(xhr.responseText);
                    var html = "";

                    if (data.status === "online") {
                        html = "🟢 Operacional<br>" +
                               "<small>Uptime: " + formatUptime(data.uptime) + "</small><br>" +
                               "<small>Último check: " + (data.lastcheck ? data.lastcheck : "-") + "</small>";
                        el.classList.remove("blink");

                    } else if (data.status === "offline") {
                        html = "🔴 Offline<br>" +
                               "<small>Último check: " + (data.lastcheck ? data.lastcheck : "-") + "</small>";
                        el.classList.add("blink");

                    } else {
                        html = "🟡 Indefinido";
                        el.classList.remove("blink");
                    }

                    el.innerHTML = html;

                } catch (e) {
                    el.innerHTML = "🟡 Erro";
                }

            } else {
                el.innerHTML = "🟡 Erro";
            }
        }
    };

    xhr.send();
}

function formatUptime(seconds) {
    if (!seconds || isNaN(seconds)) return "-";

    var days = Math.floor(seconds / 86400);
    var hours = Math.floor((seconds % 86400) / 3600);
    var minutes = Math.floor((seconds % 3600) / 60);

    return days + "d " + hours + "h " + minutes + "m";
}

function atualizarTodos() {
    atualizarStatus("web01", "web01-status");
    atualizarStatus("web02", "web02-status");
    atualizarStatus("web03", "web03-status");
    atualizarStatus("web04", "web04-status");
    atualizarStatus("ns1", "ns1-status");
    atualizarStatus("ns2", "ns2-status");
}

if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", atualizarTodos);
} else {
    atualizarTodos();
}

setInterval(atualizarTodos, 30000);