-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
495 lines (418 loc) · 13 KB
/
Copy pathapp.js
File metadata and controls
495 lines (418 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
const SUPABASE_URL = "https://odgfsyagqoicqjblkdhi.supabase.co";
const SUPABASE_ANON_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im9kZ2ZzeWFncW9pY3FqYmxrZGhpIiwicm9sZSI6ImFub24iLCJpYXQiOjE3ODEyMTYxNDYsImV4cCI6MjA5Njc5MjE0Nn0.BHpER7kf9J6jwaUpskROq4vRK_bH-YtcdBKXHuYfMXk";
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
// =======================================
// DOM ELEMENTS
// =======================================
const form = document.getElementById("report-form");
const downloadInput = document.getElementById("download");
const uploadInput = document.getElementById("upload");
const pingInput = document.getElementById("ping");
const jitterInput = document.getElementById("jitter");
const ispInput = document.getElementById("isp");
const ratingSelect = document.getElementById("rating");
const latInput = document.getElementById("lat");
const lngInput = document.getElementById("lng");
const locationStatus = document.getElementById("location-status");
const formMessage = document.getElementById("form-message");
const submitBtn = document.getElementById("submit-btn");
const statDownload = document.getElementById("stat-download");
const statUpload = document.getElementById("stat-upload");
const statPing = document.getElementById("stat-ping");
const statCount = document.getElementById("stat-count");
const refreshBtn = document.getElementById("refresh-btn");
const themeToggle = document.getElementById("theme-toggle");
const ispFilter = document.getElementById("isp-filter");
const ratingFilter = document.getElementById("rating-filter");
const heatmapToggle = document.getElementById("heatmap-toggle");
const searchInput = document.getElementById("search-input");
const searchBtn = document.getElementById("search-btn");
const exportBtn = document.getElementById("export-btn");
// =======================================
// GLOBAL STATE
// =======================================
let map;
let markerLayer;
let clickMarker;
let lightTiles;
let darkTiles;
let allReports = [];
let heatLayer = null;
// =======================================
// THEME SYSTEM
// =======================================
function applyTheme(theme) {
if (theme === "dark") {
document.documentElement.classList.add("dark");
themeToggle.textContent = "☀️ Light";
} else {
document.documentElement.classList.remove("dark");
themeToggle.textContent = "🌙 Dark";
}
localStorage.setItem("theme", theme);
}
themeToggle.addEventListener("click", () => {
const current = localStorage.getItem("theme") || "dark";
applyTheme(current === "dark" ? "light" : "dark");
});
// Load saved theme
applyTheme(localStorage.getItem("theme") || "dark");
// =======================================
// MAP SETUP
// =======================================
function initMap() {
map = L.map("map", {
zoomControl: true,
minZoom: 2,
maxBounds: [
[-85, -180],
[85, 180]
],
maxBoundsViscosity: 1.0,
worldCopyJump: false,
noWrap: true
}).setView([37.4, -122.0], 11);
// Light + Dark tile layers
lightTiles = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 19,
noWrap: true
});
darkTiles = L.tileLayer(
"https://tiles.stadiamaps.com/tiles/alidade_smooth_dark/{z}/{x}/{y}{r}.png",
{
maxZoom: 19,
noWrap: true
}
);
// Default theme
if (document.documentElement.classList.contains("dark")) {
darkTiles.addTo(map);
} else {
lightTiles.addTo(map);
}
// Switch tiles on theme toggle
function updateMapTheme() {
if (document.documentElement.classList.contains("dark")) {
map.removeLayer(lightTiles);
map.addLayer(darkTiles);
} else {
map.removeLayer(darkTiles);
map.addLayer(lightTiles);
}
}
themeToggle.addEventListener("click", updateMapTheme);
// Marker clustering
markerLayer = L.markerClusterGroup({
showCoverageOnHover: false,
maxClusterRadius: 50
});
map.addLayer(markerLayer);
// Click to set location
map.on("click", (e) => {
const { lat, lng } = e.latlng;
latInput.value = lat.toFixed(6);
lngInput.value = lng.toFixed(6);
locationStatus.textContent = `Location set: ${lat.toFixed(4)}, ${lng.toFixed(4)}`;
if (clickMarker) map.removeLayer(clickMarker);
clickMarker = L.circleMarker([lat, lng], {
radius: 7,
color: "#4f46e5",
weight: 2,
fillColor: "#a5b4fc",
fillOpacity: 0.8
}).addTo(map);
});
}
// =======================================
// FETCH REPORTS (FROM VIEW)
// =======================================
async function fetchReports() {
const { data, error } = await supabase
.from("reports_with_coords")
.select("*")
.order("created_at", { ascending: false })
.limit(2000);
if (error) {
console.error("Fetch error:", error);
return [];
}
return data.filter((r) => r.lat !== null && r.lng !== null);
}
// =======================================
// FILTERING
// =======================================
function filteredReports() {
const isp = ispFilter.value;
const rating = ratingFilter.value;
return allReports.filter((r) => {
if (isp && r.isp !== isp) return false;
if (rating && r.rating !== rating) return false;
return true;
});
}
// =======================================
// MAP MARKERS
// =======================================
function ratingColor(rating) {
return {
smooth: "#22c55e",
ok: "#eab308",
bad: "#f97316",
unusable: "#ef4444"
}[rating] || "#6b7280";
}
function addReportsToMap(reports) {
markerLayer.clearLayers();
reports.forEach((r) => {
const marker = L.circleMarker([r.lat, r.lng], {
radius: 6,
color: ratingColor(r.rating),
weight: 2,
fillColor: ratingColor(r.rating),
fillOpacity: 0.7
});
marker.bindPopup(`
<div>
<strong>${r.isp}</strong><br>
<small>${new Date(r.created_at).toLocaleString()}</small>
<hr>
<div>Download: <b>${r.download_mbps}</b> Mbps</div>
<div>Upload: <b>${r.upload_mbps}</b> Mbps</div>
<div>Ping: <b>${r.ping_ms}</b> ms</div>
<div>Jitter: <b>${r.jitter_ms}</b> ms</div>
<div>Experience: <b>${r.rating}</b></div>
</div>
`);
markerLayer.addLayer(marker);
});
}
// =======================================
// HEATMAP
// =======================================
function updateHeatmap(reports) {
if (heatLayer) {
map.removeLayer(heatLayer);
heatLayer = null;
}
const points = reports.map((r) => [
r.lat,
r.lng,
r.rating === "unusable"
? 1.0
: r.rating === "bad"
? 0.8
: r.rating === "ok"
? 0.4
: 0.2
]);
if (!points.length) return;
heatLayer = L.heatLayer(points, {
radius: 25,
blur: 15,
maxZoom: 17
});
if (heatmapToggle.dataset.active === "true") {
heatLayer.addTo(map);
}
}
// =======================================
// STATS
// =======================================
function updateStats(reports) {
if (!reports.length) {
statDownload.textContent = "–";
statUpload.textContent = "–";
statPing.textContent = "–";
statCount.textContent = "0";
return;
}
const avg = (arr) =>
arr.reduce((sum, v) => sum + (Number(v) || 0), 0) / arr.length;
statDownload.textContent = `${avg(reports.map((r) => r.download_mbps)).toFixed(1)} Mbps`;
statUpload.textContent = `${avg(reports.map((r) => r.upload_mbps)).toFixed(1)} Mbps`;
statPing.textContent = `${avg(reports.map((r) => r.ping_ms)).toFixed(1)} ms`;
statCount.textContent = reports.length;
}
// =======================================
// ISP FILTER POPULATION
// =======================================
function populateIspFilter() {
const current = ispFilter.value;
const isps = Array.from(
new Set(allReports.map((r) => r.isp).filter(Boolean))
).sort();
ispFilter.innerHTML = `<option value="">All ISPs</option>` +
isps.map((isp) => `<option value="${isp}">${isp}</option>`).join("");
if (current) ispFilter.value = current;
}
// =======================================
// CENTRAL RENDER FUNCTION
// =======================================
function applyFiltersAndRender() {
const reports = filteredReports();
addReportsToMap(reports);
updateStats(reports);
updateHeatmap(reports);
populateIspFilter();
}
// =======================================
// REFRESH DATA
// =======================================
async function refreshData() {
refreshBtn.disabled = true;
refreshBtn.textContent = "Refreshing…";
allReports = await fetchReports();
// Offline caching
if (allReports.length) {
localStorage.setItem("nim_reports_cache", JSON.stringify(allReports));
}
applyFiltersAndRender();
refreshBtn.disabled = false;
refreshBtn.textContent = "Refresh";
}
// =======================================
// SEARCH (Nominatim)
// =======================================
async function searchLocation() {
const q = searchInput.value.trim();
if (!q) return;
const url = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(
q
)}`;
try {
const res = await fetch(url, {
headers: {
"Accept-Language": "en"
}
});
const data = await res.json();
if (!data.length) return;
const { lat, lon } = data[0];
map.setView([parseFloat(lat), parseFloat(lon)], 14);
} catch (e) {
console.error("Search error", e);
}
}
searchBtn.addEventListener("click", searchLocation);
searchInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
e.preventDefault();
searchLocation();
}
});
// =======================================
// EXPORT CSV
// =======================================
function exportCsv() {
if (!allReports.length) return;
const headers = [
"id",
"download_mbps",
"upload_mbps",
"ping_ms",
"jitter_ms",
"isp",
"rating",
"lat",
"lng",
"created_at"
];
const rows = allReports.map((r) =>
headers
.map((h) => {
const v = r[h];
if (v == null) return "";
const s = String(v);
return s.includes(",") ? `"${s.replace(/"/g, '""')}"` : s;
})
.join(",")
);
const csv = [headers.join(","), ...rows].join("\n");
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `internet-map-${new Date().toISOString().slice(0, 19)}.csv`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
exportBtn.addEventListener("click", exportCsv);
// =======================================
// FORM SUBMISSION
// =======================================
function setFormMessage(text, type = "info") {
formMessage.textContent = text;
formMessage.style.color =
type === "error"
? "#ef4444"
: type === "success"
? "#22c55e"
: "#6b7280";
}
form.addEventListener("submit", async (e) => {
e.preventDefault();
const lat = parseFloat(latInput.value);
const lng = parseFloat(lngInput.value);
if (Number.isNaN(lat) || Number.isNaN(lng)) {
setFormMessage("Click on the map to set your location.", "error");
return;
}
const payload = {
download_mbps: parseFloat(downloadInput.value),
upload_mbps: parseFloat(uploadInput.value),
ping_ms: parseFloat(pingInput.value),
jitter_ms: parseFloat(jitterInput.value),
isp: ispInput.value.trim(),
rating: ratingSelect.value,
location: `POINT(${lng} ${lat})`
};
submitBtn.disabled = true;
submitBtn.textContent = "Submitting…";
const { error } = await supabase.from("reports").insert(payload);
if (error) {
console.error(error);
setFormMessage("Failed to submit report.", "error");
} else {
setFormMessage("Report submitted!", "success");
form.reset();
latInput.value = "";
lngInput.value = "";
locationStatus.textContent = "Click map to set location";
if (clickMarker) {
map.removeLayer(clickMarker);
clickMarker = null;
}
await refreshData();
}
submitBtn.disabled = false;
submitBtn.textContent = "Submit";
});
// =======================================
// INIT
// =======================================
window.addEventListener("DOMContentLoaded", async () => {
initMap();
// Load cached data first (offline mode)
const cached = localStorage.getItem("nim_reports_cache");
if (cached) {
try {
allReports = JSON.parse(cached);
applyFiltersAndRender();
} catch {}
}
await refreshData();
});
refreshBtn.addEventListener("click", refreshData);
ispFilter.addEventListener("change", applyFiltersAndRender);
ratingFilter.addEventListener("change", applyFiltersAndRender);
heatmapToggle.addEventListener("click", () => {
const active = heatmapToggle.dataset.active === "true";
const next = !active;
heatmapToggle.dataset.active = next ? "true" : "false";
heatmapToggle.textContent = next ? "Heatmap: On" : "Heatmap: Off";
const reports = filteredReports();
updateHeatmap(reports);
});