mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
feat: Add anonymous RX stats
ref:
This commit is contained in:
@@ -1,333 +1,8 @@
|
||||
---
|
||||
import BaseLayout from "../layouts/BaseLayout.astro";
|
||||
|
||||
const windowOptions = [
|
||||
{ key: "24h", label: "Last 24 hours" },
|
||||
{ key: "7d", label: "Last 7 days" },
|
||||
{ key: "30d", label: "Last 30 days" },
|
||||
];
|
||||
import { DashboardShell } from "@/components/dashboard/dashboard-shell";
|
||||
---
|
||||
|
||||
<BaseLayout title="MeshCore SAR RX Stats">
|
||||
<div class="shell">
|
||||
<div class="banner" id="error-banner" hidden></div>
|
||||
|
||||
<section class="hero">
|
||||
<div class="hero-card">
|
||||
<div class="eyebrow">MeshCore SAR</div>
|
||||
<h1>Anonymous RX traffic stats</h1>
|
||||
<p class="hero-copy">
|
||||
Static Astro dashboard for RX live-traffic packet types and path-hash modes.
|
||||
Location comes from Cloudflare ingress metadata, while device identity is reduced to key6.
|
||||
</p>
|
||||
<div class="window-tabs">
|
||||
{windowOptions.map((option) => (
|
||||
<button class="window-tab" data-window={option.key} data-active={option.key === "24h"}>
|
||||
{option.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<aside class="hero-card">
|
||||
<div class="eyebrow">Current range</div>
|
||||
<div class="hero-value" id="range-label">Loading…</div>
|
||||
<p class="hero-note" id="range-note">Fetching dashboard data from D1.</p>
|
||||
</aside>
|
||||
</section>
|
||||
|
||||
<section class="kpi-grid">
|
||||
<article class="kpi">
|
||||
<div class="eyebrow">Reports</div>
|
||||
<div class="kpi-value" id="kpi-reports">0</div>
|
||||
<p class="kpi-note">Accepted upload windows in this range.</p>
|
||||
</article>
|
||||
<article class="kpi">
|
||||
<div class="eyebrow">Reporters</div>
|
||||
<div class="kpi-value" id="kpi-reporters">0</div>
|
||||
<p class="kpi-note">Unique key6 reporters.</p>
|
||||
</article>
|
||||
<article class="kpi">
|
||||
<div class="eyebrow">Decoded RX</div>
|
||||
<div class="kpi-value" id="kpi-decoded">0</div>
|
||||
<p class="kpi-note">Packets grouped by known payload type.</p>
|
||||
</article>
|
||||
<article class="kpi">
|
||||
<div class="eyebrow">Decode Fail</div>
|
||||
<div class="kpi-value" id="kpi-failures">0</div>
|
||||
<p class="kpi-note">Malformed or undecodable RX packets.</p>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<section class="content-grid">
|
||||
<article class="panel">
|
||||
<h2>Traffic trend</h2>
|
||||
<div id="trend-chart" class="empty">No data yet.</div>
|
||||
</article>
|
||||
<article class="panel">
|
||||
<h2>Path mode distribution</h2>
|
||||
<div id="path-mode-list" class="pill-row"></div>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<section class="content-grid">
|
||||
<article class="panel">
|
||||
<h2>Top packet types</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Label</th>
|
||||
<th>Column</th>
|
||||
<th>Total</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="packet-type-table"></tbody>
|
||||
</table>
|
||||
</article>
|
||||
<article class="panel">
|
||||
<h2>Recent reporters</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>key6</th>
|
||||
<th>City</th>
|
||||
<th>Country</th>
|
||||
<th>Packets</th>
|
||||
<th>Last seen</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="reporter-table"></tbody>
|
||||
</table>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<section class="content-grid">
|
||||
<article class="panel">
|
||||
<h2>Cloudflare geo map</h2>
|
||||
<div class="map" id="geo-map">
|
||||
<div class="continent one"></div>
|
||||
<div class="continent two"></div>
|
||||
<div class="continent three"></div>
|
||||
<div class="continent four"></div>
|
||||
</div>
|
||||
<p class="legend">
|
||||
Dots reflect Cloudflare ingress latitude and longitude, not device GPS coordinates.
|
||||
</p>
|
||||
</article>
|
||||
<article class="panel">
|
||||
<h2>Packet type mix</h2>
|
||||
<div id="packet-mix" class="meter-stack"></div>
|
||||
</article>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<script is:inline>
|
||||
const state = { windowKey: "24h" };
|
||||
const elements = {
|
||||
errorBanner: document.getElementById("error-banner"),
|
||||
rangeLabel: document.getElementById("range-label"),
|
||||
rangeNote: document.getElementById("range-note"),
|
||||
reports: document.getElementById("kpi-reports"),
|
||||
reporters: document.getElementById("kpi-reporters"),
|
||||
decoded: document.getElementById("kpi-decoded"),
|
||||
failures: document.getElementById("kpi-failures"),
|
||||
trendChart: document.getElementById("trend-chart"),
|
||||
pathModeList: document.getElementById("path-mode-list"),
|
||||
packetTypeTable: document.getElementById("packet-type-table"),
|
||||
reporterTable: document.getElementById("reporter-table"),
|
||||
packetMix: document.getElementById("packet-mix"),
|
||||
geoMap: document.getElementById("geo-map"),
|
||||
};
|
||||
|
||||
const windowButtons = [...document.querySelectorAll("[data-window]")];
|
||||
|
||||
function setActiveWindow(windowKey) {
|
||||
state.windowKey = windowKey;
|
||||
for (const button of windowButtons) {
|
||||
button.dataset.active = String(button.dataset.window === windowKey);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadDashboard(windowKey) {
|
||||
setActiveWindow(windowKey);
|
||||
elements.errorBanner.hidden = true;
|
||||
elements.rangeLabel.textContent = "Loading…";
|
||||
elements.rangeNote.textContent = "Fetching dashboard data from D1.";
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/dashboard?window=${windowKey}`, {
|
||||
headers: {
|
||||
accept: "application/json",
|
||||
},
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Dashboard request failed (${response.status})`);
|
||||
}
|
||||
const summary = await response.json();
|
||||
renderDashboard(summary);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
elements.errorBanner.hidden = false;
|
||||
elements.errorBanner.textContent = `Unable to load dashboard data: ${message}`;
|
||||
elements.rangeLabel.textContent = "Unavailable";
|
||||
elements.rangeNote.textContent = "The dashboard API did not return data.";
|
||||
}
|
||||
}
|
||||
|
||||
function renderDashboard(summary) {
|
||||
elements.rangeLabel.textContent = summary.filter.label;
|
||||
elements.rangeNote.textContent = `Showing reports with window end after ${formatTimestamp(summary.filter.sinceIso)}. Generated ${formatTimestamp(summary.generatedAt)}.`;
|
||||
elements.reports.textContent = String(summary.reportCount);
|
||||
elements.reporters.textContent = String(summary.uniqueDevices);
|
||||
elements.decoded.textContent = String(summary.decodedPackets);
|
||||
elements.failures.textContent = String(summary.decodeFailures);
|
||||
|
||||
renderTrend(summary.chartPoints);
|
||||
renderPathModes(summary.pathModeTotals);
|
||||
renderPacketTypes(summary.packetTypeTotals);
|
||||
renderReporters(summary.recentReporters);
|
||||
renderPacketMix(summary.packetTypeTotals);
|
||||
renderMap(summary.locationPoints);
|
||||
}
|
||||
|
||||
function renderTrend(points) {
|
||||
if (!points.length) {
|
||||
elements.trendChart.innerHTML = '<div class="empty">No data yet for this window.</div>';
|
||||
return;
|
||||
}
|
||||
const maxValue = Math.max(...points.map((point) => point.totalPackets), 1);
|
||||
elements.trendChart.innerHTML = `
|
||||
<div class="bars">
|
||||
${points
|
||||
.map((point) => {
|
||||
const height = Math.max((point.totalPackets / maxValue) * 180, 6);
|
||||
return `
|
||||
<div class="bar-card">
|
||||
<div class="bar-value">${point.totalPackets}</div>
|
||||
<div class="bar" style="height:${height}px"></div>
|
||||
<div class="bar-label">${escapeHtml(point.label.slice(5))}</div>
|
||||
</div>`;
|
||||
})
|
||||
.join("")}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function renderPathModes(entries) {
|
||||
const rows = entries.filter((entry) => entry.total > 0);
|
||||
if (!rows.length) {
|
||||
elements.pathModeList.innerHTML = '<div class="empty">No path mode samples yet.</div>';
|
||||
return;
|
||||
}
|
||||
elements.pathModeList.innerHTML = rows
|
||||
.map(
|
||||
(entry) => `
|
||||
<div class="pill">
|
||||
<strong>${entry.total}</strong>
|
||||
${escapeHtml(entry.label)}
|
||||
</div>`,
|
||||
)
|
||||
.join("");
|
||||
}
|
||||
|
||||
function renderPacketTypes(entries) {
|
||||
const rows = entries.filter((entry) => entry.total > 0).slice(0, 8);
|
||||
if (!rows.length) {
|
||||
elements.packetTypeTable.innerHTML = '<tr><td colspan="3" class="empty">No packet data yet.</td></tr>';
|
||||
return;
|
||||
}
|
||||
elements.packetTypeTable.innerHTML = rows
|
||||
.map(
|
||||
(entry) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(entry.label)}</td>
|
||||
<td><code>${entry.key}</code></td>
|
||||
<td>${entry.total}</td>
|
||||
</tr>`,
|
||||
)
|
||||
.join("");
|
||||
}
|
||||
|
||||
function renderReporters(reporters) {
|
||||
if (!reporters.length) {
|
||||
elements.reporterTable.innerHTML = '<tr><td colspan="5" class="empty">No reporter activity yet.</td></tr>';
|
||||
return;
|
||||
}
|
||||
elements.reporterTable.innerHTML = reporters
|
||||
.map(
|
||||
(reporter) => `
|
||||
<tr>
|
||||
<td><code>${escapeHtml(reporter.key6)}</code></td>
|
||||
<td>${escapeHtml(reporter.city)}</td>
|
||||
<td>${escapeHtml(reporter.country)}</td>
|
||||
<td>${reporter.packetTotal}</td>
|
||||
<td>${escapeHtml(formatTimestamp(reporter.lastSeen))}</td>
|
||||
</tr>`,
|
||||
)
|
||||
.join("");
|
||||
}
|
||||
|
||||
function renderPacketMix(entries) {
|
||||
const rows = entries.filter((entry) => entry.total > 0).slice(0, 6);
|
||||
if (!rows.length) {
|
||||
elements.packetMix.innerHTML = '<div class="empty">No packet mix data yet.</div>';
|
||||
return;
|
||||
}
|
||||
const maxValue = Math.max(...rows.map((entry) => entry.total), 1);
|
||||
elements.packetMix.innerHTML = rows
|
||||
.map(
|
||||
(entry) => `
|
||||
<div class="meter-row">
|
||||
<div class="meter-head">
|
||||
<span>${escapeHtml(entry.label)}</span>
|
||||
<strong>${entry.total}</strong>
|
||||
</div>
|
||||
<div class="meter-track">
|
||||
<div class="meter-fill" style="width:${(entry.total / maxValue) * 100}%"></div>
|
||||
</div>
|
||||
</div>`,
|
||||
)
|
||||
.join("");
|
||||
}
|
||||
|
||||
function renderMap(points) {
|
||||
const dots = [...elements.geoMap.querySelectorAll(".map-dot")];
|
||||
for (const dot of dots) {
|
||||
dot.remove();
|
||||
}
|
||||
for (const point of points) {
|
||||
const dot = document.createElement("div");
|
||||
dot.className = "map-dot";
|
||||
dot.style.left = `${((point.longitude + 180) / 360) * 100}%`;
|
||||
dot.style.top = `${((90 - point.latitude) / 180) * 100}%`;
|
||||
dot.dataset.label = `${point.key6} · ${point.city}`;
|
||||
elements.geoMap.appendChild(dot);
|
||||
}
|
||||
}
|
||||
|
||||
function escapeHtml(value) {
|
||||
return String(value)
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
|
||||
function formatTimestamp(value) {
|
||||
const date = new Date(value);
|
||||
const month = `${date.getUTCMonth() + 1}`.padStart(2, "0");
|
||||
const day = `${date.getUTCDate()}`.padStart(2, "0");
|
||||
const hour = `${date.getUTCHours()}`.padStart(2, "0");
|
||||
const minute = `${date.getUTCMinutes()}`.padStart(2, "0");
|
||||
return `${date.getUTCFullYear()}-${month}-${day} ${hour}:${minute} UTC`;
|
||||
}
|
||||
|
||||
for (const button of windowButtons) {
|
||||
button.addEventListener("click", () => {
|
||||
loadDashboard(button.dataset.window ?? "24h");
|
||||
});
|
||||
}
|
||||
|
||||
loadDashboard(state.windowKey);
|
||||
</script>
|
||||
<DashboardShell client:load />
|
||||
</BaseLayout>
|
||||
|
||||
Reference in New Issue
Block a user