fix: trim chart runs in place and reduce to 25 to match SSR density

This commit is contained in:
nate 2026-03-18 20:06:23 +04:00
parent f2fcf1a0b9
commit cbb1a26582
1 changed files with 23 additions and 21 deletions

View File

@ -179,25 +179,36 @@
'us-west': '🇺🇸 US West', 'ap-southeast': '🇸🇬 AP Southeast' 'us-west': '🇺🇸 US West', 'ap-southeast': '🇸🇬 AP Southeast'
}; };
const MAX_RUNS = 50; const MAX_RUNS = 25;
let chartPings = <%~ JSON.stringify(chartPings.map(p => ({ const chartPings = <%~ JSON.stringify(chartPings.map(p => ({
latency_ms: p.latency_ms, region: p.region || '__none__', latency_ms: p.latency_ms, region: p.region || '__none__',
checked_at: p.checked_at, up: p.up, run_id: p.run_id || null, checked_at: p.checked_at, up: p.up, run_id: p.run_id || null,
status_code: p.status_code status_code: p.status_code
}))) %>; }))) %>;
// Trim initial data to MAX_RUNS // Trim chartPings in place to keep only the newest MAX_RUNS runs
(function() { function trimChartRuns() {
const seen = []; const s = new Set(); // Collect runs sorted by avg time
const runAvg = {};
for (const p of chartPings) { for (const p of chartPings) {
const rid = p.run_id || p.checked_at; const rid = p.run_id || p.checked_at;
if (!s.has(rid)) { s.add(rid); seen.push(rid); } if (!runAvg[rid]) runAvg[rid] = { sum: 0, n: 0 };
runAvg[rid].sum += new Date(p.checked_at).getTime();
runAvg[rid].n++;
} }
if (seen.length > MAX_RUNS) { const sorted = Object.keys(runAvg).sort((a, b) =>
const stale = new Set(seen.slice(0, seen.length - MAX_RUNS)); (runAvg[a].sum / runAvg[a].n) - (runAvg[b].sum / runAvg[b].n)
chartPings = chartPings.filter(p => !stale.has(p.run_id || p.checked_at)); );
if (sorted.length <= MAX_RUNS) return;
const stale = new Set(sorted.slice(0, sorted.length - MAX_RUNS));
// Splice in place so all references see the change
for (let i = chartPings.length - 1; i >= 0; i--) {
if (stale.has(chartPings[i].run_id || chartPings[i].checked_at)) {
chartPings.splice(i, 1);
}
} }
})(); }
trimChartRuns();
function renderChart() { function renderChart() {
const canvas = document.getElementById('chart-canvas'); const canvas = document.getElementById('chart-canvas');
@ -480,22 +491,13 @@
while (tbody.children.length > 100) tbody.removeChild(tbody.lastChild); while (tbody.children.length > 100) tbody.removeChild(tbody.lastChild);
} }
// Chart — push new ping, evict oldest complete runs to cap at ~50 runs // Chart — push new ping, trim oldest runs, re-render
chartPings.push({ chartPings.push({
latency_ms: ping.latency_ms, region: ping.region || '__none__', latency_ms: ping.latency_ms, region: ping.region || '__none__',
checked_at: ping.checked_at, up: ping.up, run_id: ping.run_id || null, checked_at: ping.checked_at, up: ping.up, run_id: ping.run_id || null,
status_code: ping.status_code status_code: ping.status_code
}); });
// Count distinct runs and remove oldest runs as a group trimChartRuns();
const seen = []; const runSet = new Set();
for (const p of chartPings) {
const rid = p.run_id || p.checked_at;
if (!runSet.has(rid)) { runSet.add(rid); seen.push(rid); }
}
if (seen.length > MAX_RUNS) {
const stale = new Set(seen.slice(0, seen.length - MAX_RUNS));
chartPings = chartPings.filter(p => !stale.has(p.run_id || p.checked_at));
}
renderChart(); renderChart();
}); });
</script> </script>