fix: stale ping part 2

This commit is contained in:
nate 2026-03-28 16:13:19 +04:00
parent e82a9e92f8
commit 8791cdc509
1 changed files with 24 additions and 16 deletions

View File

@ -30,21 +30,20 @@
<% 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);
// Pick the region with the lowest avg latency (same logic as sparklineFromPings) // Pick the region with the lowest avg latency (same logic as sparklineFromPings)
// Only consider the 3 most recent pings per region to pick best region // Pick best region from the 3 most recent pings overall (not per region)
const byRegion = {}; const recentPings = latencies.slice(-3);
for (const p of latencies) { const recentRegions = {};
for (const p of recentPings) {
const key = p.region || '__none__'; const key = p.region || '__none__';
if (!byRegion[key]) byRegion[key] = []; if (!recentRegions[key]) recentRegions[key] = [];
byRegion[key].push(p.latency_ms); recentRegions[key].push(p.latency_ms);
} }
let bestRegion = '__none__', bestAvg = Infinity; let bestRegion = '__none__', bestAvg = Infinity;
for (const [region, vals] of Object.entries(byRegion)) { for (const [region, vals] of Object.entries(recentRegions)) {
const recent = vals.slice(-3); const avg = vals.reduce((a, b) => a + b, 0) / vals.length;
const avg = recent.reduce((a, b) => a + b, 0) / recent.length;
if (avg < bestAvg) { bestAvg = avg; bestRegion = region; } if (avg < bestAvg) { bestAvg = avg; bestRegion = region; }
} }
const bestVals = byRegion[bestRegion] || []; const avgLatency = latencies.length ? latencies[latencies.length - 1].latency_ms : null;
const avgLatency = bestVals.length ? bestVals[bestVals.length - 1] : null;
%> %>
<a href="/dashboard/monitors/<%= m.id %>" data-monitor-id="<%= m.id %>" class="block monitor-card rounded-xl p-4 group"> <a href="/dashboard/monitors/<%= m.id %>" data-monitor-id="<%= m.id %>" class="block monitor-card rounded-xl p-4 group">
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
@ -114,15 +113,24 @@
function getBestRegion(monitorId) { function getBestRegion(monitorId) {
const regions = sparkData[monitorId] || {}; const regions = sparkData[monitorId] || {};
let bestRegion = '__none__', bestAvg = Infinity; // Collect all pings with timestamps to find the 3 most recent overall
const all = [];
for (const [region, vals] of Object.entries(regions)) { for (const [region, vals] of Object.entries(regions)) {
if (!vals.length) continue; for (let i = 0; i < vals.length; i++) all.push({ region, val: vals[i], idx: i });
const recent = vals.slice(-3); }
const avg = recent.reduce((a, b) => a + b, 0) / recent.length; // Use the last 3 entries (vals are appended in order)
const recent = all.slice(-3);
let bestRegion = '__none__', bestAvg = Infinity;
const byRegion = {};
for (const p of recent) {
if (!byRegion[p.region]) byRegion[p.region] = [];
byRegion[p.region].push(p.val);
}
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; } if (avg < bestAvg) { bestAvg = avg; bestRegion = region; }
} }
const vals = (regions[bestRegion] || []); const latest = all.length ? all[all.length - 1].val : null;
const latest = vals.length ? vals[vals.length - 1] : null;
return { region: bestRegion, latest }; return { region: bestRegion, latest };
} }