fix: mutate polyline points in place on SSE ping, no fetch, no flicker
This commit is contained in:
parent
2c32bc1115
commit
017d489e2e
|
|
@ -9,5 +9,5 @@ export function sparkline(values: number[], width = 120, height = 32): string {
|
|||
const y = height - ((v - min) / range) * (height - 4) - 2;
|
||||
return `${x},${y}`;
|
||||
}).join(' ');
|
||||
return `<svg width="${width}" height="${height}" class="inline-block"><polyline points="${points}" fill="none" stroke="#60a5fa" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
|
||||
return `<svg width="${width}" height="${height}" class="inline-block" data-vals="${values.join(',')}"><polyline points="${points}" fill="none" stroke="#60a5fa" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,9 +75,8 @@
|
|||
} catch {}
|
||||
}, 30000);
|
||||
|
||||
// SSE: on each ping, update text fields and fetch fresh sparkline
|
||||
const _fetchingSparkline = new Set();
|
||||
watchAccount(async (ping) => {
|
||||
// SSE: on each ping, update text fields and redraw sparkline in place
|
||||
watchAccount((ping) => {
|
||||
const card = document.querySelector(`[data-monitor-id="${ping.monitor_id}"]`);
|
||||
if (!card) return;
|
||||
|
||||
|
|
@ -89,22 +88,31 @@
|
|||
if (ping.latency_ms != null) card.querySelector('.stat-latency').textContent = ping.latency_ms + 'ms';
|
||||
card.querySelector('.stat-last').innerHTML = timeAgo(ping.checked_at);
|
||||
|
||||
// Sparkline
|
||||
// Sparkline — append point to existing polyline, drop oldest, no refetch
|
||||
if (ping.latency_ms != null) {
|
||||
const sparkEl = card.querySelector('.stat-sparkline');
|
||||
if (!sparkEl || _fetchingSparkline.has(ping.monitor_id)) return;
|
||||
_fetchingSparkline.add(ping.monitor_id);
|
||||
try {
|
||||
const res = await fetch(`/dashboard/monitors/${ping.monitor_id}/sparkline`, { credentials: 'same-origin' });
|
||||
if (res.ok) {
|
||||
const tmp = document.createElement('div');
|
||||
tmp.innerHTML = await res.text();
|
||||
const newSvg = tmp.firstElementChild;
|
||||
const oldSvg = sparkEl.querySelector('svg');
|
||||
if (newSvg && oldSvg) oldSvg.replaceWith(newSvg);
|
||||
else if (newSvg) sparkEl.appendChild(newSvg);
|
||||
const polyline = sparkEl?.querySelector('polyline');
|
||||
if (polyline) {
|
||||
const pts = polyline.getAttribute('points').trim().split(' ').filter(Boolean);
|
||||
const W = 120, H = 32;
|
||||
// Parse existing points
|
||||
let coords = pts.map(p => p.split(',').map(Number));
|
||||
// Extract just y-values (latencies are encoded in y relative to scale)
|
||||
// Easier: maintain a data attr on the svg with the raw values
|
||||
const svg = sparkEl.querySelector('svg');
|
||||
let vals = svg?.dataset.vals ? svg.dataset.vals.split(',').map(Number) : [];
|
||||
vals.push(ping.latency_ms);
|
||||
if (vals.length > 20) vals.shift();
|
||||
if (svg) svg.dataset.vals = vals.join(',');
|
||||
// Redraw polyline in place
|
||||
const max = Math.max(...vals, 1);
|
||||
const min = Math.min(...vals, 0);
|
||||
const range = max - min || 1;
|
||||
const step = W / Math.max(vals.length - 1, 1);
|
||||
const newPts = vals.map((v, i) => `${i * step},${H - ((v - min) / range) * (H - 4) - 2}`).join(' ');
|
||||
polyline.setAttribute('points', newPts);
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
_fetchingSparkline.delete(ping.monitor_id);
|
||||
});
|
||||
</script>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue