From 3abbb2cba872b6b237930fa7eba88decfb75c9e7 Mon Sep 17 00:00:00 2001 From: Mantvydas Deltuva Date: Tue, 10 Sep 2024 19:17:14 +0300 Subject: [PATCH] MDE/PKFE-46 implemented event handler for export feedback --- app/back-end/src/constants.py | 1 + app/back-end/src/events/workspace_event.py | 95 ------------------- .../src/events/workspace_export_event.py | 81 ++++++++++++++++ app/back-end/src/setup/eventer.py | 4 +- 4 files changed, 84 insertions(+), 97 deletions(-) delete mode 100644 app/back-end/src/events/workspace_event.py create mode 100644 app/back-end/src/events/workspace_export_event.py diff --git a/app/back-end/src/constants.py b/app/back-end/src/constants.py index f8ee6d1..20bf7f8 100644 --- a/app/back-end/src/constants.py +++ b/app/back-end/src/constants.py @@ -40,3 +40,4 @@ CONSOLE_FEEDBACK_EVENT = "console_feedback" WORKSPACE_FILE_SAVE_FEEDBACK_EVENT = "workspace_file_save_feedback" WORKSPACE_UPDATE_FEEDBACK_EVENT = "workspace_update_feedback" +WOKRSPACE_EXPORT_FEEDBACK_EVENT = "workspace_export_feedback" diff --git a/app/back-end/src/events/workspace_event.py b/app/back-end/src/events/workspace_event.py deleted file mode 100644 index d0b9d4e..0000000 --- a/app/back-end/src/events/workspace_event.py +++ /dev/null @@ -1,95 +0,0 @@ -""" -This module contains functions for handling workspace-related WebSocket events. - -It includes functions that manage file operations within the workspace directory. The module -provides real-time feedback to clients through WebSocket events. - -Dependencies: -- os: Used for file and directory operations. -- src.setup.extensions.socketio: The Socket.IO instance used for handling real-time communication. -- src.setup.constants.WORKSPACE_DIR: The base directory for workspace files. -""" - -# pylint: disable=import-error - -import os -from flask_socketio import SocketIO - -from src.utils.exceptions import UnexpectedError -from src.constants import WORKSPACE_DIR - - -def workspace_event_handler(socketio: SocketIO): - """ - Registers WebSocket event handlers for workspace operations using the provided SocketIO - instance. - - This function sets up event handling for the 'workspace_file_update' event, which allows - clients to update files in the workspace directory. It verifies the presence of necessary - data (UUID and file ID), handles file writing, and provides status updates back to the client. - - Args: - socketio (SocketIO): The SocketIO instance used for managing WebSocket connections and - events. - - Events Handled: - - 'workspace_file_update': Processes file update requests and emits status updates. - - Emits: - - 'workspace_file_update_status': Indicates success or failure of the file update operation - with a status message. - """ - - @socketio.on("workspace_file_update") - def handle_workspace_file_update(data): - """ - Handle the 'workspace_file_update' WebSocket event. - - This function processes the data received from the client to update a file in the workspace - directory. It ensures that required fields are present, writes the content to the specified - file, and handles any errors by sending appropriate status updates back to the client. - - Args: - data (dict): The data received from the client, expected to contain 'uuid', 'fileId', - and 'content'. - - Emits: - 'workspace_file_update_status': Emits a status message indicating success or failure of - the file update operation. - """ - uuid = data.get("uuid") - file_id = data.get("fileId") - content = data.get("content") - - # Ensure the uuid is provided - if not uuid: - socketio.emit( - "workspace_file_update_status", - {"status": "error", "message": "UUID is missing"}, - ) - return - - # Ensure the fileId is provided - if not file_id: - socketio.emit( - "workspace_file_update_status", - {"status": "error", "message": "File ID is missing"}, - ) - return - - file_path = os.path.join(WORKSPACE_DIR, uuid, file_id) - - try: - # Ensure the directory exists - os.makedirs(os.path.dirname(file_path), exist_ok=True) - - # Write the content to the file - with open(file_path, "w", encoding="utf-8") as file: - file.write(content) - - # Notify the client of the successful update - socketio.emit("workspace_file_update_status", {"status": "success"}) - - except UnexpectedError as e: - # Notify the client of an error during the update - socketio.emit("workspace_file_update_status", {"status": "error", "message": str(e)}) diff --git a/app/back-end/src/events/workspace_export_event.py b/app/back-end/src/events/workspace_export_event.py new file mode 100644 index 0000000..846396d --- /dev/null +++ b/app/back-end/src/events/workspace_export_event.py @@ -0,0 +1,81 @@ +""" +Module for handling Socket.IO events related to workspace file exports. + +This module sets up the event handler for the `WOKRSPACE_EXPORT_FEEDBACK_EVENT` event using +Socket.IO. It processes feedback about file export operations and sends appropriate real-time +feedback messages to the user's console based on the status of the export operation. + +Imports: + - socketio: The Socket.IO instance for handling real-time communication. + - socketio_emit_to_user_session: Utility function for emitting messages to a user's session. + - WOKRSPACE_EXPORT_FEEDBACK_EVENT: Constant defining the event name for file export feedback. + - CONSOLE_FEEDBACK_EVENT: Constant defining the event name for console feedback. + +Functions: + - workspace_export_event_handler: Registers the Socket.IO event handler for file export + feedback. +""" + +# pylint: disable=import-error + +from src.setup.extensions import socketio +from src.utils.helpers import socketio_emit_to_user_session +from src.constants import WOKRSPACE_EXPORT_FEEDBACK_EVENT, CONSOLE_FEEDBACK_EVENT + + +def workspace_export_event_handler(): + """ + Sets up the event handler for the `WOKRSPACE_EXPORT_FEEDBACK_EVENT` event in Socket.IO. + + This function registers an event handler for the `WOKRSPACE_EXPORT_FEEDBACK_EVENT` event, + which is triggered during file export operations in the workspace. The event handler processes + the feedback based on the status of the file export operation and sends appropriate feedback + messages to the user's console. + + This function does not return any value. It directly interacts with the Socket.IO event system + to provide real-time feedback to users. + + Side Effects: + - Registers the `handle_workspace_export_feedback` function as an event handler for + `WOKRSPACE_EXPORT_FEEDBACK_EVENT` using Socket.IO. + """ + + @socketio.on(WOKRSPACE_EXPORT_FEEDBACK_EVENT) + def handle_workspace_export_feedback(data): + """ + Handles the `WOKRSPACE_EXPORT_FEEDBACK_EVENT` event by providing feedback about the + file export operation. + + This function listens for Socket.IO events related to workspace file exports and processes + the feedback based on the status provided in the event data. It then sends a message to the + user's console indicating whether the file export was successful or not. + + Args: + data (dict): The event data containing feedback about the file export operation. + It should include: + - `status` (str): The status of the file export operation ("success" or "failure"). + - `uuid` (str): The unique identifier for the user's session. + - `sid` (str): The session identifier used for emitting real-time feedback. + + Emits: + - Success message to the user's console if the status is "success". + - Error message to the user's console if the status is "failure". + + Side Effects: + - Sends real-time feedback to the user's console using `socketio_emit_to_user_session`. + """ + + if data["status"] == "success": + socketio_emit_to_user_session( + CONSOLE_FEEDBACK_EVENT, + {"type": "succ", "message": "File export completed successfully."}, + data["uuid"], + data["sid"], + ) + else: + socketio_emit_to_user_session( + CONSOLE_FEEDBACK_EVENT, + {"type": "errr", "message": "File export failed."}, + data["uuid"], + data["sid"], + ) diff --git a/app/back-end/src/setup/eventer.py b/app/back-end/src/setup/eventer.py index 4584f9b..2e5dff5 100644 --- a/app/back-end/src/setup/eventer.py +++ b/app/back-end/src/setup/eventer.py @@ -35,14 +35,14 @@ from src.setup.extensions import socketio, logger, socket_manager # Import all event modules here -from src.events.workspace_event import workspace_event_handler +from src.events.workspace_export_event import workspace_export_event_handler def eventer(): """ Register all event handlers including connect and disconnect events. """ - workspace_event_handler(socketio) + workspace_export_event_handler() @socketio.on("connect") def handle_connect():