( port: number, timeoutMs: number = METRICS_FETCH_TIMEOUT_MS )
| 31 | |
| 32 | /** Fetch resource metrics from the Jupyter server */ |
| 33 | export async function fetchMetrics( |
| 34 | port: number, |
| 35 | timeoutMs: number = METRICS_FETCH_TIMEOUT_MS |
| 36 | ): Promise<JupyterMetricsResponse | null> { |
| 37 | const controller = new AbortController() |
| 38 | const timeoutId = setTimeout(() => controller.abort(), timeoutMs) |
| 39 | |
| 40 | try { |
| 41 | const response = await fetch(`http://localhost:${port}/api/metrics/v1`, { |
| 42 | signal: controller.signal, |
| 43 | }) |
| 44 | clearTimeout(timeoutId) |
| 45 | if (!response.ok) return null |
| 46 | const json = await response.json() |
| 47 | const result = JupyterMetricsResponseSchema.safeParse(json) |
| 48 | if (!result.success) { |
| 49 | debug(`Schema validation failed: ${result.error.message}`) |
| 50 | return null |
| 51 | } |
| 52 | return result.data |
| 53 | } catch (error) { |
| 54 | clearTimeout(timeoutId) |
| 55 | debug(`Failed to fetch metrics: ${error instanceof Error ? error.message : String(error)}`) |
| 56 | return null |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** Format bytes as human-readable string */ |
| 61 | export function formatBytes(bytes: number): string { |
no test coverage detected