Skip to content

Commit

Permalink
Fix parallel progress updates and improve naming
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmflynn committed May 30, 2024
1 parent ffa6e4c commit e6fbe93
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/electron/frontend/core/components/utils/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const createProgressPopup = async (options, tqdmCallback) => {
return { ...commonReturnValue, id, close };
};

const eventsURL = new URL("/neuroconv/events", baseUrl).href;
const progressEventsUrl = new URL("/neuroconv/events/progress", baseUrl).href;

class ProgressHandler {
constructor(props) {
Expand All @@ -112,4 +112,4 @@ class ProgressHandler {
removeEventListener = (...args) => this.source.removeEventListener(...args);
}

export const progressHandler = new ProgressHandler({ url: eventsURL });
export const progressHandler = new ProgressHandler({ url: progressEventsUrl });
3 changes: 2 additions & 1 deletion src/pyflask/manageNeuroconv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
inspect_multiple_filesystem_objects,
inspect_nwb_file,
inspect_nwb_folder,
listen_to_neuroconv_events,
listen_to_neuroconv_progress_events,
locate_data,
upload_folder_to_dandi,
upload_multiple_filesystem_objects_to_dandi,
upload_project_to_dandi,
validate_metadata,
progress_handler
)
2 changes: 1 addition & 1 deletion src/pyflask/manageNeuroconv/info/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .sse import announcer, format_sse
from .sse import format_sse
from .urls import (
CONVERSION_SAVE_FOLDER_PATH,
GUIDE_ROOT_FOLDER,
Expand Down
25 changes: 1 addition & 24 deletions src/pyflask/manageNeuroconv/info/sse.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
import json
import queue


def format_sse(data: str, event=None) -> str:
msg = f"data: {json.dumps(data)}\n\n"
if event is not None:
msg = f"event: {event}\n{msg}"
return msg


class MessageAnnouncer:
def __init__(self):
self.listeners = []

def listen(self):
q = queue.Queue(maxsize=5)
self.listeners.append(q)
return q

def announce(self, msg, event=None):
msg = format_sse(msg, event)
for i in reversed(range(len(self.listeners))):
try:
self.listeners[i].put_nowait(msg)
except queue.Full:
del self.listeners[i]


announcer = MessageAnnouncer()
return msg
4 changes: 2 additions & 2 deletions src/pyflask/manageNeuroconv/manage_neuroconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ def convert_to_nwb(info: dict) -> str:
options = (
{
interface: (
{"stub_test": info["stub_test"]} # , "iter_opts": {"report_hook": update_conversion_progress}}
{"stub_test": info["stub_test"]}
if available_options.get("properties").get(interface).get("properties", {}).get("stub_test")
else {}
)
Expand Down Expand Up @@ -911,7 +911,7 @@ def upload_project_to_dandi(


# Create an events endpoint
def listen_to_neuroconv_events():
def listen_to_neuroconv_progress_events():
messages = progress_handler.listen() # returns a queue.Queue
while True:
msg = messages.get() # blocks until a new message arrives
Expand Down
20 changes: 9 additions & 11 deletions src/pyflask/namespaces/neuroconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
inspect_multiple_filesystem_objects,
inspect_nwb_file,
inspect_nwb_folder,
listen_to_neuroconv_events,
listen_to_neuroconv_progress_events,
locate_data,
upload_folder_to_dandi,
upload_multiple_filesystem_objects_to_dandi,
upload_project_to_dandi,
validate_metadata,
progress_handler
)
from manageNeuroconv.info import announcer

neuroconv_namespace = Namespace("neuroconv", description="Neuroconv neuroconv_namespace for the NWB GUIDE.")

Expand Down Expand Up @@ -158,17 +158,16 @@ def post(self):
class InspectNWBFolder(Resource):
@neuroconv_namespace.doc(responses={200: "Success", 400: "Bad Request", 500: "Internal server error"})
def post(self):
url = f"{request.url_root}neuroconv/announce"
url = f"{request.url_root}neuroconv/announce/progress"
return inspect_nwb_folder(url, neuroconv_namespace.payload)


@neuroconv_namespace.route("/announce")
@neuroconv_namespace.route("/announce/progress")
class InspectNWBFolder(Resource):
@neuroconv_namespace.doc(responses={200: "Success", 400: "Bad Request", 500: "Internal server error"})
def post(self):
data = neuroconv_namespace.payload
announcer.announce(data)

progress_handler.announce(data)
return True


Expand All @@ -178,7 +177,7 @@ class InspectNWBFolder(Resource):
def post(self):
from os.path import isfile

url = f"{request.url_root}neuroconv/announce"
url = f"{request.url_root}neuroconv/announce/progress"

paths = neuroconv_namespace.payload["paths"]

Expand Down Expand Up @@ -207,9 +206,8 @@ def post(self):


# Create an events endpoint
# announcer.announce('test', 'publish')
@neuroconv_namespace.route("/events", methods=["GET"])
class Events(Resource):
@neuroconv_namespace.route("/events/progress", methods=["GET"])
class ProgressEvents(Resource):
@neuroconv_namespace.doc(responses={200: "Success", 400: "Bad Request", 500: "Internal server error"})
def get(self):
return Response(listen_to_neuroconv_events(), mimetype="text/event-stream")
return Response(listen_to_neuroconv_progress_events(), mimetype="text/event-stream")

0 comments on commit e6fbe93

Please sign in to comment.