/
runningSessions.tsx
198 lines (167 loc) · 4.59 KB
/
runningSessions.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Default item to display the amount of total running kernel and terminal sessions.
*/
/**
* Part of Jupyterlab status bar defaults.
*/
import React from 'react';
import {
JupyterLabPlugin,
JupyterLab,
ApplicationShell
} from '@jupyterlab/application';
import { VDomRenderer, VDomModel } from '@jupyterlab/apputils';
import {
ServiceManager,
Kernel,
TerminalSession,
TerminalManager,
SessionManager
} from '@jupyterlab/services';
import {
GroupItem,
IconItem,
IStatusBar,
interactiveItem,
TextItem
} from '@jupyterlab/statusbar';
import { IDisposable } from '@phosphor/disposable';
import { ISignal } from '@phosphor/signaling';
const HALF_SPACING = 4;
// tslint:disable-next-line:variable-name
const RunningSessionsComponent = (
props: RunningSessionsComponent.IProps
): React.ReactElement<RunningSessionsComponent.IProps> => {
return (
<GroupItem spacing={HALF_SPACING} onClick={props.handleClick}>
<GroupItem spacing={HALF_SPACING}>
<TextItem source={props.terminals} />
<IconItem source={'terminal-item'} offset={{ x: 1, y: 3 }} />
</GroupItem>
<GroupItem spacing={HALF_SPACING}>
<TextItem source={props.kernels} />
<IconItem source={'kernel-item'} offset={{ x: 0, y: 2 }} />
</GroupItem>
</GroupItem>
);
};
namespace RunningSessionsComponent {
export interface IProps {
handleClick: () => void;
kernels: number;
terminals: number;
}
}
class RunningSessions extends VDomRenderer<RunningSessions.Model>
implements IRunningSessions {
constructor(opts: RunningSessions.IOptions) {
super();
this._serviceManager = opts.serviceManager;
this._host = opts.host;
this._serviceManager.sessions.runningChanged.connect(
this._onKernelsRunningChanged
);
this._serviceManager.terminals.runningChanged.connect(
this._onTerminalsRunningChanged
);
this.model = new RunningSessions.Model();
this.addClass(interactiveItem);
}
render() {
this.node.title = `${this.model!.terminals} Terminals, ${
this.model!.kernels
} Kernels`;
return (
<RunningSessionsComponent
kernels={this.model!.kernels}
terminals={this.model!.terminals}
handleClick={this._handleItemClick}
/>
);
}
dispose() {
super.dispose();
this._serviceManager.sessions.runningChanged.disconnect(
this._onKernelsRunningChanged
);
this._serviceManager.terminals.runningChanged.disconnect(
this._onTerminalsRunningChanged
);
}
private _onKernelsRunningChanged = (
manager: SessionManager,
kernels: Kernel.IModel[]
) => {
this.model!.kernels = kernels.length;
};
private _onTerminalsRunningChanged = (
manager: TerminalManager,
terminals: TerminalSession.IModel[]
) => {
this.model!.terminals = terminals.length;
};
private _handleItemClick = () => {
this._host.expandLeft();
this._host.activateById('jp-running-sessions');
};
private _host: ApplicationShell;
private _serviceManager: ServiceManager;
}
namespace RunningSessions {
export class Model extends VDomModel implements IRunningSessions.IModel {
get kernels() {
return this._kernels;
}
set kernels(kernels: number) {
const oldKernels = this._kernels;
this._kernels = kernels;
if (oldKernels !== this._kernels) {
this.stateChanged.emit(void 0);
}
}
get terminals() {
return this._terminals;
}
set terminals(terminals: number) {
const oldTerminals = this._terminals;
this._terminals = terminals;
if (oldTerminals !== this._terminals) {
this.stateChanged.emit(void 0);
}
}
private _terminals: number = 0;
private _kernels: number = 0;
}
export interface IOptions {
host: ApplicationShell;
serviceManager: ServiceManager;
}
}
export interface IRunningSessions extends IDisposable {
readonly model: IRunningSessions.IModel | null;
readonly modelChanged: ISignal<this, void>;
}
export namespace IRunningSessions {
export interface IModel {
readonly terminals: number;
readonly kernels: number;
}
}
/*
* Initialization data for the statusbar extension.
*/
export const runningSessionsItem: JupyterLabPlugin<void> = {
id: '@jupyterlab/statusbar:running-sessions-item',
autoStart: true,
requires: [IStatusBar],
activate: (app: JupyterLab, statusBar: IStatusBar) => {
const item = new RunningSessions({
host: app.shell,
serviceManager: app.serviceManager
});
statusBar.registerStatusItem('running-sessions-item', item, {
align: 'left',
rank: 0
});
}
};