attempt fix: cont
This commit is contained in:
@@ -12,33 +12,48 @@ export function sparkline(values: number[], width = 120, height = 32, color = '#
|
||||
return `<svg width="${width}" height="${height}" class="inline-block" data-vals="${values.join(',')}" data-region="${region}"><polyline points="${points}" fill="none" stroke="${color}" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
|
||||
}
|
||||
|
||||
// Given pings with region+latency, pick the region with the lowest avg latency
|
||||
// and return its sparkline in that region's color.
|
||||
export function sparklineFromPings(pings: Array<{latency_ms?: number|null, region?: string|null}>, width = 120, height = 32): string {
|
||||
const COLORS: Record<string, string> = {
|
||||
'eu-central': '#3b82f6',
|
||||
'us-west': '#f59e0b',
|
||||
};
|
||||
const REGION_COLORS: Record<string, string> = {
|
||||
'eu-central': '#3b82f6',
|
||||
'us-west': '#f59e0b',
|
||||
};
|
||||
|
||||
// Group by region
|
||||
// Pick the best region: the one with the lowest avg latency across its last 3 pings.
|
||||
// Only considers regions that have at least one ping in the most recent 3 pings overall,
|
||||
// so stale regions that haven't reported recently are excluded.
|
||||
export function pickBestRegion(pings: Array<{latency_ms?: number|null, region?: string|null}>): { region: string, values: number[], latest: number | null } {
|
||||
const withLatency = pings.filter(p => p.latency_ms != null);
|
||||
if (!withLatency.length) return { region: '__none__', values: [], latest: null };
|
||||
|
||||
// Group all pings by region
|
||||
const byRegion: Record<string, number[]> = {};
|
||||
for (const p of pings) {
|
||||
if (p.latency_ms == null) continue;
|
||||
for (const p of withLatency) {
|
||||
const key = p.region || '__none__';
|
||||
if (!byRegion[key]) byRegion[key] = [];
|
||||
byRegion[key].push(p.latency_ms);
|
||||
byRegion[key].push(p.latency_ms!);
|
||||
}
|
||||
|
||||
if (!Object.keys(byRegion).length) return '';
|
||||
// Only consider regions that appear in the 3 most recent pings
|
||||
const recentRegions = new Set(
|
||||
withLatency.slice(-3).map(p => p.region || '__none__')
|
||||
);
|
||||
|
||||
// Pick region with lowest average latency
|
||||
let bestRegion = '__none__';
|
||||
let bestAvg = Infinity;
|
||||
for (const [region, vals] of Object.entries(byRegion)) {
|
||||
const avg = vals.reduce((a, b) => a + b, 0) / vals.length;
|
||||
if (!recentRegions.has(region)) 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 color = COLORS[bestRegion] || '#60a5fa';
|
||||
return sparkline(byRegion[bestRegion], width, height, color, bestRegion);
|
||||
const values = byRegion[bestRegion] || [];
|
||||
return { region: bestRegion, values, latest: values.length ? values[values.length - 1] : null };
|
||||
}
|
||||
|
||||
// Given pings with region+latency, pick the best region and render its sparkline.
|
||||
export function sparklineFromPings(pings: Array<{latency_ms?: number|null, region?: string|null}>, width = 120, height = 32): string {
|
||||
const { region, values } = pickBestRegion(pings);
|
||||
if (!values.length) return '';
|
||||
const color = REGION_COLORS[region] || '#60a5fa';
|
||||
return sparkline(values, width, height, color, region);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user