2020-03-28 18:29:19 +00:00
|
|
|
from flask import current_app, request
|
|
|
|
from flask_login import current_user
|
2020-03-29 18:33:00 +00:00
|
|
|
from socket import gaierror
|
2020-04-22 07:29:45 +00:00
|
|
|
from .. import db, socketio
|
2020-03-28 18:29:19 +00:00
|
|
|
from ..decorators import socketio_login_required
|
|
|
|
from ..events import connected_sessions
|
|
|
|
from ..models import Corpus, User
|
2020-04-24 13:44:40 +00:00
|
|
|
import cqi
|
2020-03-28 18:29:19 +00:00
|
|
|
import math
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
' A dictionary containing lists of, with corpus ids associated, Socket.IO
|
|
|
|
' session ids (sid). {<corpus_id>: [<sid>, ...], ...}
|
|
|
|
'''
|
2020-04-06 13:28:58 +00:00
|
|
|
corpus_analysis_sessions = {}
|
2020-03-28 18:29:19 +00:00
|
|
|
'''
|
|
|
|
' A dictionary containing Socket.IO session id - CQi client pairs.
|
|
|
|
' {<sid>: CQiClient, ...}
|
|
|
|
'''
|
2020-04-06 13:28:58 +00:00
|
|
|
corpus_analysis_clients = {}
|
2020-03-28 18:29:19 +00:00
|
|
|
|
|
|
|
|
2020-04-06 13:28:58 +00:00
|
|
|
@socketio.on('corpus_analysis_init')
|
2020-03-28 18:29:19 +00:00
|
|
|
@socketio_login_required
|
2020-04-06 13:28:58 +00:00
|
|
|
def init_corpus_analysis(corpus_id):
|
|
|
|
socketio.start_background_task(corpus_analysis_session_handler,
|
2020-03-28 18:29:19 +00:00
|
|
|
current_app._get_current_object(),
|
|
|
|
corpus_id, current_user.id, request.sid)
|
|
|
|
|
|
|
|
|
2020-04-06 13:28:58 +00:00
|
|
|
@socketio.on('corpus_analysis_query')
|
2020-03-28 18:29:19 +00:00
|
|
|
@socketio_login_required
|
2020-04-06 13:28:58 +00:00
|
|
|
def corpus_analysis_query(query):
|
|
|
|
client = corpus_analysis_clients.get(request.sid)
|
2020-03-28 18:29:19 +00:00
|
|
|
if client is None:
|
2020-04-09 13:14:39 +00:00
|
|
|
response = {'code': 424, 'desc': 'No client found for this session',
|
2020-03-29 17:40:29 +00:00
|
|
|
'msg': 'Failed Dependency'}
|
2020-04-06 13:28:58 +00:00
|
|
|
socketio.emit('corpus_analysis_query', response, room=request.sid)
|
2020-03-28 18:29:19 +00:00
|
|
|
return
|
2020-03-30 06:36:55 +00:00
|
|
|
if client.status == 'running':
|
|
|
|
client.status = 'abort'
|
|
|
|
while client.status != 'ready':
|
|
|
|
socketio.sleep(0.1)
|
2020-03-28 18:29:19 +00:00
|
|
|
try:
|
2020-03-29 17:40:29 +00:00
|
|
|
corpus = client.corpora.get('CORPUS')
|
2020-03-29 18:54:51 +00:00
|
|
|
query_status = corpus.query(query)
|
|
|
|
results = corpus.subcorpora.get('Results')
|
2020-03-29 17:40:29 +00:00
|
|
|
except cqi.errors.CQiException as e:
|
2020-04-09 13:14:39 +00:00
|
|
|
payload = {'code': e.code, 'desc': e.description, 'msg': e.name}
|
2020-03-29 17:40:29 +00:00
|
|
|
response = {'code': 500, 'desc': None, 'msg': 'Internal Server Error',
|
2020-04-09 13:14:39 +00:00
|
|
|
'payload': payload}
|
2020-04-06 13:28:58 +00:00
|
|
|
socketio.emit('corpus_analysis_query', response, room=request.sid)
|
2020-03-29 17:40:29 +00:00
|
|
|
return
|
2020-04-09 13:14:39 +00:00
|
|
|
payload = {**query_status, 'match_count': results.attrs['size']}
|
|
|
|
response = {'code': 200, 'desc': None, 'msg': 'OK', 'payload': payload}
|
2020-04-06 13:28:58 +00:00
|
|
|
socketio.emit('corpus_analysis_query', response, room=request.sid)
|
2020-04-09 13:14:39 +00:00
|
|
|
# TODO: Stop here and add a new method for transmission
|
2020-03-29 17:40:29 +00:00
|
|
|
chunk_size = 100
|
|
|
|
chunk_start = 0
|
2020-04-20 13:09:08 +00:00
|
|
|
context = 50
|
2020-03-29 17:40:29 +00:00
|
|
|
progress = 0
|
2020-03-30 06:36:55 +00:00
|
|
|
client.status = 'running'
|
2020-04-07 14:25:44 +00:00
|
|
|
while chunk_start <= results.attrs['size']:
|
2020-03-30 06:36:55 +00:00
|
|
|
if client.status == 'abort':
|
|
|
|
break
|
2020-03-29 17:40:29 +00:00
|
|
|
chunk = results.export(context=context, cutoff=chunk_size,
|
2020-04-02 09:38:27 +00:00
|
|
|
expand_lists=False, offset=chunk_start)
|
|
|
|
chunk['cpos_ranges'] = True
|
2020-04-07 14:25:44 +00:00
|
|
|
if (results.attrs['size'] == 0):
|
2020-03-29 17:40:29 +00:00
|
|
|
progress = 100
|
|
|
|
else:
|
2020-04-07 14:25:44 +00:00
|
|
|
progress = ((chunk_start + chunk_size) / results.attrs['size']) * 100
|
2020-03-29 17:40:29 +00:00
|
|
|
progress = min(100, int(math.ceil(progress)))
|
|
|
|
response = {'code': 200, 'desc': None, 'msg': 'OK',
|
|
|
|
'payload': {'chunk': chunk, 'progress': progress}}
|
2020-04-06 13:28:58 +00:00
|
|
|
socketio.emit('corpus_analysis_query_results', response,
|
2020-03-29 17:40:29 +00:00
|
|
|
room=request.sid)
|
|
|
|
chunk_start += chunk_size
|
2020-03-30 06:36:55 +00:00
|
|
|
client.status = 'ready'
|
2020-03-28 18:29:19 +00:00
|
|
|
|
|
|
|
|
2020-04-06 13:28:58 +00:00
|
|
|
@socketio.on('corpus_analysis_inspect_match')
|
2020-04-02 12:22:03 +00:00
|
|
|
@socketio_login_required
|
2020-04-06 13:28:58 +00:00
|
|
|
def corpus_analysis_inspect_match(payload):
|
2020-04-06 10:32:29 +00:00
|
|
|
payload = payload["payload"]
|
2020-04-06 13:28:58 +00:00
|
|
|
client = corpus_analysis_clients.get(request.sid)
|
2020-04-02 12:22:03 +00:00
|
|
|
if client is None:
|
2020-04-09 13:14:39 +00:00
|
|
|
response = {'code': 424, 'desc': 'No client found for this session',
|
|
|
|
'msg': 'Failed Dependency'}
|
|
|
|
socketio.emit('corpus_analysis_inspect_match', response, room=request.sid)
|
2020-04-02 12:22:03 +00:00
|
|
|
return
|
2020-04-09 13:14:39 +00:00
|
|
|
try:
|
|
|
|
corpus = client.corpora.get('CORPUS')
|
|
|
|
s = corpus.structural_attributes.get('s')
|
2020-04-15 12:55:29 +00:00
|
|
|
payload = s.export(payload['first_cpos'], payload['last_cpos'], context=10)
|
2020-04-09 13:14:39 +00:00
|
|
|
payload['cpos_ranges'] = True
|
|
|
|
except cqi.errors.CQiException as e:
|
|
|
|
payload = {'code': e.code, 'desc': e.description, 'msg': e.name}
|
|
|
|
response = {'code': 500, 'desc': None, 'msg': 'Internal Server Error',
|
|
|
|
'payload': payload}
|
|
|
|
else:
|
|
|
|
response = {'code': 200, 'desc': None, 'msg': 'OK', 'payload': payload}
|
|
|
|
socketio.emit('corpus_analysis_inspect_match', response, room=request.sid)
|
2020-04-02 12:22:03 +00:00
|
|
|
|
|
|
|
|
2020-04-06 13:28:58 +00:00
|
|
|
def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
|
2020-03-28 18:29:19 +00:00
|
|
|
with app.app_context():
|
|
|
|
''' Setup analysis session '''
|
|
|
|
corpus = Corpus.query.get(corpus_id)
|
|
|
|
user = User.query.get(user_id)
|
|
|
|
if corpus is None:
|
2020-03-29 17:40:29 +00:00
|
|
|
response = {'code': 404, 'desc': None, 'msg': 'Not Found'}
|
2020-04-06 13:28:58 +00:00
|
|
|
socketio.emit('corpus_analysis_init', response, room=session_id)
|
2020-03-28 18:29:19 +00:00
|
|
|
return
|
|
|
|
elif not (corpus.creator == user or user.is_administrator()):
|
2020-03-29 17:40:29 +00:00
|
|
|
response = {'code': 403, 'desc': None, 'msg': 'Forbidden'}
|
2020-04-06 13:28:58 +00:00
|
|
|
socketio.emit('corpus_analysis_init', response, room=session_id)
|
2020-03-28 18:29:19 +00:00
|
|
|
return
|
2020-04-09 13:14:39 +00:00
|
|
|
elif corpus.status == 'unprepared':
|
|
|
|
response = {'code': 424, 'desc': 'Corpus is not prepared',
|
|
|
|
'msg': 'Failed Dependency'}
|
|
|
|
socketio.emit('corpus_analysis_init', response, room=request.sid)
|
|
|
|
return
|
2020-03-28 18:29:19 +00:00
|
|
|
while corpus.status != 'analysing':
|
|
|
|
db.session.refresh(corpus)
|
|
|
|
socketio.sleep(3)
|
2020-04-17 14:00:40 +00:00
|
|
|
client = cqi.CQiClient('cqpserver_{}'.format(corpus_id))
|
2020-03-28 18:29:19 +00:00
|
|
|
try:
|
2020-04-09 13:14:39 +00:00
|
|
|
payload = client.connect()
|
2020-03-29 17:40:29 +00:00
|
|
|
except cqi.errors.CQiException as e:
|
2020-04-09 13:14:39 +00:00
|
|
|
payload = {'code': e.code, 'desc': e.description, 'msg': e.name}
|
2020-03-29 17:40:29 +00:00
|
|
|
response = {'code': 500, 'desc': None,
|
2020-04-09 13:14:39 +00:00
|
|
|
'msg': 'Internal Server Error', 'payload': payload}
|
2020-04-06 13:28:58 +00:00
|
|
|
socketio.emit('corpus_analysis_init', response, room=session_id)
|
2020-03-28 18:29:19 +00:00
|
|
|
return
|
2020-03-29 18:33:00 +00:00
|
|
|
except gaierror:
|
|
|
|
response = {'code': 500, 'desc': None,
|
|
|
|
'msg': 'Internal Server Error'}
|
2020-04-06 13:28:58 +00:00
|
|
|
socketio.emit('corpus_analysis_init', response, room=session_id)
|
2020-03-29 18:33:00 +00:00
|
|
|
return
|
2020-04-06 13:28:58 +00:00
|
|
|
corpus_analysis_clients[session_id] = client
|
|
|
|
if corpus_id not in corpus_analysis_sessions:
|
|
|
|
corpus_analysis_sessions[corpus_id] = [session_id]
|
2020-03-28 18:29:19 +00:00
|
|
|
else:
|
2020-04-06 13:28:58 +00:00
|
|
|
corpus_analysis_sessions[corpus_id].append(session_id)
|
2020-03-30 06:36:55 +00:00
|
|
|
client.status = 'ready'
|
2020-04-09 13:14:39 +00:00
|
|
|
response = {'code': 200, 'desc': None, 'msg': 'OK', 'payload': payload}
|
2020-04-06 13:28:58 +00:00
|
|
|
socketio.emit('corpus_analysis_init', response, room=session_id)
|
2020-03-28 18:29:19 +00:00
|
|
|
''' Observe analysis session '''
|
|
|
|
while session_id in connected_sessions:
|
|
|
|
socketio.sleep(3)
|
|
|
|
''' Teardown analysis session '''
|
2020-03-30 07:01:40 +00:00
|
|
|
if client.status == 'running':
|
|
|
|
client.status = 'abort'
|
|
|
|
while client.status != 'ready':
|
|
|
|
socketio.sleep(0.1)
|
2020-03-28 18:29:19 +00:00
|
|
|
try:
|
|
|
|
client.disconnect()
|
2020-03-29 10:38:24 +00:00
|
|
|
except cqi.errors.CQiException:
|
2020-03-28 18:29:19 +00:00
|
|
|
pass
|
2020-04-06 13:28:58 +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)
|
2020-03-28 18:29:19 +00:00
|
|
|
corpus.status = 'stop analysis'
|
|
|
|
db.session.commit()
|