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:
function atualizarStatus(host, elementId) {
async function atualizarStatus(host, elementId) {
     var xhr = new XMLHttpRequest();
     try {
    xhr.open("GET", "/noc/status.php?host=" + host, true);
        const response = await fetch('/noc/status.php?host=' + host);
        const data = await response.json();
        const el = document.getElementById(elementId);


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


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


                    if (data.status === "online") {
        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 =
                        html = "🔴 Offline<br>" +
                '<span style="color:#22c55e;font-weight:600;">● OPERACIONAL</span><br>' +
                              "<small>Último check: " + (data.lastcheck ? data.lastcheck : "-") + "</small>";
                '<small>Uptime: ' + formatUptime(data.uptime) + '</small><br>' +
                        el.classList.add("blink");
                '<small>Atualizado: ' + formatHora(data.lastcheck) + '</small>';


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


                    el.innerHTML = html;
        } else if (data.status === "offline") {


                 } catch (e) {
            html =
                    el.innerHTML = "🟡 Erro";
                 '<span style="color:#ef4444;font-weight:600;">● OFFLINE</span><br>' +
                 }
                 '<small>Último check: ' + (data.lastcheck || '-') + '</small>';


             } else {
             el.classList.add("blink");
                 el.innerHTML = "🟡 Erro";
 
             }
        } else {
 
            html =
                 '<span style="color:#facc15;font-weight:600;">● INDEFINIDO</span>';
 
             el.classList.remove("blink");
         }
         }
    };


     xhr.send();
        el.innerHTML = html;
 
     } catch (e) {
        document.getElementById(elementId).innerHTML =
            '<span style="color:#facc15;">● ERRO</span>';
    }
}
}


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


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


     return days + "d " + hours + "h " + minutes + "m";
     return days + 'd ' + hours + 'h';
}
 
function formatHora(datetime) {
    if (!datetime) return '-';
 
    const partes = datetime.split(' ');
    if (partes.length > 1) {
        return partes[1];
    }
    return datetime;
}
}


Linha 63: Linha 70:
}
}


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

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

async function atualizarStatus(host, elementId) {
    try {
        const response = await fetch('/noc/status.php?host=' + host);
        const data = await response.json();
        const el = document.getElementById(elementId);

        if (!el) return;

        let html = '';

        if (data.status === "online") {

            html =
                '<span style="color:#22c55e;font-weight:600;">● OPERACIONAL</span><br>' +
                '<small>Uptime: ' + formatUptime(data.uptime) + '</small><br>' +
                '<small>Atualizado: ' + formatHora(data.lastcheck) + '</small>';

            el.classList.remove("blink");

        } else if (data.status === "offline") {

            html =
                '<span style="color:#ef4444;font-weight:600;">● OFFLINE</span><br>' +
                '<small>Último check: ' + (data.lastcheck || '-') + '</small>';

            el.classList.add("blink");

        } else {

            html =
                '<span style="color:#facc15;font-weight:600;">● INDEFINIDO</span>';

            el.classList.remove("blink");
        }

        el.innerHTML = html;

    } catch (e) {
        document.getElementById(elementId).innerHTML =
            '<span style="color:#facc15;">● ERRO</span>';
    }
}

function formatUptime(seconds) {
    if (!seconds || seconds <= 0) return '-';

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

    return days + 'd ' + hours + 'h';
}

function formatHora(datetime) {
    if (!datetime) return '-';

    const partes = datetime.split(' ');
    if (partes.length > 1) {
        return partes[1];
    }
    return datetime;
}

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");
}

document.addEventListener("DOMContentLoaded", function () {
    atualizarTodos();
    setInterval(atualizarTodos, 30000);
});