From 8a607b6dc04a0a5a6dc64eef4c05781f095002c3 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Mon, 11 Nov 2019 11:51:18 +0100 Subject: [PATCH] Multiple analyses sessions perfectly implemented. --- app/corpora/events.py | 20 +++++++++++++------- app/corpora/views.py | 5 +++-- app/templates/corpora/corpus.html.j2 | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/app/corpora/events.py b/app/corpora/events.py index 57aa0a57..e33d275e 100644 --- a/app/corpora/events.py +++ b/app/corpora/events.py @@ -7,6 +7,7 @@ from .CQiWrapper.CQiWrapper import CQiWrapper import logging +analysis_sessions = {} analysis_clients = {} @@ -14,17 +15,18 @@ analysis_clients = {} @login_required def init_corpus_analysis(corpus_id): ''' TODO: Check if current_user is allowed to subscribe to this ''' + if str(corpus_id) not in analysis_sessions: + analysis_sessions[str(corpus_id)] = [request.sid] socketio.start_background_task(observe_corpus_analysis_connection, current_app._get_current_object(), - corpus_id, - request.sid) + corpus_id, request.sid) @socketio.on('query_event') def recv_query(message): logger = logging.getLogger(__name__) logger.warning(message) - analysis_client = analysis_clients[request.sid] + # analysis_client = analysis_clients[request.sid] def observe_corpus_analysis_connection(app, corpus_id, session_id): @@ -34,10 +36,11 @@ def observe_corpus_analysis_connection(app, corpus_id, session_id): while corpus.status != 'analysing': db.session.refresh(corpus) socketio.sleep(3) - analysis_client = CQiWrapper(username='opaque', password='opaque', - host='{}_analysis_container{}'.format(corpus.creator.username, corpus.id), port=4877) - analysis_clients[session_id] = analysis_client + analysis_client = CQiWrapper(host='{}_analysis_container{}'.format(corpus.creator.username, corpus.id), + password='opaque', port=4877, + username='opaque') analysis_client.connect() + analysis_clients['session_id'] = analysis_client corpus_name = 'CORPUS' result_subcorpus_name = 'Query-results' # should be set by the user somehow query = '"and";' @@ -50,5 +53,8 @@ def observe_corpus_analysis_connection(app, corpus_id, session_id): logger.warning(analysis_client.ctrl_ping()) socketio.sleep(3) analysis_clients.pop(session_id, None) - corpus.status = 'stop analysis' + analysis_sessions[str(corpus_id)].remove(session_id) + if not analysis_sessions[str(corpus_id)]: + analysis_sessions.pop(str(corpus_id), None) + corpus.status = 'stop analysis' db.session.commit() diff --git a/app/corpora/views.py b/app/corpora/views.py index 2cc907d5..205cfe2b 100644 --- a/app/corpora/views.py +++ b/app/corpora/views.py @@ -54,14 +54,15 @@ def corpus(corpus_id): def corpus_analysis(corpus_id): logger = logging.getLogger(__name__) corpus = Corpus.query.get_or_404(corpus_id) - corpus.status = 'start analysis' + if corpus.status == 'prepared': + corpus.status = 'start analysis' + db.session.commit() query = request.args.get('query') logger.warning('Query first: {}'.format(query)) hits_per_page = request.args.get('hits_per_page', 30) context = request.args.get('context', 10) dl_form = QueryDownloadForm() form = QueryForm(hits_per_page=hits_per_page, context=context, query=query) - db.session.commit() if form.validate_on_submit(): logger = logging.getLogger(__name__) logger.warning('Data has been sent!') diff --git a/app/templates/corpora/corpus.html.j2 b/app/templates/corpora/corpus.html.j2 index adbe92c7..98e1f81e 100644 --- a/app/templates/corpora/corpus.html.j2 +++ b/app/templates/corpora/corpus.html.j2 @@ -62,9 +62,9 @@ statusElement.classList.add(CorpusList.STATUS_COLORS[status] || CorpusList.STATUS_COLORS['default']); statusElement.innerText = status; var analyseBtn = document.getElementById('analyse'); - if (status === 'prepared') { + if (status === 'prepared' || status === 'analysing') { analyseBtn.classList.remove('hide', 'disabled'); - } else if (status === 'analysing' || status === 'start analysis' || status === 'stop analysis') { + } else if (status === 'start analysis' || status === 'stop analysis') { analyseBtn.classList.remove('hide'); analyseBtn.classList.add('disabled'); }