nopaque/app/corpora/events.py

87 lines
3.2 KiB
Python
Raw Normal View History

2019-11-12 12:23:54 +00:00
from app import db, logger, socketio
from app.events import connected_sessions
from app.models import Corpus
from flask import current_app, request
from flask_login import current_user, login_required
2019-11-18 13:24:31 +00:00
from .CQiWrapper.CQiWrapper import CQiWrapper
2019-11-11 13:20:44 +00:00
'''
' A dictionary containing lists of, with corpus ids associated, Socket.IO
' session ids (sid). {<corpus_id>: [<sid>, ...], ...}
2019-11-11 13:20:44 +00:00
'''
2019-11-22 13:13:14 +00:00
corpus_analysis_sessions = {}
2019-11-11 13:20:44 +00:00
'''
' A dictionary containing Socket.IO session id - CQi client pairs.
' {<sid>: CQiClient, ...}
2019-11-11 13:20:44 +00:00
'''
2019-11-22 13:13:14 +00:00
corpus_analysis_clients = {}
2019-11-22 13:13:14 +00:00
@socketio.on('request_corpus_analysis')
@login_required
2019-11-22 13:13:14 +00:00
def request_corpus_analysis(corpus_id):
2019-11-15 12:09:12 +00:00
corpus = Corpus.query.get(corpus_id)
if corpus is None:
2019-11-22 13:13:14 +00:00
socketio.emit('init_corpus_analysis', '[404]: Not Found',
room=request.sid)
2019-11-18 10:08:33 +00:00
elif not (corpus.creator == current_user
or current_user.is_administrator()):
2019-11-22 13:13:14 +00:00
socketio.emit('init_corpus_analysis', '[403]: Forbidden',
room=request.sid)
2019-11-14 12:19:05 +00:00
else:
2019-11-22 13:13:14 +00:00
socketio.start_background_task(corpus_analysis_session_handler,
2019-11-18 10:08:33 +00:00
current_app._get_current_object(),
corpus_id, request.sid)
2019-11-22 13:13:14 +00:00
@socketio.on('corpus_analysis')
@login_required
2019-11-22 13:13:14 +00:00
def corpus_analysis(message):
client = corpus_analysis_clients.get(request.sid)
if client is None:
socketio.emit('query', '[424]: Failed Dependency',
2019-11-18 10:08:33 +00:00
room=request.sid)
return
2019-11-12 13:00:03 +00:00
""" Prepare and execute a query """
2019-11-28 13:14:56 +00:00
corpus_name = 'CORPUS'
2019-11-28 14:19:52 +00:00
query = str(message['query'])
result_len = int(message['hits_per_page'])
context_len = int(message['context'])
result_offset = int(0)
2019-11-28 13:14:56 +00:00
client.select_corpus(corpus_name)
client.query_subcorpus(query)
2019-11-28 14:19:52 +00:00
results = client.show_query_results(result_len=result_len,
context_len=context_len,
result_offset=result_offset)
2019-11-27 12:29:43 +00:00
2019-11-28 13:14:56 +00:00
socketio.emit('corpus_analysis', results, room=request.sid)
2019-11-22 13:13:14 +00:00
def corpus_analysis_session_handler(app, corpus_id, session_id):
with app.app_context():
2019-11-22 13:13:14 +00:00
''' Setup analysis session '''
corpus = Corpus.query.get(corpus_id)
while corpus.status != 'analysing':
db.session.refresh(corpus)
socketio.sleep(3)
client = CQiWrapper(host='analyse_corpus_{}'.format(corpus_id))
client.connect()
corpus_analysis_clients[session_id] = client
if corpus_id not in corpus_analysis_sessions:
corpus_analysis_sessions[corpus_id] = [session_id]
else:
corpus_analysis_sessions[corpus_id].append(session_id)
2019-11-25 09:12:39 +00:00
socketio.emit('request_corpus_analysis', '[201]: Created',
room=session_id)
2019-11-22 13:13:14 +00:00
''' Observe analysis session '''
while session_id in connected_sessions:
2019-11-12 12:23:54 +00:00
socketio.sleep(3)
2019-11-22 13:13:14 +00:00
''' Teardown analysis session '''
client.disconnect()
corpus_analysis_clients.pop(session_id, None)
corpus_analysis_sessions[corpus_id].remove(session_id)
if not corpus_analysis_sessions[corpus_id]:
corpus_analysis_sessions.pop(corpus_id, None)
corpus.status = 'stop analysis'
db.session.commit()