@@ -17,6 +17,7 @@ define([
17
17
$ ( '#maintoolbar-container' ) . append (
18
18
$ ( '<div>' ) . attr ( 'id' , 'jupyter-resource-usage-display-cpu' )
19
19
. addClass ( 'btn-group' )
20
+ . addClass ( 'jupyter-resource-usage-hide' )
20
21
. addClass ( 'pull-right' ) . append (
21
22
$ ( '<strong>' ) . text ( ' CPU: ' )
22
23
) . append (
@@ -28,6 +29,9 @@ define([
28
29
$ ( 'head' ) . append (
29
30
$ ( '<style>' ) . html ( '.jupyter-resource-usage-warn { background-color: #FFD2D2; color: #D8000C; }' )
30
31
) ;
32
+ $ ( 'head' ) . append (
33
+ $ ( '<style>' ) . html ( '.jupyter-resource-usage-hide { display: none; }' )
34
+ ) ;
31
35
$ ( 'head' ) . append (
32
36
$ ( '<style>' ) . html ( '#jupyter-resource-usage-display { padding: 2px 8px; }' )
33
37
) ;
@@ -67,28 +71,29 @@ define([
67
71
}
68
72
69
73
$ ( '#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 ) + "% (" ;
79
74
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
+ }
88
93
}
94
+
95
+ $ ( '#jupyter-resource-usage-cpu' ) . text ( display ) ;
89
96
}
90
-
91
- $ ( '#jupyter-resource-usage-cpu' ) . text ( display ) ;
92
97
}
93
98
} ) ;
94
99
} ;
0 commit comments