From 2e2d349f3aa32a28b6c36de5ac93558db31808f0 Mon Sep 17 00:00:00 2001 From: VisualDust Date: Tue, 12 Dec 2023 01:25:22 +0800 Subject: [PATCH] added "id" in event message (representing row id in database --- neetbox/_daemon/_protocol.py | 3 +++ neetbox/_daemon/server/_flask_server.py | 6 +++--- neetbox/_daemon/server/_websocket_server.py | 19 ++++++++++--------- neetbox/cli/parse.py | 1 - pyproject.toml | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/neetbox/_daemon/_protocol.py b/neetbox/_daemon/_protocol.py index a02ac7a6..e18ba4ef 100644 --- a/neetbox/_daemon/_protocol.py +++ b/neetbox/_daemon/_protocol.py @@ -53,6 +53,7 @@ class EventMsg: who: str = None timestamp: str = get_timestamp() history_len: int = -1 + id: int = None # id in database def json(self): return { @@ -65,6 +66,7 @@ def json(self): PAYLOAD_KEY: self.payload, TIMESTAMP_KEY: self.timestamp, HISTORY_LEN_KEY: self.history_len, + ID_KEY: self.id, } def dumps(self): @@ -84,6 +86,7 @@ def loads(cls, src): event_id=src.get(EVENT_ID_KEY, -1), timestamp=src.get(TIMESTAMP_KEY, get_timestamp()), history_len=src.get(HISTORY_LEN_KEY, -1), + id=src.get(ID_KEY, None), ) @classmethod diff --git a/neetbox/_daemon/server/_flask_server.py b/neetbox/_daemon/server/_flask_server.py index 92630663..ee56892e 100644 --- a/neetbox/_daemon/server/_flask_server.py +++ b/neetbox/_daemon/server/_flask_server.py @@ -152,7 +152,7 @@ def upload_image(project_id): return abort(404) message = EventMsg.loads(request.form["json"]) image_bytes = request.files["image"].read() - lastrowid = Bridge.of_id(project_id).save_blob_to_history( + message.id = Bridge.of_id(project_id).save_blob_to_history( table_name="image", run_id=message.run_id, series=message.series, @@ -162,9 +162,8 @@ def upload_image(project_id): num_row_limit=message.history_len, ) message.payload = message.payload or {} - message.payload[ID_KEY] = lastrowid Bridge.of_id(project_id).ws_send_to_frontends(message) - return {"result": "ok", "id": lastrowid} + return {"result": "ok", "id": message.id} @app.route(f"{FRONTEND_API_ROOT}/project//image/", methods=["GET"]) def get_image_of(project_id, image_id: int): @@ -199,6 +198,7 @@ def get_history_image_metadata_of(project_id): @app.route(f"{FRONTEND_API_ROOT}/project//scalar", methods=["GET"]) def get_history_scalar_of(project_id): + time.sleep(2) return get_history_json_of( project_id=project_id, table_name="scalar", condition=request.args.get("condition") ) diff --git a/neetbox/_daemon/server/_websocket_server.py b/neetbox/_daemon/server/_websocket_server.py index 5dfcb345..6e931202 100644 --- a/neetbox/_daemon/server/_websocket_server.py +++ b/neetbox/_daemon/server/_websocket_server.py @@ -144,6 +144,15 @@ def on_event_type_json( logger.warn(f"handle {message.event_type}. {message.project_id} not found.") return bridge = Bridge.of_id(message.project_id) + if save_history: + message.id = bridge.save_json_to_history( + table_name=message.event_type, + json_data=message.payload, + series=message.series, + run_id=message.run_id, + timestamp=message.timestamp, + num_row_limit=message.history_len, + ) if forward_to: if forward_to == IdentityType.SELF: forward_to = message.who @@ -155,15 +164,7 @@ def on_event_type_json( bridge.ws_send_to_frontends(message) # forward to frontends elif forward_to in [IdentityType.CLI, IdentityType.BOTH]: bridge.ws_send_to_client(message) # forward to frontends - if save_history: - bridge.save_json_to_history( - table_name=message.event_type, - json_data=message.payload, - series=message.series, - run_id=message.run_id, - timestamp=message.timestamp, - num_row_limit=message.history_len, - ) + return # return after handling log forwarding def on_event_type_status(message: EventMsg): diff --git a/neetbox/cli/parse.py b/neetbox/cli/parse.py index 35c1d458..a43f918f 100644 --- a/neetbox/cli/parse.py +++ b/neetbox/cli/parse.py @@ -18,7 +18,6 @@ from neetbox.utils.massive import check_read_toml from ._client_web_apis import * -from .app._app import NeetBoxApp console = Console() diff --git a/pyproject.toml b/pyproject.toml index 3b1be14a..f49897f0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "neetbox" -version = "0.3.0" +version = "0.3.1" description = "Logging/Debugging/Tracing/Managing/Facilitating long running python projects, especially a replacement of tensorboard for deep learning projects" license = "MIT" authors = ["VisualDust ", "Lideming "]