attempt fix: cont

This commit is contained in:
2026-03-28 16:17:15 +04:00
parent 8791cdc509
commit 3dc0757b4a
3 changed files with 52 additions and 48 deletions
+19 -30
View File
@@ -28,22 +28,9 @@
</div>
<% } else { %>
<% it.monitors.forEach(function(m) {
const latencies = (m.pings || []).filter(p => p.latency_ms != null);
// Pick the region with the lowest avg latency (same logic as sparklineFromPings)
// Pick best region from the 3 most recent pings overall (not per region)
const recentPings = latencies.slice(-3);
const recentRegions = {};
for (const p of recentPings) {
const key = p.region || '__none__';
if (!recentRegions[key]) recentRegions[key] = [];
recentRegions[key].push(p.latency_ms);
}
let bestRegion = '__none__', bestAvg = Infinity;
for (const [region, vals] of Object.entries(recentRegions)) {
const avg = vals.reduce((a, b) => a + b, 0) / vals.length;
if (avg < bestAvg) { bestAvg = avg; bestRegion = region; }
}
const avgLatency = latencies.length ? latencies[latencies.length - 1].latency_ms : null;
const pingsForSparkline = (m.pings || []).filter(p => p.latency_ms != null);
const best = it.pickBestRegion(pingsForSparkline);
const avgLatency = best.latest;
%>
<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">
@@ -55,7 +42,7 @@
</div>
</div>
<div class="flex items-center gap-6 shrink-0 ml-4">
<div class="hidden sm:block stat-sparkline" style="width:120px;height:32px"><%~ it.sparklineSSR(latencies) %></div>
<div class="hidden sm:block stat-sparkline" style="width:120px;height:32px"><%~ it.sparklineSSR(pingsForSparkline) %></div>
<div class="text-right" style="width:72px">
<div class="text-sm text-gray-300 stat-latency"><%= avgLatency != null ? avgLatency + 'ms' : '—' %></div>
<div class="text-xs text-gray-500 stat-last"><%~ m.last_ping ? it.timeAgoSSR(m.last_ping.checked_at) : 'no pings' %></div>
@@ -113,24 +100,26 @@
function getBestRegion(monitorId) {
const regions = sparkData[monitorId] || {};
// Collect all pings with timestamps to find the 3 most recent overall
const all = [];
// Only consider regions that appear in the 3 most recent pings overall
// sparkData stores vals per region in chronological order; build a merged timeline
const timeline = [];
for (const [region, vals] of Object.entries(regions)) {
for (let i = 0; i < vals.length; i++) all.push({ region, val: vals[i], idx: i });
for (const val of vals) timeline.push({ region, val });
}
// Use the last 3 entries (vals are appended in order)
const recent = all.slice(-3);
if (!timeline.length) return { region: '__none__', latest: null };
const recentRegions = new Set(timeline.slice(-3).map(p => p.region));
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;
for (const [region, vals] of Object.entries(regions)) {
if (!recentRegions.has(region) || !vals.length) continue;
const recent = vals.slice(-3);
const avg = recent.reduce((a, b) => a + b, 0) / recent.length;
if (avg < bestAvg) { bestAvg = avg; bestRegion = region; }
}
const latest = all.length ? all[all.length - 1].val : null;
const vals = regions[bestRegion] || [];
const latest = vals.length ? vals[vals.length - 1] : null;
return { region: bestRegion, latest };
}