Skip to content

Commit

Permalink
Merge pull request #60 from Strexas/MDE/PKFE-30
Browse files Browse the repository at this point in the history
MDE/PKFE-30
  • Loading branch information
mantvydasdeltuva authored Sep 11, 2024
2 parents 6d856b2 + e89d083 commit f492789
Show file tree
Hide file tree
Showing 13 changed files with 1,040 additions and 73 deletions.
3 changes: 3 additions & 0 deletions app/back-end/src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
WORKSPACE_AGGREGATE_ROUTE = "/workspace/aggregate"
WORKSPACE_IMPORT_ROUTE = "/workspace/import"
WORKSPACE_EXPORT_ROUTE = "/workspace/export"
WORKSPACE_DOWNLOAD_ROUTE = "/workspace/download"
WORKSPACE_MERGE_ROUTE = "/workspace/merge"
WORKSPACE_APPLY_ROUTE = "/workspace/apply"

# Events
CONSOLE_FEEDBACK_EVENT = "console_feedback"
Expand Down
276 changes: 276 additions & 0 deletions app/back-end/src/routes/workspace_apply_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
"""
Workspace apply route module.
This module defines routes for applying algorithms to files and saving the results to the user's
workspace.
"""

# pylint: disable=import-error

import os
import time # TODO: Remove this import once the apply logic is implemented
from flask import Blueprint, request, jsonify

from src.setup.extensions import logger
from src.utils.helpers import socketio_emit_to_user_session
from src.utils.exceptions import UnexpectedError
from src.constants import (
WORKSPACE_APPLY_ROUTE,
WORKSPACE_DIR,
CONSOLE_FEEDBACK_EVENT,
WORKSPACE_UPDATE_FEEDBACK_EVENT,
)

workspace_apply_route_bp = Blueprint("workspace_apply_route", __name__)


@workspace_apply_route_bp.route(
f"{WORKSPACE_APPLY_ROUTE}/spliceai/<path:relative_path>", methods=["GET"]
)
def get_workspace_apply_spliceai(relative_path):
"""
Route to apply the SpliceAI algorithm to a file and save the result to the workspace.
"""

# Check if 'uuid' and 'sid' are provided in the headers
if "uuid" not in request.headers or "sid" not in request.headers:
return jsonify({"error": "UUID and SID headers are required"}), 400

uuid = request.headers.get("uuid")
sid = request.headers.get("sid")

# Check if 'override' and 'applyTo' are provided
if "override" not in request.args or "applyTo" not in request.args:
return (
jsonify({"error": "'override', and 'applyTo' parameters are required"}),
400,
)

# Explanation about the parameters:
# - destination_path: string
# - The path to the destination file (where to save it) in the user's workspace
# Destination file can either be a new file or an existing file, check its existence
# - override: boolean
# - If true, the existing destination file should be overridden
# - If false, the existing destination file should not be overridden and merged
# content should be appended
# - apply_to: string
# - The path to the file to which the SpliceAI algorithm should be applied
# Destination file can be the same file so ensure correct handling

destination_path = os.path.join(WORKSPACE_DIR, uuid, relative_path)
override = request.args.get("override")
apply_to = os.path.join(WORKSPACE_DIR, uuid, request.args.get("applyTo"))

try:
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "info",
"message": f"Applying SpliceAI algorithm to '{relative_path}' with "
+ f"override: '{override}'...",
},
uuid,
sid,
)

#
# TODO: Implement SpliceAI algorithm apply and save logic using defined parameters
# [destination_path, override, apply_to]
#

# TODO: Remove this sleep statement once the apply logic is implemented
time.sleep(1) # Simulate a delay for the apply process

# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "succ",
"message": f"SpliceAI algorithm was successfully applied to '{relative_path}'.",
},
uuid,
sid,
)

socketio_emit_to_user_session(
WORKSPACE_UPDATE_FEEDBACK_EVENT,
{"status": "updated"},
uuid,
sid,
)

