From 52739394ff0fc9939aae46c15059b7cc925fca22 Mon Sep 17 00:00:00 2001 From: sneak Date: Sat, 25 Jul 2026 17:40:49 +0700 Subject: [PATCH] =?UTF-8?q?WIP:=20report=20buffer=20=E2=80=94=20add=20clie?= =?UTF-8?q?nt=20geolocation=20fetch=20and=20telemetry=20report=20builder?= =?UTF-8?q?=20in=20main.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/main.js b/src/main.js index 3ab6bb8..2d616ab 100644 --- a/src/main.js +++ b/src/main.js @@ -140,6 +140,50 @@ const GATEWAY_CANDIDATES = [ "http://10.0.0.1", ]; +// --- Geolocation via API ----------------------------------------------------- + +async function fetchGeoInfo() { + try { + const res = await fetch("https://freeipapi.com/api/json/"); + if (!res.ok) return null; + return await res.json(); + } catch { + return null; + } +} + +// --- Telemetry Report -------------------------------------------------------- + +function getClientId() { + const key = "netwatch_client_id"; + let id = localStorage.getItem(key); + if (!id) { + id = crypto.randomUUID(); + localStorage.setItem(key, id); + } + return id; +} + +const clientId = getClientId(); + +function buildReport(state) { + return { + clientId, + timestamp: new Date().toISOString(), + geo: state.geoInfo, + hosts: state.wan.map((h) => ({ + name: h.name, + url: h.url, + status: h.status, + history: h.history.map((s) => ({ + t: s.timestamp, + latency: s.latency, + error: s.error || null, + })), + })), + }; +} + // --- Formatting Helpers ------------------------------------------------------ function formatUTCTimestamp(date) { @@ -269,6 +313,7 @@ class AppState { this.local = localHosts.map((h) => new HostState(h)); this.paused = false; this.tickCount = 0; + this.geoInfo = null; } get allHosts() { @@ -1140,6 +1185,9 @@ async function init() { } const state = new AppState(localHosts); + fetchGeoInfo().then((geo) => { + state.geoInfo = geo; + }); buildUI(state); log.info("UI built, starting tick loop"); @@ -1194,6 +1242,11 @@ async function init() { doTick(); let tickIntervalId = setInterval(doTick, CONFIG.updateInterval); + setInterval(() => { + if (state.tickCount > 1) + console.log("NetWatch report:", buildReport(state)); + }, 60000); + document .getElementById("interval-select") .addEventListener("change", (e) => {