This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
/
dbstatus.tsx
405 lines (364 loc) · 12.3 KB
/
dbstatus.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import React from 'react';
import { VDomRenderer, VDomModel, ISessionContext } from '@jupyterlab/apputils';
import { URLExt } from '@jupyterlab/coreutils';
import { interactiveItem, TextItem } from '@jupyterlab/statusbar';
import { ServerConnection } from '@jupyterlab/services';
import { ILabShell } from '@jupyterlab/application';
import { INotebookTracker } from '@jupyterlab/notebook';
import { Dialog, showDialog } from '@jupyterlab/apputils';
import { Poll } from '@lumino/polling';
import { NotebookPanel } from '@jupyterlab/notebook';
class Counter {
private _count = 0;
private _limit = 0;
private _delay = 0;
constructor() {
this.reset()
}
next() {
this._count += 1;
var result = (this._count % this._delay == 0)
if ((this._limit != -1) && (this._count > this._limit)) {
switch (this._delay) {
case 1: {
this._delay = 2;
this._limit = 30; // 60 seconds every 2 seconds
break;
}
case 2: {
this._delay = 5;
this._limit = 24; // 120 seconds every 5 seconds
break;
}
case 5: {
this._delay = 10; // every 10 seconds until reset
this._limit = -1;
break;
}
default: {
this._delay = 10; // every 10 seconds until reset
this._limit = -1;
break;
}
}
this._count = 1;
console.debug("Databrickslabs-jupyterlab: monitoring every", this._delay, "seconds")
}
return result
}
reset() {
this._count = 0
this._limit = 60 // 60 seconds every seconds
this._delay = 1
}
}
/**
* A VDomRenderer for showing memory usage by a kernel.
*/
export class DbStatus extends VDomRenderer<DbStatus.Model> {
/**
* Construct a new databricks status item.
*/
constructor(labShell: ILabShell, notebookTracker: INotebookTracker) {
super(new DbStatus.Model({
refreshRate: 1000,
labShell: labShell,
notebookTracker: notebookTracker
}));
this.addClass(interactiveItem);
}
/**
* Render the databricks status item.
*/
render() {
if (!this.model) {
return null;
}
let text: string = this.model.currentStatus;
if (text === "UNREACHABLE") {
text = "[ " + text + " ] (click here to restart)";
// } else if (text === "CONNECT FAILED") {
// text = "[ " + text + " ] (retrying)";
} else if (text === "STARTING") {
text = "[ " + text + " ] (click here to reconfigure)";
} else {
text = "[ " + text + " ]";
}
return <TextItem onClick={() => this.model.handleClick()} title="Current databricks status" source={text} />;
}
}
/**
* A namespace for DbStatus statics.
*/
export namespace DbStatus {
/**
* A VDomModel for the databricks status item.
*/
export class Model extends VDomModel {
/**
* Construct a new databricks model.
*
* @param options: the options for creating the model.
*/
constructor(options: Model.IOptions) {
super();
this._notebookTracker = options.notebookTracker;
options.labShell.restored.then(
(layout) => {
// notebookTracker.currentWidget might be wrong after new start or browser refresh with more
// than 1 open tab. Hence loop through all open tabs to find the visble one
this._notebookTracker.forEach((notebookPanel: NotebookPanel) => {
if (notebookPanel.isVisible) {
console.debug("Databrickslabs-jupyterlab: select open notebook")
this.check_status(notebookPanel.sessionContext)
}
})
// When a new tab gets selected, update the kernel id
this._notebookTracker.currentChanged.connect((sender: INotebookTracker, notebookPanel: NotebookPanel) => {
console.debug("Databrickslabs-jupyterlab: selected notebook changed")
this.check_status()
})
// When a kernel gets strted, restarted or stopped, update the kernel id
options.labShell.currentChanged.connect((_, change) => {
const { oldValue, newValue } = change;
if (oldValue) {
var context = (oldValue as NotebookPanel).sessionContext
context.connectionStatusChanged.disconnect(this.onConnectionStatusChange, this)
}
if (newValue) {
var context = (newValue as NotebookPanel).sessionContext
context.connectionStatusChanged.connect(this.onConnectionStatusChange, this)
}
})
}
)
this._poll = new Poll<Private.IDbStatusRequestResult | null>({
factory: () => Private.factory(this, options.notebookTracker),
frequency: {
interval: options.refreshRate,
backoff: true
},
name: 'statusbar-extension:databrickslabs-jupyterlab#status'
});
this._poll.ticked.connect(poll => {
const { payload, phase } = poll.state;
if (phase === 'resolved') {
this._updateStatusValues(payload);
return;
}
if (phase === 'rejected') {
const oldStatusAvailable = this._statusAvailable;
this._statusAvailable = false;
this._currentStatus = "";
if (oldStatusAvailable) {
this.stateChanged.emit();
}
return;
}
});
}
cleanup() {
console.info("Databrickslabs-jupyterlab: cleanup")
this._updateStatusValues(null)
}
private onConnectionStatusChange(context: ISessionContext, change: any) {
console.debug("Databrickslabs-jupyterlab: kernel state changed: " + change)
if (change == "disconnected") {
this.cleanup()
} else if (change == "connected") {
this.check_status()
}
}
private async check_status(context: ISessionContext = null) {
if (!context) {
var context = this._notebookTracker.currentWidget.sessionContext;
// console.debug("Databrickslabs-jupyterlab: check status, set: ", context)
}
// console.debug("Databrickslabs-jupyterlab: check status: ", context)
context.ready.then(async () => {
var name = context.kernelDisplayName
var id = context.session.kernel.id
const response = await Private.request("databrickslabs-jupyterlab-status", name, id);
// console.debug("Databrickslabs-jupyterlab: check status, response: ", response)
if (response && response.ok) {
var result = await response.json();
console.debug("Databrickslabs-jupyterlab: check status, result: ", result)
if (result.status === "UNREACHABLE") {
this._updateStatusValues(result)
}
} else {
if (response) {
console.error("check_status response: unknown", response)
}
}
})
}
private _restart_dialog(status: string) {
if (!this._dialog_shown) {
this._dialog_shown = true;
var title, body, label;
if (status === "UNREACHABLE") {
title = "Cluster not reachable";
body = "Cluster cannot be reached. Check cluster or your network (e.g. VPN) and then press 'Restart' to restart the kernel or cluster";
label = "Restart";
} else {
title = "Reconfigure cluster";
body = "Reconfigure the cluster, i.e. fix ssh config, install libs, and create Spark Context";
label = "Reconfigure";
}
showDialog({
title: title,
body: body,
buttons: [
Dialog.cancelButton(),
Dialog.warnButton({ label: label })
]
}).then(result => {
if (result.button.accept) {
let context = this._notebookTracker.currentWidget.sessionContext;
this._notebookTracker.currentWidget.sessionContext.kernelDisplayName
let id = context.session.kernel.id;
let name = context.kernelDisplayName;
Private.request("/databrickslabs-jupyterlab-start", name, id)
this.counter.reset();
}
})
this._dialog_shown = false;
}
}
/**
* Given the results of the status request, update model values.
*/
private _updateStatusValues(value: Private.IDbStatusRequestResult | null): void {
const oldStatusAvailable = this._statusAvailable;
const oldCurrentStatus = this._currentStatus;
console.debug("Databrickslabs-jupyterlab: (old status avail, old current status, value)",
oldStatusAvailable, oldCurrentStatus, value
)
if (value === null) {
this._statusAvailable = false;
this._currentStatus = "";
} else {
var status = value.status;
this._statusAvailable = true;
this._currentStatus = status;
}
if (this._currentStatus === "UNREACHABLE") {
if (this._currentStatus !== oldCurrentStatus) {
this._restart_dialog(this._currentStatus)
}
}
if (
this._currentStatus !== oldCurrentStatus ||
this._statusAvailable !== oldStatusAvailable
) {
this.stateChanged.emit(void 0);
}
}
/**
* Click handler
*/
handleClick() {
this._restart_dialog(this.currentStatus)
}
get restarting_count(): number {
return this._restarting_count;
}
set restarting_count(value: number) {
this._restarting_count = value;
}
get restarting(): boolean {
return this._restarting;
}
set restarting(value: boolean) {
this._restarting = value;
}
/**
* Whether the metrics server extension is available.
*/
get statusAvailable(): boolean {
return this._statusAvailable;
}
/**
* The current status
*/
get currentStatus(): string {
return this._currentStatus;
}
/**
* Dispose of the memory usage model.
*/
dispose(): void {
super.dispose();
}
private _currentStatus: string = "";
private _statusAvailable: boolean = false;
private _poll: Poll<Private.IDbStatusRequestResult>;
private _notebookTracker: INotebookTracker;
private _restarting = false;
private _restarting_count = 0;
private _dialog_shown = false;
public counter = new Counter()
}
/**
* A namespace for Model statics.
*/
export namespace Model {
/**
* Options for creating a MemoryUsage model.
*/
export interface IOptions {
/**
* The refresh rate (in ms) for querying the server.
*/
refreshRate: number;
labShell: ILabShell;
notebookTracker: INotebookTracker;
}
}
}
namespace Private {
const SERVER_CONNECTION_SETTINGS = ServerConnection.makeSettings();
var old_name = "";
export interface IDbStatusRequestResult {
status: string;
}
export async function request(command: string, name: string, id: string) {
if (name && name.slice(0, 4) != "SSH ") {
return null
}
console.debug("Databrickslabs-jupyterlab: request ", command, name, id)
var regex = new RegExp("^SSH (.+) ([^:]+):(.*)");
var parts = regex.exec(name);
var cluster_id = parts[1];
var profile = parts[2];
var url = URLExt.join(SERVER_CONNECTION_SETTINGS.baseUrl, command);
url = url + "?profile=" + profile + "&cluster_id=" + cluster_id + "&id=" + id;
// console.debug("Databrickslabs-jupyterlab: request url: ", url)
return ServerConnection.makeRequest(url, {}, SERVER_CONNECTION_SETTINGS);
}
export async function factory(parent: DbStatus.Model, notebookTracker: INotebookTracker): Promise<IDbStatusRequestResult | null> {
let name = notebookTracker.currentWidget.sessionContext.kernelDisplayName;
var tab_switch = old_name !== name;
old_name = name;
let id = notebookTracker.currentWidget.sessionContext.session.kernel.id;
var is_starting = parent.currentStatus !== "Connected";
var do_status_check = parent.counter.next()
if (do_status_check || is_starting || tab_switch) {
const response = await request("databrickslabs-jupyterlab-status", name, id);
// console.debug("Databrickslabs-jupyterlab: factory, response: ", response)
if (response.ok) {
try {
var result = await response.json();
// console.debug("Databrickslabs-jupyterlab: factory, result: ", result)
return result
} catch (error) {
throw error;
}
}
} else {
return { "status": parent.currentStatus }
}
return null;
}
}