nopaque/app/corpora/events.py

55 lines
2.1 KiB
Python

from app import db, socketio
from app.events import connected_sessions
from app.models import Corpus
from flask import current_app, request
from flask_login import login_required
from .CQiWrapper.CQiWrapper import CQiWrapper
import logging
analysis_clients = {}
@socketio.on('init_corpus_analysis')
@login_required
def init_corpus_analysis(corpus_id):
''' TODO: Check if current_user is allowed to subscribe to this '''
socketio.start_background_task(observe_corpus_analysis_connection,
current_app._get_current_object(),
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]
def observe_corpus_analysis_connection(app, corpus_id, session_id):
logger = logging.getLogger(__name__)
with app.app_context():
corpus = Corpus.query.filter_by(id=corpus_id).first()
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.connect()
logger.warning(analysis_client.corpus_list_corpora())
corpus_name = 'CORPUS'
result_subcorpus_name = 'Query-results' # should be set by the user somehow
query = '"and";'
analysis_client.create_attribute_strings(corpus_name)
logger.warning('Log after create_attribute_strings.')
analysis_client.query_subcorpus(corpus_name, result_subcorpus_name, query)
logger.warning('Log after query_subcorpus.')
while session_id in connected_sessions:
logger.warning(analysis_client.ctrl_ping())
socketio.sleep(3)
analysis_clients.pop(session_id, None)
corpus.status = 'stop analysis'
db.session.commit()