@@ -13,6 +13,8 @@ import {
1313 type CommandLaneDiagnostics ,
1414} from "../../lib/gateway-diagnostics.ts" ;
1515import { renderCommandLaneRows } from "./lane-table.ts" ;
16+ import "./sparkline-tile.ts" ;
17+ import type { SparklineSample } from "./sparkline-tile.ts" ;
1618
1719type DebugOverlaySectionContext = {
1820 client : GatewayBrowserClient ;
@@ -23,7 +25,7 @@ type TypedDebugOverlaySectionDescriptor<T> = {
2325 id : string ;
2426 titleKey : string ;
2527 load : ( context : DebugOverlaySectionContext , signal : AbortSignal ) => Promise < T > ;
26- render : ( value : T , statusHistory : readonly DebugOverlayStatusSnapshot [ ] ) => TemplateResult ;
28+ render : ( value : T , statusHistory : readonly DebugOverlayStatusSample [ ] ) => TemplateResult ;
2729} ;
2830
2931export type DebugOverlaySectionDescriptor = TypedDebugOverlaySectionDescriptor < unknown > ;
@@ -45,6 +47,7 @@ type EventLoopSnapshot = {
4547 cpuCoreRatio ?: number ;
4648 delayP99Ms ?: number ;
4749 delayMaxMs ?: number ;
50+ reasons ?: string [ ] ;
4851} ;
4952
5053export type DebugOverlayStatusSnapshot = {
@@ -57,6 +60,11 @@ export type DebugOverlayStatusSnapshot = {
5760 uptimeMs ?: number ;
5861} ;
5962
63+ export type DebugOverlayStatusSample = {
64+ at : number ;
65+ status : DebugOverlayStatusSnapshot ;
66+ } ;
67+
6068type ActiveSession = {
6169 key ?: string ;
6270 sessionId ?: string ;
@@ -82,104 +90,86 @@ function renderLanes(diagnostics: CommandLaneDiagnostics): TemplateResult {
8290 ` ;
8391}
8492
85- function renderSparkline (
86- history : readonly DebugOverlayStatusSnapshot [ ] ,
87- kind : "cpu" | "memory" | "delay" ,
88- label : string ,
89- readValue : ( sample : DebugOverlayStatusSnapshot ) => number | undefined ,
90- formatValue : ( value : number ) => string | undefined ,
91- ) {
92- const values = history . map ( readValue ) . filter ( ( value ) : value is number => Number . isFinite ( value ) ) ;
93- const current = values . at ( - 1 ) ;
94- if ( values . length < 2 || current === undefined ) {
95- return nothing ;
93+ function collectSamples (
94+ history : readonly DebugOverlayStatusSample [ ] ,
95+ read : ( status : DebugOverlayStatusSnapshot ) => number | undefined ,
96+ ) : SparklineSample [ ] {
97+ const samples : SparklineSample [ ] = [ ] ;
98+ for ( const entry of history ) {
99+ const value = read ( entry . status ) ;
100+ if ( typeof value === "number" && Number . isFinite ( value ) ) {
101+ samples . push ( { value, at : entry . at } ) ;
102+ }
96103 }
97- const maximum = Math . max ( ...values , 1 ) ;
98- const points = values
99- . map ( ( value , index ) => {
100- const x = ( index / ( values . length - 1 ) ) * 100 ;
101- const y = 30 - ( value / maximum ) * 26 ;
102- return `${ x } ,${ y } ` ;
103- } )
104- . join ( " " ) ;
105- return html `
106- < figure class ="debug-overlay__graph debug-overlay__graph-- ${ kind } ">
107- < figcaption >
108- < span > ${ label } </ span >
109- < span class ="mono "> ${ formatValue ( current ) } </ span >
110- </ figcaption >
111- < svg viewBox ="0 0 100 32 " preserveAspectRatio ="none " aria-hidden ="true ">
112- < polygon points ="0,32 ${ points } 100,32 "> </ polygon >
113- < polyline points =${ points } > </ polyline >
114- </ svg >
115- </ figure >
116- ` ;
104+ return samples ;
105+ }
106+
107+ function formatPercent ( value : number ) : string {
108+ return `${ Math . round ( value * 100 ) } %` ;
109+ }
110+
111+ function formatMegabytes ( bytes : number ) : string {
112+ return t ( "debug.overlay.memoryMb" , { value : String ( Math . round ( bytes / 1_048_576 ) ) } ) ;
113+ }
114+
115+ function formatDelayMs ( value : number ) : string {
116+ return formatDurationCompact ( value ) ?? t ( "common.na" ) ;
117117}
118118
119119function renderStatus (
120120 status : DebugOverlayStatusSnapshot ,
121- history : readonly DebugOverlayStatusSnapshot [ ] ,
121+ history : readonly DebugOverlayStatusSample [ ] ,
122122) : TemplateResult {
123123 const eventLoop = status . eventLoop ;
124- const utilization =
124+ const reasons = eventLoop ?. reasons ?? [ ] ;
125+ const cpuDegraded = reasons . includes ( "cpu" ) || reasons . includes ( "event_loop_utilization" ) ;
126+ const delayDegraded = reasons . includes ( "event_loop_delay" ) ;
127+ const loopSub =
125128 typeof eventLoop ?. utilization === "number"
126- ? ` ${ Math . round ( eventLoop . utilization * 100 ) } %`
127- : t ( "common.na" ) ;
128- const delay =
129- typeof eventLoop ?. delayP99Ms === "number"
130- ? formatDurationCompact ( eventLoop . delayP99Ms )
131- : t ( "common.na" ) ;
132- const maxDelay =
129+ ? t ( "debug.overlay.loopShort" , { value : formatPercent ( eventLoop . utilization ) } )
130+ : "" ;
131+ const heapSub =
132+ typeof status . processMemory ?. heapUsedBytes === "number"
133+ ? t ( "debug.overlay.heapShort" , { value : formatMegabytes ( status . processMemory . heapUsedBytes ) } )
134+ : "" ;
135+ const maxSub =
133136 typeof eventLoop ?. delayMaxMs === "number"
134- ? formatDurationCompact ( eventLoop . delayMaxMs )
135- : t ( "common.na" ) ;
137+ ? t ( "debug.overlay.maxShort" , { value : formatDelayMs ( eventLoop . delayMaxMs ) } )
138+ : "" ;
136139 return html `
137- ${ history . length >= 2
138- ? html `< div class ="debug-overlay__graphs ">
139- ${ renderSparkline (
140- history ,
141- "cpu" ,
142- t ( "debug.overlay.cpu" ) ,
143- ( sample ) => sample . eventLoop ?. cpuCoreRatio ,
144- ( value ) => `${ Math . round ( value * 100 ) } %` ,
145- ) }
146- ${ renderSparkline (
147- history ,
148- "memory" ,
149- t ( "debug.overlay.memory" ) ,
150- ( sample ) => sample . processMemory ?. rssBytes ,
151- ( value ) =>
152- t ( "debug.overlay.memoryMb" , { value : String ( Math . round ( value / 1_048_576 ) ) } ) ,
153- ) }
154- ${ renderSparkline (
155- history ,
156- "delay" ,
157- t ( "debug.overlay.delayP99" ) ,
158- ( sample ) => sample . eventLoop ?. delayP99Ms ,
159- formatDurationCompact ,
160- ) }
140+ < div class ="debug-overlay__vitals ">
141+ < openclaw-debug-sparkline
142+ class ="debug-overlay__vital debug-overlay__vital--cpu "
143+ data-degraded =${ cpuDegraded ? "" : nothing }
144+ .label =${ t ( "debug.overlay.cpu" ) }
145+ .sub=${ loopSub }
146+ .samples=${ collectSamples ( history , ( sample ) => sample . eventLoop ?. cpuCoreRatio ) }
147+ .format=${ formatPercent }
148+ .floorMax=${ 1 }
149+ > </ openclaw-debug-sparkline >
150+ < openclaw-debug-sparkline
151+ class ="debug-overlay__vital debug-overlay__vital--memory "
152+ .label =${ t ( "debug.overlay.memory" ) }
153+ .sub =${ heapSub }
154+ .samples=${ collectSamples ( history , ( sample ) => sample . processMemory ?. rssBytes ) }
155+ .format=${ formatMegabytes }
156+ autorange
157+ > </ openclaw-debug-sparkline >
158+ < openclaw-debug-sparkline
159+ class ="debug-overlay__vital debug-overlay__vital--delay "
160+ data-degraded =${ delayDegraded ? "" : nothing }
161+ .label =${ t ( "debug.overlay.delayP99" ) }
162+ .sub=${ maxSub }
163+ .samples=${ collectSamples ( history , ( sample ) => sample . eventLoop ?. delayP99Ms ) }
164+ .format=${ formatDelayMs }
165+ .floorMax=${ 20 }
166+ > </ openclaw-debug-sparkline >
167+ </ div >
168+ ${ typeof status . uptimeMs === "number"
169+ ? html `< div class ="debug-overlay__vitals-footer mono ">
170+ ${ t ( "debug.overlay.uptime" ) } ${ formatDurationHuman ( status . uptimeMs ) }
161171 </ div > `
162172 : nothing }
163- < dl class ="debug-overlay__metrics ">
164- < div >
165- < dt > ${ t ( "debug.overlay.utilization" ) } </ dt >
166- < dd class ="mono "> ${ utilization } </ dd >
167- </ div >
168- < div >
169- < dt > ${ t ( "debug.overlay.delayP99" ) } </ dt >
170- < dd class ="mono "> ${ delay } </ dd >
171- </ div >
172- < div >
173- < dt > ${ t ( "debug.overlay.delayMax" ) } </ dt >
174- < dd class ="mono "> ${ maxDelay } </ dd >
175- </ div >
176- ${ typeof status . uptimeMs === "number"
177- ? html `< div >
178- < dt > ${ t ( "debug.overlay.uptime" ) } </ dt >
179- < dd class ="mono "> ${ formatDurationHuman ( status . uptimeMs ) } </ dd >
180- </ div > `
181- : "" }
182- </ dl >
183173 ` ;
184174}
185175
0 commit comments