feat: add monitoring dashboard and metrics collector
هذا الالتزام موجود في:
437
q5-mithal-monitor/dashboard.html
Normal file
437
q5-mithal-monitor/dashboard.html
Normal file
@@ -0,0 +1,437 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Mithal Monitor Dashboard</title>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #0f172a;
|
||||
--bg-soft: #111c38;
|
||||
--card: rgba(15, 23, 42, 0.88);
|
||||
--card-border: rgba(148, 163, 184, 0.18);
|
||||
--text: #e2e8f0;
|
||||
--muted: #94a3b8;
|
||||
--accent: #38bdf8;
|
||||
--good: #22c55e;
|
||||
--bad: #ef4444;
|
||||
--warn: #f59e0b;
|
||||
--shadow: 0 18px 60px rgba(2, 6, 23, 0.45);
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
||||
color: var(--text);
|
||||
background:
|
||||
radial-gradient(circle at top left, rgba(56, 189, 248, 0.18), transparent 34%),
|
||||
radial-gradient(circle at top right, rgba(34, 197, 94, 0.14), transparent 28%),
|
||||
linear-gradient(180deg, #020617 0%, var(--bg) 45%, #020617 100%);
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.page {
|
||||
max-width: 1280px;
|
||||
margin: 0 auto;
|
||||
padding: 32px 20px 48px;
|
||||
}
|
||||
|
||||
.hero {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
align-items: end;
|
||||
margin-bottom: 24px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.eyebrow {
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.18em;
|
||||
color: var(--accent);
|
||||
font-size: 12px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
font-size: clamp(2rem, 5vw, 3.6rem);
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.subhead {
|
||||
margin-top: 10px;
|
||||
color: var(--muted);
|
||||
max-width: 680px;
|
||||
}
|
||||
|
||||
.status-pill {
|
||||
padding: 10px 14px;
|
||||
border-radius: 999px;
|
||||
background: rgba(15, 23, 42, 0.7);
|
||||
border: 1px solid var(--card-border);
|
||||
color: var(--muted);
|
||||
box-shadow: var(--shadow);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
.metrics-grid {
|
||||
grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: var(--card);
|
||||
border: 1px solid var(--card-border);
|
||||
border-radius: 20px;
|
||||
padding: 18px;
|
||||
box-shadow: var(--shadow);
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
.metric-label {
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.metric-value {
|
||||
font-size: 2rem;
|
||||
font-weight: 700;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
.metric-note {
|
||||
margin-top: 8px;
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
min-height: 18px;
|
||||
}
|
||||
|
||||
.good { color: var(--good); }
|
||||
.bad { color: var(--bad); }
|
||||
.warn { color: var(--warn); }
|
||||
|
||||
.panel-title {
|
||||
margin: 0 0 14px;
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.chart-wrap {
|
||||
height: 380px;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
th, td {
|
||||
text-align: left;
|
||||
padding: 12px 10px;
|
||||
border-bottom: 1px solid rgba(148, 163, 184, 0.14);
|
||||
vertical-align: top;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
th {
|
||||
color: var(--muted);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.table-scroll {
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.small {
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.footer-note {
|
||||
margin-top: 14px;
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
@media (max-width: 720px) {
|
||||
.page { padding: 18px 14px 34px; }
|
||||
.card { padding: 16px; }
|
||||
.metric-value { font-size: 1.6rem; }
|
||||
.chart-wrap { height: 320px; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main class="page">
|
||||
<section class="hero">
|
||||
<div>
|
||||
<div class="eyebrow">Mithal.space monitoring</div>
|
||||
<h1>Live uptime and latency dashboard</h1>
|
||||
<p class="subhead">This page reads <strong>metrics.json</strong>, summarizes the last 24 hours, and refreshes automatically every 60 seconds.</p>
|
||||
</div>
|
||||
<div class="status-pill" id="lastRefresh">Waiting for data...</div>
|
||||
</section>
|
||||
|
||||
<section class="grid metrics-grid">
|
||||
<div class="card">
|
||||
<div class="metric-label">Uptime percentage (24h)</div>
|
||||
<div class="metric-value" id="uptimePercentage">--</div>
|
||||
<div class="metric-note" id="uptimeNote"></div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="metric-label">Latest HTTP latency</div>
|
||||
<div class="metric-value" id="latestLatency">--</div>
|
||||
<div class="metric-note" id="latencyNote"></div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="metric-label">Latest DNS lookup time</div>
|
||||
<div class="metric-value" id="latestDns">--</div>
|
||||
<div class="metric-note" id="dnsNote"></div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="metric-label">SSL status</div>
|
||||
<div class="metric-value" id="sslStatus">--</div>
|
||||
<div class="metric-note" id="sslNote"></div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="metric-label">SSL expiration date</div>
|
||||
<div class="metric-value" id="sslExpiry">--</div>
|
||||
<div class="metric-note" id="sslExpiryNote"></div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="metric-label">Latest search response time</div>
|
||||
<div class="metric-value" id="latestSearch">--</div>
|
||||
<div class="metric-note" id="searchNote"></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="card" style="margin-bottom: 18px;">
|
||||
<h2 class="panel-title">Latency line chart</h2>
|
||||
<div class="chart-wrap">
|
||||
<canvas id="latencyChart"></canvas>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h2 class="panel-title">Latest 10 monitoring records</h2>
|
||||
<div class="table-scroll">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Timestamp</th>
|
||||
<th>Status</th>
|
||||
<th>Uptime</th>
|
||||
<th>Latency</th>
|
||||
<th>DNS</th>
|
||||
<th>SSL</th>
|
||||
<th>SSL Expiry</th>
|
||||
<th>Search</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="recordsBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="footer-note" id="errorNote"></div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<script>
|
||||
const metricsPath = 'metrics.json';
|
||||
let latencyChart = null;
|
||||
|
||||
function safeNumber(value, digits = 2) {
|
||||
if (value === null || value === undefined || Number.isNaN(Number(value))) {
|
||||
return '--';
|
||||
}
|
||||
return `${Number(value).toFixed(digits)} ms`;
|
||||
}
|
||||
|
||||
function formatTimestamp(timestamp) {
|
||||
if (!timestamp) {
|
||||
return '--';
|
||||
}
|
||||
const date = new Date(timestamp);
|
||||
if (Number.isNaN(date.getTime())) {
|
||||
return timestamp;
|
||||
}
|
||||
return date.toLocaleString();
|
||||
}
|
||||
|
||||
function formatBoolean(value) {
|
||||
if (value === true) {
|
||||
return 'UP';
|
||||
}
|
||||
if (value === false) {
|
||||
return 'DOWN';
|
||||
}
|
||||
return '--';
|
||||
}
|
||||
|
||||
function setMetric(id, value, unit = 'ms', noteId = null, error = null) {
|
||||
const valueEl = document.getElementById(id);
|
||||
const noteEl = noteId ? document.getElementById(noteId) : null;
|
||||
if (!valueEl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
valueEl.textContent = '--';
|
||||
valueEl.className = 'metric-value warn';
|
||||
} else if (unit === 'bool') {
|
||||
valueEl.textContent = formatBoolean(value);
|
||||
valueEl.className = `metric-value ${value ? 'good' : 'bad'}`;
|
||||
} else if (unit === 'text') {
|
||||
valueEl.textContent = String(value);
|
||||
valueEl.className = 'metric-value';
|
||||
} else {
|
||||
valueEl.textContent = safeNumber(value);
|
||||
valueEl.className = 'metric-value';
|
||||
}
|
||||
|
||||
if (noteEl) {
|
||||
noteEl.textContent = error || '';
|
||||
}
|
||||
}
|
||||
|
||||
function calculateUptimePercentage(records) {
|
||||
const lastDay = Date.now() - (24 * 60 * 60 * 1000);
|
||||
const recentRecords = records.filter((record) => {
|
||||
const time = new Date(record.timestamp).getTime();
|
||||
return !Number.isNaN(time) && time >= lastDay;
|
||||
});
|
||||
|
||||
if (!recentRecords.length) {
|
||||
return { percentage: null, count: 0 };
|
||||
}
|
||||
|
||||
const successful = recentRecords.filter((record) => record.uptime === true).length;
|
||||
return {
|
||||
percentage: Math.round((successful / recentRecords.length) * 1000) / 10,
|
||||
count: recentRecords.length,
|
||||
};
|
||||
}
|
||||
|
||||
function buildChart(records) {
|
||||
const canvas = document.getElementById('latencyChart');
|
||||
const labels = records.map((record) => formatTimestamp(record.timestamp));
|
||||
const data = records.map((record) => record.latency_ms ?? null);
|
||||
|
||||
if (latencyChart) {
|
||||
latencyChart.destroy();
|
||||
}
|
||||
|
||||
latencyChart = new Chart(canvas, {
|
||||
type: 'line',
|
||||
data: {
|
||||
labels,
|
||||
datasets: [{
|
||||
label: 'HTTP latency (ms)',
|
||||
data,
|
||||
tension: 0.35,
|
||||
borderColor: '#38bdf8',
|
||||
backgroundColor: 'rgba(56, 189, 248, 0.15)',
|
||||
pointRadius: 3,
|
||||
pointHoverRadius: 5,
|
||||
fill: true,
|
||||
spanGaps: true,
|
||||
}],
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
plugins: {
|
||||
legend: {
|
||||
labels: { color: '#e2e8f0' },
|
||||
},
|
||||
},
|
||||
scales: {
|
||||
x: {
|
||||
ticks: { color: '#94a3b8', maxRotation: 0, autoSkip: true },
|
||||
grid: { color: 'rgba(148, 163, 184, 0.12)' },
|
||||
},
|
||||
y: {
|
||||
ticks: { color: '#94a3b8' },
|
||||
grid: { color: 'rgba(148, 163, 184, 0.12)' },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function renderTable(records) {
|
||||
const body = document.getElementById('recordsBody');
|
||||
body.innerHTML = '';
|
||||
|
||||
const latestRecords = records.slice(-10).reverse();
|
||||
|
||||
if (!latestRecords.length) {
|
||||
body.innerHTML = '<tr><td colspan="8" class="small">No records available yet.</td></tr>';
|
||||
return;
|
||||
}
|
||||
|
||||
for (const record of latestRecords) {
|
||||
const row = document.createElement('tr');
|
||||
row.innerHTML = `
|
||||
<td>${formatTimestamp(record.timestamp)}</td>
|
||||
<td>${record.status_code ?? '--'}</td>
|
||||
<td class="${record.uptime === true ? 'good' : record.uptime === false ? 'bad' : 'warn'}">${formatBoolean(record.uptime)}</td>
|
||||
<td>${record.latency_ms ?? '--'}${record.latency_ms === null && record.latency_ms_error ? `<div class="small">${record.latency_ms_error}</div>` : ''}</td>
|
||||
<td>${record.dns_lookup_ms ?? '--'}${record.dns_lookup_ms === null && record.dns_lookup_ms_error ? `<div class="small">${record.dns_lookup_ms_error}</div>` : ''}</td>
|
||||
<td class="${record.ssl_valid === true ? 'good' : record.ssl_valid === false ? 'bad' : 'warn'}">${formatBoolean(record.ssl_valid)}</td>
|
||||
<td>${record.ssl_expiry ?? '--'}${record.ssl_expiry === null && record.ssl_expiry_error ? `<div class="small">${record.ssl_expiry_error}</div>` : ''}</td>
|
||||
<td>${record.search_response_ms ?? '--'}${record.search_response_ms === null && record.search_response_ms_error ? `<div class="small">${record.search_response_ms_error}</div>` : ''}</td>
|
||||
`;
|
||||
body.appendChild(row);
|
||||
}
|
||||
}
|
||||
|
||||
function renderSummary(records) {
|
||||
const latest = records[records.length - 1] || {};
|
||||
const uptimeSummary = calculateUptimePercentage(records);
|
||||
|
||||
document.getElementById('uptimePercentage').textContent = uptimeSummary.percentage === null ? '--' : `${uptimeSummary.percentage}%`;
|
||||
document.getElementById('uptimeNote').textContent = uptimeSummary.count ? `${uptimeSummary.count} records in the last 24 hours` : 'No recent records';
|
||||
|
||||
setMetric('latestLatency', latest.latency_ms, 'ms', 'latencyNote', latest.latency_ms_error || null);
|
||||
setMetric('latestDns', latest.dns_lookup_ms, 'ms', 'dnsNote', latest.dns_lookup_ms_error || null);
|
||||
setMetric('sslStatus', latest.ssl_valid, 'bool', 'sslNote', latest.ssl_valid_error || null);
|
||||
document.getElementById('sslExpiry').textContent = latest.ssl_expiry || '--';
|
||||
document.getElementById('sslExpiryNote').textContent = latest.ssl_expiry_error || '';
|
||||
setMetric('latestSearch', latest.search_response_ms, 'ms', 'searchNote', latest.search_response_ms_error || null);
|
||||
|
||||
document.getElementById('lastRefresh').textContent = `Last updated: ${new Date().toLocaleTimeString()}`;
|
||||
}
|
||||
|
||||
async function loadMetrics() {
|
||||
const errorNote = document.getElementById('errorNote');
|
||||
try {
|
||||
const response = await fetch(`${metricsPath}?t=${Date.now()}`, { cache: 'no-store' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load metrics.json (${response.status})`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const records = Array.isArray(data) ? data : [];
|
||||
renderSummary(records);
|
||||
buildChart(records);
|
||||
renderTable(records);
|
||||
errorNote.textContent = '';
|
||||
} catch (error) {
|
||||
errorNote.textContent = `Unable to load metrics: ${error.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
loadMetrics();
|
||||
setInterval(loadMetrics, 60000);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
المرجع في مشكلة جديدة
حظر مستخدم