2019-11-12 12:23:54 +00:00
|
|
|
from app import db, logger, socketio
|
2019-11-06 13:35:33 +00:00
|
|
|
from app.events import connected_sessions
|
2020-01-27 12:18:54 +00:00
|
|
|
from app.models import Corpus, User
|
2019-11-06 13:35:33 +00:00
|
|
|
from flask import current_app, request
|
2019-11-12 11:04:07 +00:00
|
|
|
from flask_login import current_user, login_required
|
2019-11-18 13:24:31 +00:00
|
|
|
from .CQiWrapper.CQiWrapper import CQiWrapper
|
2019-11-06 13:35:33 +00:00
|
|
|
|
2019-11-11 13:20:44 +00:00
|
|
|
'''
|
2019-11-12 11:04:07 +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.
|
2019-11-12 11:04:07 +00:00
|
|
|
' {<sid>: CQiClient, ...}
|
2019-11-11 13:20:44 +00:00
|
|
|
'''
|
2019-11-22 13:13:14 +00:00
|
|
|
corpus_analysis_clients = {}
|
2019-11-06 13:35:33 +00:00
|
|
|
|
|
|
|
|
2020-01-27 12:18:54 +00:00
|
|
|
@socketio.on('corpus_analysis_init')
|
2019-11-06 13:35:33 +00:00
|
|
|
@login_required
|
2020-01-27 12:18:54 +00:00
|
|
|
def init_corpus_analysis(corpus_id):
|
2020-01-27 15:11:34 +00:00
|
|
|
logger.warning("Initiating corpus analysis.")
|
2020-01-27 12:18:54 +00:00
|
|
|
socketio.start_background_task(corpus_analysis_session_handler,
|
|
|
|
current_app._get_current_object(),
|
|
|
|
corpus_id, current_user.id, request.sid)
|
2019-11-06 13:35:33 +00:00
|
|
|
|
|
|
|
|
2020-03-12 13:13:07 +00:00
|
|
|
@socketio.on('corpus_analysis_query')
|
2019-11-12 11:04:07 +00:00
|
|
|
@login_required
|
2020-03-12 13:13:07 +00:00
|
|
|
def corpus_analysis_query(query):
|
2019-11-22 13:13:14 +00:00
|
|
|
client = corpus_analysis_clients.get(request.sid)
|
|
|
|
if client is None:
|
2020-03-12 13:18:07 +00:00
|
|
|
socketio.emit('query', '[424]: Failed Dependency', room=request.sid)
|
2019-11-18 10:08:33 +00:00
|
|
|
return
|
2020-03-12 13:13:07 +00:00
|
|
|
client.select_corpus('CORPUS')
|
2020-01-16 10:56:16 +00:00
|
|
|
try:
|
|
|
|
client.query_subcorpus(query)
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning(e)
|
2020-01-27 15:11:34 +00:00
|
|
|
response = str(e)
|
|
|
|
if response == "CQI_CQP_ERROR_GENERAL":
|
2020-03-12 13:13:07 +00:00
|
|
|
response = {'code': 1}
|
2020-01-27 15:11:34 +00:00
|
|
|
socketio.emit('corpus_analysis_query', response, room=request.sid)
|
2020-01-16 10:56:16 +00:00
|
|
|
else:
|
2020-03-12 13:13:07 +00:00
|
|
|
chunk_size = 500
|
|
|
|
chunk_start = 0
|
2020-03-12 13:18:38 +00:00
|
|
|
context = 100
|
2020-03-12 13:13:07 +00:00
|
|
|
while chunk_start < client.match_count:
|
|
|
|
chunk = client.show_query_results(result_len=chunk_size,
|
2020-03-12 13:18:38 +00:00
|
|
|
context_len=context,
|
2020-03-12 13:13:07 +00:00
|
|
|
result_offset=chunk_start)
|
|
|
|
socketio.emit('corpus_analysis_query', chunk, room=request.sid)
|
|
|
|
chunk_start += chunk_size
|
2019-11-06 13:35:33 +00:00
|
|
|
|
|
|
|
|
2019-12-02 15:13:53 +00:00
|
|
|
@socketio.on('inspect_match')
|
|
|
|
@login_required
|
|
|
|
def inspect_match(message):
|
|
|
|
client = corpus_analysis_clients.get(request.sid)
|
|
|
|
if client is None:
|
|
|
|
socketio.emit('query', '[424]: Failed Dependency',
|
|
|
|
room=request.sid)
|
|
|
|
return
|
|
|
|
# Get more context for given match CPOS
|
|
|
|
match_context = client.get_sentences(message['cpos'],
|
|
|
|
get_surrounding_s=True,
|
|
|
|
l_r_s_context_additional_len=3)
|
|
|
|
socketio.emit('match_context', match_context, room=request.sid)
|
|
|
|
|
|
|
|
|
2020-01-27 12:18:54 +00:00
|
|
|
def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
|
2019-11-06 13:35:33 +00:00
|
|
|
with app.app_context():
|
2019-11-22 13:13:14 +00:00
|
|
|
''' Setup analysis session '''
|
|
|
|
corpus = Corpus.query.get(corpus_id)
|
2020-01-27 12:18:54 +00:00
|
|
|
user = User.query.get(user_id)
|
|
|
|
if corpus is None:
|
|
|
|
response = {'code': 404, 'msg': 'Not Found'}
|
|
|
|
socketio.emit('corpus_analysis_init', response, room=session_id)
|
|
|
|
return
|
|
|
|
elif not (corpus.creator == user or user.is_administrator()):
|
|
|
|
response = {'code': 403, 'msg': 'Forbidden'}
|
|
|
|
socketio.emit('corpus_analysis_init', response, room=session_id)
|
|
|
|
return
|
2019-11-22 13:13:14 +00:00
|
|
|
while corpus.status != 'analysing':
|
|
|
|
db.session.refresh(corpus)
|
|
|
|
socketio.sleep(3)
|
2019-12-03 10:46:56 +00:00
|
|
|
client = CQiWrapper(host='corpus_{}_analysis'.format(corpus_id))
|
2020-03-16 12:15:13 +00:00
|
|
|
try:
|
|
|
|
client.connect()
|
|
|
|
except Exception:
|
|
|
|
response = {'code': 500, 'msg': 'Internal Server Error'}
|
|
|
|
socketio.emit('corpus_analysis_init', response, room=session_id)
|
|
|
|
return
|
2019-11-22 13:13:14 +00:00
|
|
|
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)
|
2020-01-27 12:18:54 +00:00
|
|
|
response = {'code': 201, 'msg': 'Created'}
|
|
|
|
socketio.emit('corpus_analysis_init', response, room=session_id)
|
2019-11-22 13:13:14 +00:00
|
|
|
''' Observe analysis session '''
|
2019-11-06 13:35:33 +00:00
|
|
|
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 '''
|
2020-03-16 12:15:13 +00:00
|
|
|
try:
|
|
|
|
client.disconnect()
|
|
|
|
except Exception:
|
|
|
|
pass
|
2019-11-22 13:13:14 +00:00
|
|
|
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)
|
2019-11-11 10:51:18 +00:00
|
|
|
corpus.status = 'stop analysis'
|
2019-11-12 11:04:07 +00:00
|
|
|
db.session.commit()
|