Skip to content

Commit

Permalink
adding awareness event when open and close websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Jialin Zhang committed Mar 12, 2024
1 parent fab7f19 commit dfa1e7d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion jupyter_collaboration/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .handlers import DocSessionHandler, YDocWebSocketHandler
from .loaders import FileLoaderMapping
from .stores import SQLiteYStore
from .utils import EVENTS_SCHEMA_PATH
from .utils import AWARENESS_EVENTS_SCHEMA_PATH, EVENTS_SCHEMA_PATH
from .websocketserver import JupyterWebsocketServer


Expand Down Expand Up @@ -60,6 +60,7 @@ class YDocExtension(ExtensionApp):
def initialize(self):
super().initialize()
self.serverapp.event_logger.register_event_schema(EVENTS_SCHEMA_PATH)
self.serverapp.event_logger.register_event_schema(AWARENESS_EVENTS_SCHEMA_PATH)

def initialize_settings(self):
self.settings.update(
Expand Down
43 changes: 43 additions & 0 deletions jupyter_collaboration/events/awareness.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"$id": https://schema.jupyter.org/jupyter_collaboration/awareness/v1
"$schema": "http://json-schema.org/draft-07/schema"
version: 1
title: Collaborative awareness events
personal-data: true
description: |
Awareness events emitted from server-side during a collaborative session.
type: object
required:
- level
- room
- user
- action
properties:
level:
enum:
- INFO
- DEBUG
- WARNING
- ERROR
- CRITICAL
description: |
Message type.
room:
type: string
description: |
Room ID. Usually composed by the file type, format and ID.
user:
type: string
description: |
The name of the user who joined or left room.
action:
enum:
- join
- leave
description: |
Possible values:
1. join
2. leave
msg:
type: string
description: |
Optional event message.
11 changes: 11 additions & 0 deletions jupyter_collaboration/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .loaders import FileLoaderMapping
from .rooms import DocumentRoom, TransientRoom
from .utils import (
JUPYTER_COLLABORATION_AWARENESS_EVENTS_URI,
JUPYTER_COLLABORATION_EVENTS_URI,
LogLevel,
MessageType,
Expand Down Expand Up @@ -184,6 +185,7 @@ async def open(self, room_id):
try:
# Initialize the room
await self.room.initialize()
self._emit_awareness_event(LogLevel.INFO, self.current_user.name, "join")
except Exception as e:
_, _, file_id = decode_file_path(self._room_id)
file = self._file_loaders[file_id]
Expand Down Expand Up @@ -284,6 +286,8 @@ def on_close(self) -> None:
# keep the document for a while in case someone reconnects
self.log.info("Cleaning room: %s", self._room_id)
self.room.cleaner = asyncio.create_task(self._clean_room())
if isinstance(self.room, DocumentRoom):
self._emit_awareness_event(LogLevel.INFO, self.current_user.name, "leave")

def _emit(self, level: LogLevel, action: str | None = None, msg: str | None = None) -> None:
_, _, file_id = decode_file_path(self._room_id)
Expand All @@ -296,6 +300,13 @@ def _emit(self, level: LogLevel, action: str | None = None, msg: str | None = No
data["msg"] = msg

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

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

self.event_logger.emit(schema_id=JUPYTER_COLLABORATION_AWARENESS_EVENTS_URI, data=data)

async def _clean_room(self) -> None:
"""
Expand Down
2 changes: 2 additions & 0 deletions jupyter_collaboration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

JUPYTER_COLLABORATION_EVENTS_URI = "https://schema.jupyter.org/jupyter_collaboration/session/v1"
EVENTS_SCHEMA_PATH = pathlib.Path(__file__).parent / "events" / "session.yaml"
JUPYTER_COLLABORATION_AWARENESS_EVENTS_URI = "https://schema.jupyter.org/jupyter_collaboration/awareness/v1"
AWARENESS_EVENTS_SCHEMA_PATH = pathlib.Path(__file__).parent / "events" / "awareness.yaml"


class MessageType(IntEnum):
Expand Down

0 comments on commit dfa1e7d

Please sign in to comment.