fix: show latest latency from fastest region instead of average on home

This commit is contained in:
nate 2026-03-18 19:36:24 +04:00
parent 688245b0c2
commit 5b7a211c21
1 changed files with 27 additions and 7 deletions

View File

@ -25,8 +25,20 @@
<% } else { %> <% } else { %>
<% it.monitors.forEach(function(m) { <% it.monitors.forEach(function(m) {
const latencies = (m.pings || []).filter(p => p.latency_ms != null); const latencies = (m.pings || []).filter(p => p.latency_ms != null);
const latencyVals = latencies.map(p => p.latency_ms); // Pick the region with the lowest avg latency (same logic as sparklineFromPings)
const avgLatency = latencyVals.length ? Math.round(latencyVals.reduce((a, b) => a + b, 0) / latencyVals.length) : null; const byRegion = {};
for (const p of latencies) {
const key = p.region || '__none__';
if (!byRegion[key]) byRegion[key] = [];
byRegion[key].push(p.latency_ms);
}
let bestRegion = '__none__', bestAvg = Infinity;
for (const [region, vals] of Object.entries(byRegion)) {
const avg = vals.reduce((a, b) => a + b, 0) / vals.length;
if (avg < bestAvg) { bestAvg = avg; bestRegion = region; }
}
const bestVals = byRegion[bestRegion] || [];
const avgLatency = bestVals.length ? bestVals[bestVals.length - 1] : null;
%> %>
<a href="/dashboard/monitors/<%= m.id %>" data-monitor-id="<%= m.id %>" class="block bg-gray-900 hover:bg-gray-800/80 border border-gray-800 rounded-xl p-4 transition-colors group"> <a href="/dashboard/monitors/<%= m.id %>" data-monitor-id="<%= m.id %>" class="block bg-gray-900 hover:bg-gray-800/80 border border-gray-800 rounded-xl p-4 transition-colors group">
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
@ -96,16 +108,22 @@
sparkData[mid][region] = vals; sparkData[mid][region] = vals;
}); });
function redrawSparkline(card, monitorId) { function getBestRegion(monitorId) {
const regions = sparkData[monitorId] || {}; const regions = sparkData[monitorId] || {};
// Pick region with lowest average latency (same as SSR)
let bestRegion = '__none__', bestAvg = Infinity; let bestRegion = '__none__', bestAvg = Infinity;
for (const [region, vals] of Object.entries(regions)) { for (const [region, vals] of Object.entries(regions)) {
if (!vals.length) continue; if (!vals.length) continue;
const avg = vals.reduce((a, b) => a + b, 0) / vals.length; const avg = vals.reduce((a, b) => a + b, 0) / vals.length;
if (avg < bestAvg) { bestAvg = avg; bestRegion = region; } if (avg < bestAvg) { bestAvg = avg; bestRegion = region; }
} }
const vals = regions[bestRegion]; const vals = (regions[bestRegion] || []);
const latest = vals.length ? vals[vals.length - 1] : null;
return { region: bestRegion, latest };
}
function redrawSparkline(card, monitorId) {
const { region: bestRegion } = getBestRegion(monitorId);
const vals = (sparkData[monitorId] || {})[bestRegion];
if (!vals || !vals.length) return; if (!vals || !vals.length) return;
const color = REGION_COLORS[bestRegion] || '#60a5fa'; const color = REGION_COLORS[bestRegion] || '#60a5fa';
const W = 120, H = 32; const W = 120, H = 32;
@ -129,8 +147,7 @@
const dot = card.querySelector('.status-dot'); const dot = card.querySelector('.status-dot');
if (dot) dot.className = `status-dot w-2.5 h-2.5 rounded-full ${ping.up ? 'bg-green-500' : 'bg-red-500'}`; if (dot) dot.className = `status-dot w-2.5 h-2.5 rounded-full ${ping.up ? 'bg-green-500' : 'bg-red-500'}`;
// Last latency + last ping time // Last ping time
if (ping.latency_ms != null) card.querySelector('.stat-latency').textContent = ping.latency_ms + 'ms';
card.querySelector('.stat-last').innerHTML = timeAgo(ping.checked_at); card.querySelector('.stat-last').innerHTML = timeAgo(ping.checked_at);
// Update sparkline data per region, then redraw picking best region // Update sparkline data per region, then redraw picking best region
@ -142,6 +159,9 @@
sparkData[mid][region].push(ping.latency_ms); sparkData[mid][region].push(ping.latency_ms);
if (sparkData[mid][region].length > 20) sparkData[mid][region].shift(); if (sparkData[mid][region].length > 20) sparkData[mid][region].shift();
redrawSparkline(card, mid); redrawSparkline(card, mid);
// Show best region's latest latency
const { latest } = getBestRegion(mid);
if (latest != null) card.querySelector('.stat-latency').textContent = latest + 'ms';
} }
}); });
</script> </script>