Files
sf-monitorizacion-health/src/views/index.ejs
2026-05-07 09:30:11 +02:00

239 lines
8.0 KiB
Plaintext

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Monitor de Salud</title>
<style>
body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; background: #f9f9f9; }
h1 { color: #333; }
table { width: 100%; border-collapse: collapse; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
td { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }
th { background: #f4f4f4; font-weight: bold; }
tr:hover { background: #fafafa; }
tr.status-PAUSADO { opacity: 0.6; background: #eee; }
.OK { color: #2e7d32; font-weight: bold; }
.ERROR { color: #c62828; font-weight: bold; }
.TIMEOUT { color: #e65100; font-weight: bold; }
.PAUSADO { color: #757575; font-weight: bold; }
.toolbar {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
background: #fff;
padding: 15px;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
gap: 16px;
}
.toolbar-actions {
display: flex;
align-items: center;
gap: 10px;
}
.btn {
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
font: inherit;
line-height: 1;
text-decoration: none;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 6px;
height: 36px;
box-sizing: border-box;
}
.btn svg {
width: 16px;
height: 16px;
flex: 0 0 auto;
}
.btn-add { background: #2e7d32; color: white; }
.btn-refresh { background: #0277bd; color: white; }
.btn-mini { padding: 4px 8px; font-size: 16px; }
.btn:disabled { background: #ccc; cursor: not-allowed; }
.actions .btn-mini {
background: white;
color: #111;
border: 1px solid #111;
}
.actions .btn-mini:disabled {
background: #eee;
color: #777;
border-color: #999;
}
.refresh-group { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.status-message { color: #2e7d32; min-width: 70px; }
input { padding: 6px; border-radius: 4px; border: 1px solid #ddd; }
input[type="number"] { text-align: right; }
</style>
</head>
<body>
<h1>Monitor de Salud</h1>
<div class="toolbar">
<div class="toolbar-actions">
<a href="/nuevo" class="btn btn-add">
<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="9"></circle>
<path d="M12 8v8"></path>
<path d="M8 12h8"></path>
</svg>
Añadir Proyecto
</a>
<button class="btn btn-refresh" onclick="runCheck('/check-all', this)">Actualizar Todo</button>
</div>
<div class="refresh-group">
<label for="refreshSeconds">Comprobar estados cada:</label>
<input type="number" id="refreshSeconds" min="10" max="3600" value="<%= monitorIntervalSeconds %>" style="width: 60px;">
<span>segundos</span>
<button class="btn btn-mini" onclick="updateMonitorInterval(this)">Aplicar</button>
<span id="intervalStatus" class="status-message"></span>
</div>
</div>
<table>
<thead>
<tr>
<th>Proyecto</th>
<th>URL</th>
<th>Estado</th>
<th>Ultima comprobacion</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
<% proyectos.forEach(function(p) { %>
<tr class="status-<%= p.estado_monitoreo %>">
<td><%= p.nombre %></td>
<td>
<a href="<%= p.url_health %>" target="_blank">
<%= p.url_health %>
</a>
</td>
<td class="<%= p.estado_monitoreo === 'PAUSADO' ? 'PAUSADO' : (p.estado_codigo || 'PENDIENTE') %>">
<%= p.estado_monitoreo === 'PAUSADO' ? 'PAUSADO' : (p.estado_codigo || 'PENDIENTE') %>
</td>
<td>
<%= p.fecha ? new Date(p.fecha).toLocaleString('es-ES') : '-' %>
</td>
<td class="actions">
<!-- Botón refrescar -->
<button class="btn btn-refresh btn-mini" onclick="runCheck('/check/<%= p.id %>', this)" title="Refrescar">↻</button>
<!-- Botón pausar -->
<button class="btn btn-pause btn-mini" onclick="runAction('/proyectos/<%= p.id%>/toggle-status', this)">
<%= p.estado_monitoreo === 'ACTIVO' ? '⏸' : '▶' %>
</button>
<!-- Botón eliminar -->
<button class="btn btn-delete btn-mini" onclick="confirmDelete('/proyectos/<%= p.id %>/eliminar', this)">
🗑
</button>
</td>
</tr>
<% }) %>
</tbody>
</table>
<script>
const monitorIntervalSeconds = <%= monitorIntervalSeconds %>;
function schedulePageRefresh(seconds) {
setTimeout(() => {
location.reload();
}, seconds * 1000);
}
async function runCheck(url, btn) {
const originalText = btn.innerText;
btn.disabled = true;
btn.innerText = "...";
try {
const response = await fetch(url, { method: 'POST' });
if (response.ok) {
location.reload();
} else {
alert("Error al actualizar");
btn.disabled = false;
btn.innerText = originalText;
}
} catch (error) {
console.error("Fallo en la peticion:", error);
btn.disabled = false;
btn.innerText = originalText;
}
}
async function updateMonitorInterval(btn) {
const seconds = parseInt(document.getElementById('refreshSeconds').value, 10);
const status = document.getElementById('intervalStatus');
const originalText = btn.innerText;
if (!Number.isInteger(seconds) || seconds < 10 || seconds > 3600) {
alert("El intervalo debe estar entre 10 y 3600 segundos.");
return;
}
btn.disabled = true;
btn.innerText = "...";
status.innerText = "";
try {
const response = await fetch('/monitor-interval', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ seconds })
});
if (response.ok) {
location.reload();
} else {
alert("Error al guardar el intervalo");
}
} catch (error) {
console.error("Fallo guardando el intervalo:", error);
} finally {
btn.disabled = false;
btn.innerText = originalText;
}
}
async function runAction(url, btn) {
btn.disabled = true;
try {
const response = await fetch(url, { method: 'POST' });
if (response.ok) {
location.reload();
} else {
alert("Error al realizar la acción");
btn.disabled = false;
}
} catch (error) {
console.error("Fallo en la peticion:", error);
btn.disabled = false;
}
}
function confirmDelete(url, btn) {
if (confirm("¿Estas seguro de que quieres dejar de monitorizar este proyecto? El historial no se borrara.")) {
runAction(url, btn);
}
}
schedulePageRefresh(monitorIntervalSeconds);
</script>
</body>
</html>