Skip to content

Commit b857015

Browse files
3coinsdlqqq
andcommitted
Telemetry patch (#457)
* Moved event logger handler up the stack. * Fixed lint errors * Combined handler, and plugin. * Fixed lint errors * separate launcher command and rename show-notebook-jobs (#3) --------- Co-authored-by: david qiu <[email protected]>
1 parent 22a9edd commit b857015

File tree

3 files changed

+47
-33
lines changed

3 files changed

+47
-33
lines changed

src/index.tsx

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ export namespace CommandIDs {
3232
export const deleteJob = 'scheduling:delete-job';
3333
export const createJobFileBrowser = 'scheduling:create-from-filebrowser';
3434
export const createJobCurrentNotebook = 'scheduling:create-from-notebook';
35-
export const showNotebookJobs = 'scheduling:show-notebook-jobs';
35+
export const restoreLayout = 'scheduling:restore-layout';
3636
export const stopJob = 'scheduling:stop-job';
3737
export const downloadFiles = 'scheduling:download-files';
38+
export const listJobsFromLauncher = 'scheduling:list-jobs-from-launcher';
3839
}
3940

4041
export const NotebookJobsPanelId = 'notebook-jobs-panel';
@@ -68,19 +69,14 @@ const advancedOptions: JupyterFrontEndPlugin<Scheduler.IAdvancedOptions> = {
6869
}
6970
};
7071

71-
// Default Telemetry Handler
72-
const telemetryHandler = async (
73-
eventLog: Scheduler.IEventLog
74-
): Promise<void> => {
75-
console.log(JSON.stringify(eventLog, undefined, 4));
76-
};
77-
7872
const telemetry: JupyterFrontEndPlugin<Scheduler.TelemetryHandler> = {
7973
id: '@jupyterlab/scheduler:TelemetryHandler',
8074
autoStart: true,
8175
provides: Scheduler.TelemetryHandler,
8276
activate: (app: JupyterFrontEnd) => {
83-
return telemetryHandler;
77+
return async (e: Scheduler.IEventLog) => {
78+
/*noop*/
79+
};
8480
}
8581
};
8682

@@ -174,21 +170,34 @@ async function activatePlugin(
174170
namespace: 'jupyterlab-scheduler'
175171
});
176172
restorer.restore(widgetTracker, {
177-
command: CommandIDs.showNotebookJobs,
173+
command: CommandIDs.restoreLayout,
178174
args: widget => widget.content.model.toJson(),
179175
name: () => 'jupyterlab-scheduler'
180176
});
181177

182178
let mainAreaWidget: MainAreaWidget<NotebookJobsPanel> | undefined;
183179
let jobsPanel: NotebookJobsPanel | undefined;
184180

