@@ -17,6 +17,7 @@ define([
1717 $ ( '#maintoolbar-container' ) . append (
1818 $ ( '<div>' ) . attr ( 'id' , 'jupyter-resource-usage-display-cpu' )
1919 . addClass ( 'btn-group' )
20+ . addClass ( 'jupyter-resource-usage-hide' )
2021 . addClass ( 'pull-right' ) . append (
2122 $ ( '<strong>' ) . text ( ' CPU: ' )
2223 ) . append (
@@ -28,6 +29,9 @@ define([
2829 $ ( 'head' ) . append (
2930 $ ( '<style>' ) . html ( '.jupyter-resource-usage-warn { background-color: #FFD2D2; color: #D8000C; }' )
3031 ) ;
32+ $ ( 'head' ) . append (
33+ $ ( '<style>' ) . html ( '.jupyter-resource-usage-hide { display: none; }' )
34+ ) ;
3135 $ ( 'head' ) . append (
3236 $ ( '<style>' ) . html ( '#jupyter-resource-usage-display { padding: 2px 8px; }' )
3337 ) ;
@@ -67,28 +71,29 @@ define([
6771 }
6872
6973 $ ( '#jupyter-resource-usage-mem' ) . text ( display ) ;
70- }
71- } ) ;
72- $ . getJSON ( {
73- url : utils . get_body_data ( 'baseUrl' ) + 'api/metrics/v1' ,
74- success : function ( data ) {
75- var cpuPercent = data [ 'cpu_percent' ] ;
76- var maxCpu = data [ 'cpu_count' ] ;
77- var limits = data [ 'limits' ] ;
78- var display = parseFloat ( cpuPercent ) . toFixed ( 0 ) + "% (" ;
7974
80- if ( limits [ 'cpu' ] ) {
81- if ( limits [ 'cpu' ] [ 'cpu' ] ) {
82- display += ( Math . round ( parseFloat ( cpuPercent ) / 100 ) ) . toString ( ) + " / " + maxCpu + ")" ;
83- }
84- if ( limits [ 'cpu' ] [ 'warn' ] ) {
85- $ ( '#jupyter-resource-usage-display-cpu' ) . addClass ( 'jupyter-resource-usage-warn' ) ;
86- } else {
87- $ ( '#jupyter-resource-usage-display-cpu' ) . removeClass ( 'jupyter-resource-usage-warn' ) ;
75+ // Handle CPU display
76+ var cpuPercent = data [ 'cpu_percent' ] ;
77+ if ( cpuPercent ) {
78+ // Remove hide CSS class if the metrics API gives us a CPU percent to display
79+ $ ( '#jupyter-resource-usage-display-cpu' ) . removeClass ( 'jupyter-resource-usage-hide' ) ;
80+ var maxCpu = data [ 'cpu_count' ] ;
81+ var limits = data [ 'limits' ] ;
82+ // Display CPU usage as "{percent}% ({usedCpu} / {maxCPU})" e.g. "123% (1 / 8)"
83+ var percentString = parseFloat ( cpuPercent ) . toFixed ( 0 ) ;
84+ var usedCpu = Math . round ( parseFloat ( cpuPercent ) / 100 ) . toString ( ) ;
85+ var display = `${ percentString } % (${ usedCpu } / ${ maxCpu } )` ;
86+ // Handle limit warning
87+ if ( limits [ 'cpu' ] ) {
88+ if ( limits [ 'cpu' ] [ 'warn' ] ) {
89+ $ ( '#jupyter-resource-usage-display-cpu' ) . addClass ( 'jupyter-resource-usage-warn' ) ;
90+ } else {
91+ $ ( '#jupyter-resource-usage-display-cpu' ) . removeClass ( 'jupyter-resource-usage-warn' ) ;
92+ }
8893 }
94+
95+ $ ( '#jupyter-resource-usage-cpu' ) . text ( display ) ;
8996 }
90-
91- $ ( '#jupyter-resource-usage-cpu' ) . text ( display ) ;
9297 }
9398 } ) ;
9499 } ;
0 commit comments