@@ -3,29 +3,75 @@ import {
33 JupyterFrontEnd ,
44 JupyterFrontEndPlugin ,
55} from '@jupyterlab/application' ;
6+
7+ import { IToolbarWidgetRegistry } from '@jupyterlab/apputils' ;
8+
9+ import { ISettingRegistry } from '@jupyterlab/settingregistry' ;
10+
611import { INotebookTracker } from '@jupyterlab/notebook' ;
12+
713import { LabIcon } from '@jupyterlab/ui-components' ;
14+
815import { ICommandPalette } from '@jupyterlab/apputils' ;
16+
917import { IConsoleTracker } from '@jupyterlab/console' ;
10- import { KernelUsagePanel } from './panel' ;
11- import tachometer from '../style/tachometer.svg' ;
1218
1319import { IStatusBar } from '@jupyterlab/statusbar' ;
1420
1521import { ITranslator } from '@jupyterlab/translation' ;
1622
17- import { MemoryUsage } from './memoryUsage' ;
23+ import { JSONObject } from '@lumino/coreutils' ;
24+
25+ import { KernelUsagePanel } from './panel' ;
26+
27+ import tachometer from '../style/tachometer.svg' ;
28+
29+ import { ResourceUsage } from './model' ;
30+
31+ import { ResourceUsageStatus } from './resourceUsage' ;
32+
1833import { KernelWidgetTracker } from './tracker' ;
1934
35+ import { CpuView } from './cpuView' ;
36+
37+ import { MemoryView } from './memoryView' ;
38+
39+ /**
40+ * Disable system monitor panels by default.
41+ */
42+ const DEFAULT_ENABLE_SYSTEM_MONITOR = false ;
43+
44+ /**
45+ * The default refresh rate.
46+ */
47+ const DEFAULT_REFRESH_RATE = 5000 ;
48+
49+ /**
50+ * The default memory label.
51+ */
52+ const DEFAULT_MEMORY_LABEL = 'Mem: ' ;
53+
54+ /**
55+ * The default CPU label.
56+ */
57+ const DEFAULT_CPU_LABEL = 'CPU: ' ;
58+
59+ /**
60+ * An interface for resource settings.
61+ */
62+ interface IResourceSettings extends JSONObject {
63+ label : string ;
64+ }
65+
2066namespace CommandIDs {
2167 export const getKernelUsage = 'kernel-usage:get' ;
2268}
2369
2470/**
2571 * Initialization data for the jupyter-resource-usage extension.
2672 */
27- const memoryStatusPlugin : JupyterFrontEndPlugin < void > = {
28- id : '@jupyter-server/resource-usage:memory- status-item' ,
73+ const resourceStatusPlugin : JupyterFrontEndPlugin < void > = {
74+ id : '@jupyter-server/resource-usage:status-item' ,
2975 autoStart : true ,
3076 requires : [ IStatusBar , ITranslator ] ,
3177 activate : (
@@ -34,9 +80,9 @@ const memoryStatusPlugin: JupyterFrontEndPlugin<void> = {
3480 translator : ITranslator
3581 ) => {
3682 const trans = translator . load ( 'jupyter-resource-usage' ) ;
37- const item = new MemoryUsage ( trans ) ;
83+ const item = new ResourceUsageStatus ( trans ) ;
3884
39- statusBar . registerStatusItem ( memoryStatusPlugin . id , {
85+ statusBar . registerStatusItem ( resourceStatusPlugin . id , {
4086 item,
4187 align : 'left' ,
4288 rank : 2 ,
@@ -46,6 +92,54 @@ const memoryStatusPlugin: JupyterFrontEndPlugin<void> = {
4692 } ,
4793} ;
4894
95+ /**
96+ * Initialization data for the jupyterlab-system-monitor extension.
97+ */
98+ const systemMonitorPlugin : JupyterFrontEndPlugin < void > = {
99+ id : '@jupyter-server/resource-usage:topbar-item' ,
100+ autoStart : true ,
101+ requires : [ IToolbarWidgetRegistry ] ,
102+ optional : [ ISettingRegistry ] ,
103+ activate : async (
104+ app : JupyterFrontEnd ,
105+ toolbarRegistry : IToolbarWidgetRegistry ,
106+ settingRegistry : ISettingRegistry
107+ ) => {
108+ let enablePlugin = DEFAULT_ENABLE_SYSTEM_MONITOR ;
109+ let refreshRate = DEFAULT_REFRESH_RATE ;
110+ let cpuLabel = DEFAULT_CPU_LABEL ;
111+ let memoryLabel = DEFAULT_MEMORY_LABEL ;
112+
113+ if ( settingRegistry ) {
114+ const settings = await settingRegistry . load ( systemMonitorPlugin . id ) ;
115+ enablePlugin = settings . get ( 'enable' ) . composite as boolean ;
116+ refreshRate = settings . get ( 'refreshRate' ) . composite as number ;
117+ const cpuSettings = settings . get ( 'cpu' ) . composite as IResourceSettings ;
118+ cpuLabel = cpuSettings . label ;
119+ const memorySettings = settings . get ( 'memory' )
120+ . composite as IResourceSettings ;
121+ memoryLabel = memorySettings . label ;
122+ }
123+
124+ const model = new ResourceUsage . Model ( { refreshRate } ) ;
125+ await model . refresh ( ) ;
126+
127+ if ( enablePlugin && model . cpuAvailable ) {
128+ toolbarRegistry . addFactory ( 'TopBar' , 'cpu' , ( ) => {
129+ const cpu = CpuView . createCpuView ( model , cpuLabel ) ;
130+ return cpu ;
131+ } ) ;
132+ }
133+
134+ if ( enablePlugin && model . memoryAvailable ) {
135+ toolbarRegistry . addFactory ( 'TopBar' , 'memory' , ( ) => {
136+ const memory = MemoryView . createMemoryView ( model , memoryLabel ) ;
137+ return memory ;
138+ } ) ;
139+ }
140+ } ,
141+ } ;
142+
49143const kernelUsagePlugin : JupyterFrontEndPlugin < void > = {
50144 id : '@jupyter-server/resource-usage:kernel-panel-item' ,
51145 autoStart : true ,
@@ -101,7 +195,8 @@ const kernelUsagePlugin: JupyterFrontEndPlugin<void> = {
101195} ;
102196
103197const plugins : JupyterFrontEndPlugin < any > [ ] = [
104- memoryStatusPlugin ,
198+ resourceStatusPlugin ,
199+ systemMonitorPlugin ,
105200 kernelUsagePlugin ,
106201] ;
107202export default plugins ;
0 commit comments