181+
const eventLogger: Scheduler.EventLogger = eventName => {
182+
if (!eventName) {
183+
return;
184+
}
185+
const eventLog = {
186+
body: {
187+
name: `org.jupyter.jupyter-scheduler.${eventName}`
188+
},
189+
timestamp: new Date()
190+
};
191+
telemetryHandler(eventLog).then();
192+
};
193+
185194
const showJobsPanel = async (data: IJobsModel) => {
186195
if (!mainAreaWidget || mainAreaWidget.isDisposed) {
187196
// Create new jobs panel widget
188197
jobsPanel = new NotebookJobsPanel({
189198
app,
190199
translator,
191-
telemetryHandler,
200+
eventLogger,
192201
advancedOptions: advancedOptions
193202
});
194203
// Create new main area widget
@@ -222,14 +231,15 @@ async function activatePlugin(
222231

223232
// Commands
224233

225-
commands.addCommand(CommandIDs.showNotebookJobs, {
226-
execute: async args => showJobsPanel(args as IJobsModel),
227-
label: trans.__('Notebook Jobs'),
228-
icon: eventNoteIcon
234+
commands.addCommand(CommandIDs.restoreLayout, {
235+
execute: async args => {
236+
showJobsPanel(args as IJobsModel);
237+
}
229238
});
230239

231240
commands.addCommand(CommandIDs.createJobFileBrowser, {
232241
execute: async () => {
242+
eventLogger('file-browser.create-job');
233243
const widget = fileBrowserTracker.currentWidget;
234244
const filePath = getSelectedFilePath(widget) ?? '';
235245

@@ -252,6 +262,7 @@ async function activatePlugin(
252262

253263
commands.addCommand(CommandIDs.createJobCurrentNotebook, {
254264
execute: async () => {
265+
eventLogger('notebook-header.create-job');
255266
// Get the current notebook's name and path
256267
const contentsModel =
257268
notebookTracker.currentWidget?.context?.contentsModel;
@@ -301,8 +312,22 @@ async function activatePlugin(
301312

302313
// Add to launcher
303314
if (launcher) {
315+
commands.addCommand(CommandIDs.listJobsFromLauncher, {
316+
execute: async () => {
317+
eventLogger('launcher.show-jobs');
318+
showJobsPanel({
319+
jobsView: JobsView.ListJobs
320+
});
321+
},
322+
label: trans.__('Notebook Jobs'),
323+
icon: eventNoteIcon
324+
});
325+
304326
launcher.add({
305-
command: CommandIDs.showNotebookJobs
327+
command: CommandIDs.listJobsFromLauncher,
328+
args: {
329+
launcher: true
330+
}
306331
});
307332
}
308333
}

src/notebook-jobs-panel.tsx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class NotebookJobsPanel extends VDomRenderer<JobsModel> {
4141
readonly _translator: ITranslator;
4242
readonly _trans: TranslationBundle;
4343
readonly _advancedOptions: React.FunctionComponent<Scheduler.IAdvancedOptionsProps>;
44-
readonly _telemetryHandler: Scheduler.TelemetryHandler;
44+
readonly _eventLogger: Scheduler.EventLogger;
4545
private _newlyCreatedId: string | undefined;
4646
private _newlyCreatedName: string | undefined;
4747
private _last_input_drop_target: Element | null;
@@ -68,7 +68,7 @@ export class NotebookJobsPanel extends VDomRenderer<JobsModel> {
6868
this._translator = options.translator;
6969
this._trans = this._translator.load('jupyterlab');
7070
this._advancedOptions = options.advancedOptions;
71-
this._telemetryHandler = options.telemetryHandler;
71+
this._eventLogger = options.eventLogger;
7272
this._last_input_drop_target = null;
7373

7474
this.node.setAttribute('role', 'region');
@@ -124,19 +124,6 @@ export class NotebookJobsPanel extends VDomRenderer<JobsModel> {
124124
}
125125
};
126126

127-
logEvent(eventName: string): void {
128-
if (!eventName) {
129-
return;
130-
}
131-
const eventLog = {
132-
body: {
133-
name: `org.jupyter.jupyter-scheduler.${eventName}`
134-
},
135-
timestamp: new Date()
136-
};
137-
this._telemetryHandler(eventLog).then();
138-
}
139-
140127
/**
141128
* Handle the DOM events for the directory listing.
142129
*
@@ -235,7 +222,7 @@ export class NotebookJobsPanel extends VDomRenderer<JobsModel> {
235222
return (
236223
<ThemeProvider theme={getJupyterLabTheme()}>
237224
<TranslatorContext.Provider value={this._translator}>
238-
<LogContext.Provider value={this.logEvent.bind(this)}>
225+
<LogContext.Provider value={this._eventLogger.bind(this)}>
239226
<ErrorBoundary
240227
alertTitle={this._trans.__('Internal error')}
241228
alertMessage={this._trans.__(
@@ -325,7 +312,7 @@ namespace NotebookJobsPanel {
325312
app: JupyterFrontEnd;
326313
translator: ITranslator;
327314
advancedOptions: Scheduler.IAdvancedOptions;
328-
telemetryHandler: Scheduler.TelemetryHandler;
315+
eventLogger: Scheduler.EventLogger;
329316
model?: JobsModel;
330317
}
331318
}

src/tokens.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,6 @@ export namespace Scheduler {
5959
export const TelemetryHandler = new Token<TelemetryHandler>(
6060
'@jupyterlab/scheduler:ITelemetryHandler'
6161
);
62+
63+
export type EventLogger = (eventName: string) => void;
6264
}

0 commit comments

Comments
 (0)