export function metric(sample, name) { const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') const found = sample.metrics?.match(new RegExp(`^${escaped}\\s+(\\S+)`, 'm')) return found ? Number(found[1]) : null } export function rss(sample) { const found = sample.status?.match(/^VmRSS:\s+(\d+)/m) return found ? Number(found[1]) * 1024 : null } function slope(points) { const x = points.reduce((sum, p) => sum + p[0], 0) / points.length const y = points.reduce((sum, p) => sum + p[1], 0) / points.length return points.reduce((sum, p) => sum + (p[0] - x) * (p[1] - y), 0) / points.reduce((sum, p) => sum + (p[0] - x) ** 2, 0) * 60 } export function assess(samples, minimumSeconds = 300) { const last = samples.at(-1) if (!last || last.seconds < minimumSeconds) return { stable: false, reason: 'warmup' } const window = samples.filter(s => s.seconds >= last.seconds - 180) if (window.length < 30 || window[0].seconds > last.seconds - 175) return { stable: false, reason: 'coverage' } if (window.some((s, i) => s.health !== 200 || !s.metrics || rss(s) === null || (i && s.seconds - window[i - 1].seconds > 10))) return { stable: false, reason: 'health-or-sampling-gap' } const names = ['relay_seq', 'relay_pool_queued_bytes', 'relay_connected_inbound'] if (window.some(s => names.some(name => metric(s, name) === null))) return { stable: false, reason: 'missing-metrics' } const progress = [] for (let i = 0; i < 3; i++) { const end = last.seconds - i * 60 const bin = window.filter(s => s.seconds >= end - 60 && s.seconds <= end) progress.push(metric(bin.at(-1), 'relay_seq') - metric(bin[0], 'relay_seq')) } const memory = window.map(s => [s.seconds, rss(s)]) const queue = window.map(s => [s.seconds, metric(s, 'relay_pool_queued_bytes')]) const workers = window.map(s => metric(s, 'relay_connected_inbound')) const memorySlope = slope(memory) / 2**20 const queueSlope = slope(queue) / 2**20 const memoryRange = (Math.max(...memory.map(p => p[1])) - Math.min(...memory.map(p => p[1]))) / 2**20 const queueChange = (queue.at(-1)[1] - queue[0][1]) / 2**20 const cache = window.map(s => { const values = ['signing_keys', 'did_uids', 'host_ids'].map(component => metric(s, `relay_cache_logical_bytes{component="${component}"}`)) return values.some(v => v === null) ? null : [s.seconds, values.reduce((a, b) => a + b, 0)] }).filter(Boolean) const cacheSlope = cache.length >= 30 ? slope(cache) / 2**20 : null const active = progress.every(delta => delta >= 600) const boundedMemory = Math.abs(memorySlope) <= .5 && memoryRange <= 12 const boundedQueue = Math.abs(queueSlope) <= 1 && Math.abs(queueChange) <= 2 && Math.max(...queue.map(p => p[1])) <= 64 * 2**20 const boundedWorkers = Math.max(...workers) - Math.min(...workers) <= Math.max(10, Math.max(...workers) * .01) const boundedCache = cacheSlope !== null && Math.abs(cacheSlope) <= .5 return { stable: active && boundedMemory && boundedQueue && boundedWorkers && boundedCache, reason: !active ? 'processing-not-advancing' : !boundedQueue ? 'queue-changing' : !boundedMemory ? 'rss-changing' : !boundedCache ? 'cache-changing' : !boundedWorkers ? 'workers-changing' : 'stable-window', window_seconds: last.seconds - window[0].seconds, progress_per_minute: progress, rss_mib_per_min: memorySlope, rss_range_mib: memoryRange, queue_mib_per_min: queueSlope, queue_change_mib: queueChange, cache_mib_per_min: cacheSlope } }