Skip to content

Commit

Permalink
Review: use lab console log
Browse files Browse the repository at this point in the history
  • Loading branch information
hbcarlos committed Apr 25, 2023
1 parent ead7182 commit 586999c
Show file tree
Hide file tree
Showing 5 changed files with 730 additions and 430 deletions.
2 changes: 1 addition & 1 deletion jupyter_collaboration/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def _emit(self, level: LogLevel, action: str = None, msg: str = None) -> None:
_, _, file_id = decode_file_path(self._room_id)
path = self._file_id_manager.get_path(file_id)

data = {"level": level, "room": self._room_id, "path": path}
data = {"level": level.value, "room": self._room_id, "path": path}
if action:
data["action"] = action
if msg:
Expand Down
4 changes: 2 additions & 2 deletions jupyter_collaboration/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ async def initialize(self) -> None:
self._emit(LogLevel.INFO, "initialize", "Room initialized")

def _emit(self, level: LogLevel, action: str = None, msg: str = None) -> None:
data = {"level": level, "room": self._room_id, "path": self._file.path}
data = {"level": level.value, "room": self._room_id, "path": self._file.path}
if action:
data["action"] = action
if msg:
data["msg"] = msg

self.event_logger.emit(schema_id=JUPYTER_COLLABORATION_EVENTS_URI, data=data)
self._logger.emit(schema_id=JUPYTER_COLLABORATION_EVENTS_URI, data=data)

def _clean(self) -> None:
"""
Expand Down
3 changes: 3 additions & 0 deletions packages/collaboration-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
"@jupyterlab/codemirror": "^4.0.0-beta.0",
"@jupyterlab/coreutils": "^6.0.0-beta.0",
"@jupyterlab/filebrowser": "^4.0.0-beta.0",
"@jupyterlab/fileeditor": "^4.0.0-beta.0",
"@jupyterlab/logconsole": "^4.0.0-beta.0",
"@jupyterlab/notebook": "^4.0.0-beta.0",
"@jupyterlab/services": "^7.0.0-beta.0",
"@jupyterlab/settingregistry": "^4.0.0-beta.0",
"@jupyterlab/statedb": "^4.0.0-beta.0",
Expand Down
113 changes: 88 additions & 25 deletions packages/collaboration-extension/src/filebrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import {
IFileBrowserFactory
} from '@jupyterlab/filebrowser';
import { showDialog, Dialog } from '@jupyterlab/apputils';
import { ITranslator } from '@jupyterlab/translation';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { ILogger, ILoggerRegistry } from '@jupyterlab/logconsole';
import { INotebookTracker } from '@jupyterlab/notebook';
import { IEditorTracker } from '@jupyterlab/fileeditor';

import { CommandRegistry } from '@lumino/commands';

Expand Down Expand Up @@ -168,34 +171,94 @@ export const logger: JupyterFrontEndPlugin<void> = {
id: '@jupyter/collaboration-extension:logger',
description: 'A logging plugin for debugging purposes.',
autoStart: true,
requires: [ITranslator],
optional: [],
activate: (app: JupyterFrontEnd, translator: ITranslator): void => {
const trans = translator.load('jupyter_collaboration');
optional: [ILoggerRegistry, INotebookTracker, IEditorTracker, ITranslator],
activate: (
app: JupyterFrontEnd,
loggerRegistry: ILoggerRegistry | null,
nbtracker: INotebookTracker | null,
editorTracker: IEditorTracker | null,
translator: ITranslator | null
): void => {
const trans = (translator ?? nullTranslator).load('jupyter_collaboration');
const schemaID =
'https://events.jupyter.org/jupyter_server/jupyter_collaboration/v1';

if (!loggerRegistry) {
app.serviceManager.events.stream.connect((_, emission) => {
if (emission.schema_id === schemaID) {
console.debug(
`[${emission.room}(${emission.path})] ${emission.action ?? ''}: ${
emission.msg ?? ''
}`
);

if (emission.level === 'WARNING') {
showDialog({
title: trans.__('Warning'),
body: trans.__(
`Two collaborative sessions are accessing the file ${emission.path} simultaneously.
\nOpening the same file using different views simultaneously is not supported. Please, close one view; otherwise, you might lose some of your progress.`
),
buttons: [Dialog.okButton()]
});
}
}
});

return;
}

app.serviceManager.events.stream.connect((_, emission) => {
if (
emission.schema_id ===
'https://events.jupyter.org/jupyter_server/jupyter_collaboration/v1'
) {
console.debug(
`[${emission.room}(${emission.path})] ${emission.action ?? ''}: ${
emission.msg ?? ''
}`
);

if (emission.level === 'warn') {
showDialog({
title: trans.__('Warning'),
body: trans.__(
`Two collaborative sessions are accessing the file ${emission.path} simultaneously.
\nOpening the same file using different views simultaneously is not supported. Please, close one view; otherwise, you might lose some of your progress.`
),
buttons: [Dialog.okButton()]
const loggers: Map<string, ILogger> = new Map();

if (nbtracker) {
nbtracker.widgetAdded.connect((sender, nb) => {
const logger = loggerRegistry.getLogger(nb.context.path);
loggers.set(nb.context.localPath, logger);

nb.disposed.connect(nb => {
loggers.delete(nb.context.localPath);
});
});
}

if (editorTracker) {
editorTracker.widgetAdded.connect((sender, editor) => {
const logger = loggerRegistry.getLogger(editor.context.path);
loggers.set(editor.context.localPath, logger);

editor.disposed.connect(editor => {
loggers.delete(editor.context.localPath);
});
});
}

void (async () => {
const { events } = app.serviceManager;
for await (const emission of events.stream) {
if (emission.schema_id === schemaID) {
const logger = loggers.get(emission.path as string);

logger?.log({
type: 'text',
level: (emission.level as string).toLowerCase() as any,
data: `[${emission.room}] ${emission.action ?? ''}: ${
emission.msg ?? ''
}`
});

if (emission.level === 'WARNING') {
showDialog({
title: trans.__('Warning'),
body: trans.__(
`Two collaborative sessions are accessing the file ${emission.path} simultaneously.
\nOpening the same file using different views simultaneously is not supported. Please, close one view; otherwise, you might lose some of your progress.`
),
buttons: [Dialog.okButton()]
});
}
}
}
});
})();
}
};

Expand Down
Loading

0 comments on commit 586999c

Please sign in to comment.