From 5515c4de5f0398ba7d4c60d1386963d018a91aa2 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Thu, 9 Jan 2025 10:19:54 +0300 Subject: feat: Init add module for streaming api endpoins. --- gn3/api/streaming.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 gn3/api/streaming.py (limited to 'gn3/api/streaming.py') diff --git a/gn3/api/streaming.py b/gn3/api/streaming.py new file mode 100644 index 0000000..7c9254a --- /dev/null +++ b/gn3/api/streaming.py @@ -0,0 +1 @@ +""" File contains endpoint for computational streaming""" -- cgit 1.4.1 From 8bfe9616012deab8f910e89b233ea819ec22c1c3 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Thu, 9 Jan 2025 10:22:50 +0300 Subject: feat: Add streaming main endpoint. --- gn3/api/streaming.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'gn3/api/streaming.py') diff --git a/gn3/api/streaming.py b/gn3/api/streaming.py index 7c9254a..6569ceb 100644 --- a/gn3/api/streaming.py +++ b/gn3/api/streaming.py @@ -1 +1,24 @@ """ File contains endpoint for computational streaming""" +import os +from flask import current_app +from flask import jsonify +from flask import Blueprint +from flask import request + +streaming = Blueprint("streaming", __name__) + + +@streaming.route("/stream/", methods=["GET"]) +def stream(identifier="output"): + """ This endpoints streams stdout from a file expects + the indetifier to be the file """ + output_file = os.path.join(current_app.config.get("TMPDIR"), + f"{identifier}.txt") + seek_position = int(request.args.get("peak", 0)) + with open(output_file, encoding="utf-8") as file_handler: + # read to the last position default to 0 + file_handler.seek(seek_position) + results = {"data": file_handler.readlines(), + "run_id": identifier, + "pointer": file_handler.tell()} + return jsonify(results) -- cgit 1.4.1 From 4e63aa2246c2f3ca6c09c284f7f78b896fe0e33b Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Thu, 9 Jan 2025 10:28:26 +0300 Subject: feat: Add and register streaming blueprint. --- gn3/api/streaming.py | 6 +++--- gn3/app.py | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'gn3/api/streaming.py') diff --git a/gn3/api/streaming.py b/gn3/api/streaming.py index 6569ceb..213d97f 100644 --- a/gn3/api/streaming.py +++ b/gn3/api/streaming.py @@ -5,11 +5,11 @@ from flask import jsonify from flask import Blueprint from flask import request -streaming = Blueprint("streaming", __name__) +streaming = Blueprint("stream", __name__) -@streaming.route("/stream/", methods=["GET"]) -def stream(identifier="output"): +@streaming.route("/", methods=["GET"]) +def stream(identifier): """ This endpoints streams stdout from a file expects the indetifier to be the file """ output_file = os.path.join(current_app.config.get("TMPDIR"), diff --git a/gn3/app.py b/gn3/app.py index 0bec32a..3841396 100644 --- a/gn3/app.py +++ b/gn3/app.py @@ -28,6 +28,7 @@ from gn3.api.metadata import metadata from gn3.api.sampledata import sampledata from gn3.api.llm import gnqa from gn3.api.rqtl2 import rqtl2 +from gn3.api.streaming import streaming from gn3.case_attributes import caseattr @@ -109,6 +110,7 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask: app.register_blueprint(caseattr, url_prefix="/api/case-attribute") app.register_blueprint(gnqa, url_prefix="/api/llm") app.register_blueprint(rqtl2, url_prefix="/api/rqtl2") + app.register_blueprint(streaming, url_prefix="/api/stream") register_error_handlers(app) return app -- cgit 1.4.1 From f73605a67d3521e9a5cea86bdd5a947cf574f061 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Thu, 9 Jan 2025 10:43:21 +0300 Subject: refactor: enhance docstring for streaming endpoint. --- gn3/api/streaming.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'gn3/api/streaming.py') diff --git a/gn3/api/streaming.py b/gn3/api/streaming.py index 213d97f..355d903 100644 --- a/gn3/api/streaming.py +++ b/gn3/api/streaming.py @@ -10,13 +10,15 @@ streaming = Blueprint("stream", __name__) @streaming.route("/", methods=["GET"]) def stream(identifier): - """ This endpoints streams stdout from a file expects - the indetifier to be the file """ + """ This endpoint streams stdout from a file. + It expects the indetifier to be the filename + in the TMPDIR created at the main computation + endpoint see example api/rqtl.""" output_file = os.path.join(current_app.config.get("TMPDIR"), f"{identifier}.txt") seek_position = int(request.args.get("peak", 0)) with open(output_file, encoding="utf-8") as file_handler: - # read to the last position default to 0 + # read from the last read position default to 0 file_handler.seek(seek_position) results = {"data": file_handler.readlines(), "run_id": identifier, -- cgit 1.4.1 From ec0b1563cce151ef8de5f92f61342924d87d63b0 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Thu, 9 Jan 2025 12:04:54 +0300 Subject: Minor typo fix. --- gn3/api/streaming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gn3/api/streaming.py') diff --git a/gn3/api/streaming.py b/gn3/api/streaming.py index 355d903..2b6b431 100644 --- a/gn3/api/streaming.py +++ b/gn3/api/streaming.py @@ -11,7 +11,7 @@ streaming = Blueprint("stream", __name__) @streaming.route("/", methods=["GET"]) def stream(identifier): """ This endpoint streams stdout from a file. - It expects the indetifier to be the filename + It expects the identifier to be the filename in the TMPDIR created at the main computation endpoint see example api/rqtl.""" output_file = os.path.join(current_app.config.get("TMPDIR"), -- cgit 1.4.1