Skip to content

Commit f7b5b86

Browse files
authored
Use serverSettings for API requests (#256)
1 parent d18b7ed commit f7b5b86

5 files changed

Lines changed: 58 additions & 41 deletions

File tree

packages/labextension/src/handler.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import { ServerConnection } from '@jupyterlab/services';
66
*
77
* @param endPoint API REST end point for the extension
88
* @param init Initial values for the request
9+
* @param serverSettings Optional server connection settings
910
* @returns The response body interpreted as JSON
1011
*/
1112
export async function requestAPI<T>(
1213
endPoint = '',
13-
init: RequestInit = {}
14+
init: RequestInit = {},
15+
serverSettings?: ServerConnection.ISettings
1416
): Promise<T> {
15-
const settings = ServerConnection.makeSettings();
17+
const settings = serverSettings ?? ServerConnection.makeSettings();
1618
const requestUrl = URLExt.join(
1719
settings.baseUrl,
1820
'api/metrics/v1/kernel_usage', // API Namespace

packages/labextension/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ const resourceStatusPlugin: JupyterFrontEndPlugin<void> = {
7979
info: JupyterLab.IInfo | null
8080
) => {
8181
const refreshRate = DEFAULT_REFRESH_RATE;
82+
const { serverSettings } = app.serviceManager;
8283

8384
const trans = translator.load('jupyter-resource-usage');
8485
const item = new ResourceUsageStatus(trans, {
8586
refreshRate,
87+
serverSettings,
8688
refreshStandby: () => {
8789
if (info) {
8890
return !info.isConnected || 'when-hidden';
@@ -117,6 +119,7 @@ const systemMonitorPlugin: JupyterFrontEndPlugin<void> = {
117119
settingRegistry: ISettingRegistry | null,
118120
info: JupyterLab.IInfo | null
119121
) => {
122+
const { serverSettings } = app.serviceManager;
120123
let enablePlugin = DEFAULT_ENABLE_SYSTEM_MONITOR;
121124
let refreshRate = DEFAULT_REFRESH_RATE;
122125
let cpuLabel = DEFAULT_CPU_LABEL;
@@ -141,6 +144,7 @@ const systemMonitorPlugin: JupyterFrontEndPlugin<void> = {
141144

142145
const model = new ResourceUsage.Model({
143146
refreshRate,
147+
serverSettings,
144148
refreshStandby: () => {
145149
if (info) {
146150
return !info.isConnected || 'when-hidden';
@@ -188,6 +192,7 @@ const kernelUsagePlugin: JupyterFrontEndPlugin<void> = {
188192
const trans = translator.load('jupyter-resource-usage');
189193

190194
const { commands, shell } = app;
195+
const { serverSettings } = app.serviceManager;
191196
const category = trans.__('Kernel Resource');
192197

193198
let panel: KernelUsagePanel | null = null;
@@ -203,6 +208,7 @@ const kernelUsagePlugin: JupyterFrontEndPlugin<void> = {
203208
panel = new KernelUsagePanel({
204209
tracker,
205210
trans,
211+
serverSettings,
206212
});
207213
shell.add(panel, 'right', { rank: 200 });
208214
}

packages/labextension/src/model.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ export namespace ResourceUsage {
5555
*/
5656
constructor(options: Model.IOptions) {
5757
super();
58+
this._serverSettings =
59+
options.serverSettings ?? ServerConnection.makeSettings();
5860
for (let i = 0; i < N_BUFFER; i++) {
5961
this._values.push({ memoryPercent: 0, cpuPercent: 0, diskPercent: 0 });
6062
}
6163
this._poll = new Poll<Private.IMetricRequestResult | null>({
6264
factory: (): Promise<Private.IMetricRequestResult | null> =>
63-
Private.factory(),
65+
Private.factory(this._serverSettings),
6466
frequency: {
6567
interval: options.refreshRate,
6668
backoff: true,
@@ -310,6 +312,7 @@ export namespace ResourceUsage {
310312
private _memoryLimit: number | null = null;
311313
private _cpuLimit: number | null = null;
312314
private _poll: Poll<Private.IMetricRequestResult | null>;
315+
private _serverSettings: ServerConnection.ISettings;
313316
private _memUnits: MemoryUnit = 'B';
314317
private _diskUnits: MemoryUnit = 'B';
315318
private _warn = new ResourceUsageWarning();
@@ -329,6 +332,11 @@ export namespace ResourceUsage {
329332
*/
330333
refreshRate: number;
331334

335+
/**
336+
* The server connection settings to use for API requests.
337+
*/
338+
serverSettings?: ServerConnection.ISettings;
339+
332340
/**
333341
* When the model stops polling the API. Defaults to `when-hidden`.
334342
*/
@@ -361,19 +369,6 @@ export namespace ResourceUsage {
361369
* A namespace for module private statics.
362370
*/
363371
namespace Private {
364-
/**
365-
* Settings for making requests to the server.
366-
*/
367-
const SERVER_CONNECTION_SETTINGS = ServerConnection.makeSettings();
368-
369-
/**
370-
* The url endpoint for making requests to the server.
371-
*/
372-
const METRIC_URL = URLExt.join(
373-
SERVER_CONNECTION_SETTINGS.baseUrl,
374-
'api/metrics/v1'
375-
);
376-
377372
/**
378373
* The shape of a response from the metrics server extension.
379374
*/
@@ -404,11 +399,13 @@ namespace Private {
404399
/**
405400
* Make a request to the backend.
406401
*/
407-
export const factory = async (): Promise<IMetricRequestResult | null> => {
402+
export const factory = async (
403+
serverSettings: ServerConnection.ISettings
404+
): Promise<IMetricRequestResult | null> => {
408405
const request = ServerConnection.makeRequest(
409-
METRIC_URL,
406+
URLExt.join(serverSettings.baseUrl, 'api/metrics/v1'),
410407
{},
411-
SERVER_CONNECTION_SETTINGS
408+
serverSettings
412409
);
413410
const response = await request;
414411

packages/labextension/src/panel.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Message } from '@lumino/messaging';
2+
import type { ServerConnection } from '@jupyterlab/services';
23
import { TranslationBundle } from '@jupyterlab/translation';
34
import { StackedPanel } from '@lumino/widgets';
45
import { LabIcon } from '@jupyterlab/ui-components';
@@ -13,6 +14,7 @@ export class KernelUsagePanel extends StackedPanel {
1314
constructor(props: {
1415
tracker: KernelWidgetTracker;
1516
trans: TranslationBundle;
17+
serverSettings?: ServerConnection.ISettings;
1618
}) {
1719
super();
1820
this.addClass(PANEL_CLASS);
@@ -27,6 +29,7 @@ export class KernelUsagePanel extends StackedPanel {
2729
tracker: props.tracker,
2830
panel: this,
2931
trans: props.trans,
32+
serverSettings: props.serverSettings,
3033
});
3134
this.addWidget(widget);
3235
}

packages/labextension/src/widget.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useRef, useState, useEffect } from 'react';
22
import { ReactWidget, ISessionContext } from '@jupyterlab/apputils';
33
import { IChangedArgs } from '@jupyterlab/coreutils';
44
import { Kernel } from '@jupyterlab/services';
5+
import type { ServerConnection } from '@jupyterlab/services';
56
import { TranslationBundle } from '@jupyterlab/translation';
67
import { requestAPI } from './handler';
78
import { KernelUsagePanel } from './panel';
@@ -126,8 +127,9 @@ const KernelUsage = (props: {
126127
tracker: KernelWidgetTracker;
127128
panel: KernelUsagePanel;
128129
trans: TranslationBundle;
130+
serverSettings?: ServerConnection.ISettings;
129131
}) => {
130-
const { panel } = props;
132+
const { panel, serverSettings } = props;
131133
const [kernelId, setKernelId] = useState<string>();
132134
const [path, setPath] = useState<string>();
133135
const [usage, setUsage] = useState<Usage | undefined>();
@@ -147,30 +149,32 @@ const KernelUsage = (props: {
147149
kernelIdRef.current = kernelId;
148150

149151
const requestUsage = (kid: string) => {
150-
return requestAPI<any>(`get_usage/${kid}`).then((data) => {
151-
// The kernel reply may arrive late due to lax timeouts, so we need to
152-
// check if it is for the current kernel
152+
return requestAPI<any>(`get_usage/${kid}`, {}, serverSettings).then(
153+
(data) => {
154+
// The kernel reply may arrive late due to lax timeouts, so we need to
155+
// check if it is for the current kernel
153156

154-
if (kid !== kernelIdRef.current) {
155-
// Ignore outdated response, but preserve current reason
156-
return;
157-
}
157+
if (kid !== kernelIdRef.current) {
158+
// Ignore outdated response, but preserve current reason
159+
return;
160+
}
158161

159-
if (data.content?.reason) {
160-
const reason = data.content;
161-
setReason(reason);
162-
return;
163-
} else {
164-
setReason(undefined);
165-
}
162+
if (data.content?.reason) {
163+
const reason = data.content;
164+
setReason(reason);
165+
return;
166+
} else {
167+
setReason(undefined);
168+
}
166169

167-
const usage: Usage = {
168-
...data.content,
169-
timestamp: new Date(),
170-
kernel_id: kid,
171-
};
172-
setUsage(usage);
173-
});
170+
const usage: Usage = {
171+
...data.content,
172+
timestamp: new Date(),
173+
kernel_id: kid,
174+
};
175+
setUsage(usage);
176+
}
177+
);
174178
};
175179

176180
useEffect(() => {
@@ -378,11 +382,13 @@ export class KernelUsageWidget extends ReactWidget {
378382
tracker: KernelWidgetTracker;
379383
panel: KernelUsagePanel;
380384
trans: TranslationBundle;
385+
serverSettings?: ServerConnection.ISettings;
381386
}) {
382387
super();
383388
this._tracker = props.tracker;
384389
this._panel = props.panel;
385390
this._trans = props.trans;
391+
this._serverSettings = props.serverSettings;
386392
this.addClass(KERNEL_USAGE_CLASS);
387393
}
388394

@@ -392,7 +398,10 @@ export class KernelUsageWidget extends ReactWidget {
392398
tracker={this._tracker}
393399
panel={this._panel}
394400
trans={this._trans}
401+
serverSettings={this._serverSettings}
395402
/>
396403
);
397404
}
405+
406+
private _serverSettings: ServerConnection.ISettings | undefined;
398407
}

0 commit comments

Comments
 (0)