except FileNotFoundError as e:
logger.error(
"FileNotFoundError: %s while applying SpliceAI algorithm %s", e, destination_path
)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "errr",
"message": f"FileNotFoundError: {e} while applying SpliceAI algorithm"
+ f"{destination_path}",
},
uuid,
sid,
)
return jsonify({"error": "Requested file not found"}), 404
except PermissionError as e:
logger.error(
"PermissionError: %s while applying SpliceAI algorithm %s", e, destination_path
)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "errr",
"message": f"PermissionError: {e} while applying SpliceAI algorithm"
+ f"{destination_path}",
},
uuid,
sid,
)
return jsonify({"error": "Permission denied"}), 403
except UnexpectedError as e:
logger.error(
"UnexpectedError: %s while applying SpliceAI algorithm %s", e.message, destination_path
)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "errr",
"message": f"UnexpectedError: {e.message} while applying SpliceAI algorithm"
+ f"{destination_path}",
},
uuid,
sid,
)
return jsonify({"error": "An internal error occurred"}), 500

return jsonify({"message": "SpliceAI algorithm was successfully applied"}), 200


@workspace_apply_route_bp.route(
f"{WORKSPACE_APPLY_ROUTE}/cadd/<path:relative_path>", methods=["GET"]
)
def get_workspace_apply_cadd(relative_path):
"""
Route to apply the CADD algorithm to a file and save the result to the workspace.
"""

# Check if 'uuid' and 'sid' are provided in the headers
if "uuid" not in request.headers or "sid" not in request.headers:
return jsonify({"error": "UUID and SID headers are required"}), 400

uuid = request.headers.get("uuid")
sid = request.headers.get("sid")

# Check if 'override' and 'applyTo' are provided
if "override" not in request.args or "applyTo" not in request.args:
return (
jsonify({"error": "'override', and 'applyTo' parameters are required"}),
400,
)

# Explanation about the parameters:
# - destination_path: string
# - The path to the destination file (where to save it) in the user's workspace
# Destination file can either be a new file or an existing file, check its existence
# - override: boolean
# - If true, the existing destination file should be overridden
# - If false, the existing destination file should not be overridden and merged
# content should be appended
# - apply_to: string
# - The path to the file to which the CADD algorithm should be applied
# Destination file can be the same file so ensure correct handling

destination_path = os.path.join(WORKSPACE_DIR, uuid, relative_path)
override = request.args.get("override")
apply_to = os.path.join(WORKSPACE_DIR, uuid, request.args.get("applyTo"))

try:
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "info",
"message": f"Applying CADD algorithm to '{relative_path}' with "
+ f"override: '{override}'...",
},
uuid,
sid,
)

#
# TODO: Implement CADD algorithm apply and save logic using defined parameters
# [destination_path, override, apply_to]
#

# TODO: Remove this sleep statement once the apply logic is implemented
time.sleep(1) # Simulate a delay for the apply process

# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "succ",
"message": f"CADD algorithm was successfully applied to '{relative_path}'.",
},
uuid,
sid,
)

socketio_emit_to_user_session(
WORKSPACE_UPDATE_FEEDBACK_EVENT,
{"status": "updated"},
uuid,
sid,
)

except FileNotFoundError as e:
logger.error("FileNotFoundError: %s while applying CADD algorithm %s", e, destination_path)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "errr",
"message": f"FileNotFoundError: {e} while applying CADD algorithm "
+ f"{destination_path}",
},
uuid,
sid,
)
return jsonify({"error": "Requested file not found"}), 404
except PermissionError as e:
logger.error("PermissionError: %s while applying CADD algorithm %s", e, destination_path)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "errr",
"message": f"PermissionError: {e} while applying CADD algorithm {destination_path}",
},
uuid,
sid,
)
return jsonify({"error": "Permission denied"}), 403
except UnexpectedError as e:
logger.error(
"UnexpectedError: %s while applying CADD algorithm %s", e.message, destination_path
)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "errr",
"message": f"UnexpectedError: {e.message} while applying CADD algorithm "
+ f"{destination_path}",
},
uuid,
sid,
)
return jsonify({"error": "An internal error occurred"}), 500

return jsonify({"message": "CADD algorithm was successfully applied"}), 200
Loading

0 comments on commit f492789

Please sign in to comment.