@@ -5,13 +5,13 @@ define([
55 function setupDOM ( ) {
66 $ ( '#maintoolbar-container' ) . append (
77 $ ( '<div>' ) . attr ( 'id' , 'nbresuse-display' )
8- . addClass ( 'btn-group' )
9- . addClass ( 'pull-right' )
10- . append (
11- $ ( '<strong>' ) . text ( 'Memory: ' )
12- ) . append (
8+ . addClass ( 'btn-group' )
9+ . addClass ( 'pull-right' )
10+ . append (
11+ $ ( '<strong>' ) . text ( 'Memory: ' )
12+ ) . append (
1313 $ ( '<span>' ) . attr ( 'id' , 'nbresuse-mem' )
14- . attr ( 'title' , 'Actively used Memory (updates every 5s)' )
14+ . attr ( 'title' , 'Actively used Memory (updates every 5s)' )
1515 )
1616 ) ;
1717 // FIXME: Do something cleaner to get styles in here?
@@ -24,49 +24,35 @@ define([
2424 }
2525
2626 function humanFileSize ( size ) {
27- var i = Math . floor ( Math . log ( size ) / Math . log ( 1024 ) ) ;
28- return ( size / Math . pow ( 1024 , i ) ) . toFixed ( 1 ) * 1 + ' ' + [ 'B' , 'kB' , 'MB' , 'GB' , 'TB' ] [ i ] ;
27+ var i = Math . floor ( Math . log ( size ) / Math . log ( 1024 ) ) ;
28+ return ( size / Math . pow ( 1024 , i ) ) . toFixed ( 1 ) * 1 + ' ' + [ 'B' , 'kB' , 'MB' , 'GB' , 'TB' ] [ i ] ;
2929 }
3030
31-
32- function metric ( metric_name , text , multiple = false ) {
33- var regex = new RegExp ( "^" + metric_name + "\{?([^ \}]*)\}? (.*)$" , "gm" ) ;
34- var matches = [ ] ;
35- var match ;
36-
37- do {
38- match = regex . exec ( text ) ;
39- if ( match ) {
40- matches . push ( match )
41- }
42- }
43- while ( match ) ;
44-
45- if ( ! multiple ) {
46- if ( matches . length > 0 )
47- return matches [ 0 ] ;
48- return null ;
49- } else
50- return matches ;
51- }
52-
53- var displayMetrics = function ( ) {
31+ var displayMetrics = function ( ) {
5432 if ( document . hidden ) {
5533 // Don't poll when nobody is looking
5634 return ;
5735 }
58- $ . ajax ( {
59- url : utils . get_body_data ( 'baseUrl' ) + 'metrics' ,
60- success : function ( data ) {
61- let totalMemoryUsage = metric ( "total_memory_usage" , data ) ;
62- let maxMemoryUsage = metric ( "max_memory_usage" , data ) ;
36+ $ . getJSON ( {
37+ url : utils . get_body_data ( 'baseUrl' ) + 'api/nbresuse/v1' ,
38+ success : function ( data ) {
39+ totalMemoryUsage = humanFileSize ( data [ 'rss' ] ) ;
40+
41+ var limits = data [ 'limits' ] ;
42+ var display = totalMemoryUsage ;
6343
64- if ( maxMemoryUsage [ 2 ] <= 0 )
65- return ;
66- totalMemoryUsage = humanFileSize ( parseFloat ( totalMemoryUsage [ 2 ] ) ) ;
67- maxMemoryUsage = humanFileSize ( parseFloat ( maxMemoryUsage [ 2 ] ) ) ;
44+ if ( limits [ 'memory' ] ) {
45+ if ( limits [ 'memory' ] [ 'rss' ] ) {
46+ maxMemoryUsage = humanFileSize ( limits [ 'memory' ] [ 'rss' ] ) ;
47+ display += "/" + maxMemoryUsage
48+ }
49+ if ( limits [ 'memory' ] [ 'warn' ] ) {
50+ $ ( '#nbresuse-display' ) . addClass ( 'nbresuse-warn' ) ;
51+ } else {
52+ $ ( '#nbresuse-display' ) . removeClass ( 'nbresuse-warn' ) ;
53+ }
54+ }
6855
69- var display = totalMemoryUsage + "/" + maxMemoryUsage ;
7056 $ ( '#nbresuse-mem' ) . text ( display ) ;
7157 }
7258 } ) ;
@@ -78,7 +64,7 @@ define([
7864 // Update every five seconds, eh?
7965 setInterval ( displayMetrics , 1000 * 5 ) ;
8066
81- document . addEventListener ( "visibilitychange" , function ( ) {
67+ document . addEventListener ( "visibilitychange" , function ( ) {
8268 // Update instantly when user activates notebook tab
8369 // FIXME: Turn off update timer completely when tab not in focus
8470 if ( ! document . hidden ) {
@@ -90,4 +76,4 @@ define([
9076 return {
9177 load_ipython_extension : load_ipython_extension ,
9278 } ;
93- } ) ;
79+ } ) ;
0 commit comments