2019-09-02 08:42:28 +00:00
|
|
|
from flask import current_app, request
|
2019-08-27 08:40:29 +00:00
|
|
|
from flask_login import current_user, login_required
|
2019-09-18 09:33:06 +00:00
|
|
|
from ..decorators import admin_required
|
2019-09-02 08:42:28 +00:00
|
|
|
from .. import db, socketio
|
2019-08-28 15:27:43 +00:00
|
|
|
from ..models import User
|
2019-08-27 08:40:29 +00:00
|
|
|
import json
|
2019-08-29 08:42:53 +00:00
|
|
|
import jsonpatch
|
2019-11-04 12:18:55 +00:00
|
|
|
import logging
|
2019-09-02 08:59:13 +00:00
|
|
|
|
2019-08-28 15:27:43 +00:00
|
|
|
|
2019-08-29 08:42:53 +00:00
|
|
|
'''
|
|
|
|
' A list containing session ids of disconnected Socket.IO sessions. It is used
|
|
|
|
' to signal associated background tasks to stop.
|
|
|
|
'''
|
|
|
|
disconnected = []
|
2019-08-27 08:40:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@socketio.on('connect')
|
|
|
|
@login_required
|
|
|
|
def connect():
|
2019-09-02 07:17:00 +00:00
|
|
|
'''
|
|
|
|
' The Socket.IO module creates a session id (sid) on each request. The
|
|
|
|
' initiating client is automatically placed in a room with that sid, which
|
|
|
|
' will be used for further information exchange generated by a background
|
|
|
|
' task associated with the sid.
|
|
|
|
'''
|
2019-08-28 15:27:43 +00:00
|
|
|
socketio.start_background_task(background_task,
|
2019-09-02 08:42:28 +00:00
|
|
|
current_app._get_current_object(),
|
2019-08-28 15:27:43 +00:00
|
|
|
current_user.id,
|
|
|
|
request.sid)
|
|
|
|
|
|
|
|
|
2019-09-18 12:05:56 +00:00
|
|
|
@socketio.on('inspect_user')
|
2019-09-18 09:33:06 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-09-18 12:05:56 +00:00
|
|
|
def inspect_user(user_id):
|
2019-09-18 09:33:06 +00:00
|
|
|
'''
|
|
|
|
' The Socket.IO module creates a session id (sid) on each request. The
|
|
|
|
' initiating admin is automatically placed in a room with that sid, which
|
|
|
|
' will be used for further information exchange generated by a background
|
|
|
|
' task associated with the sid. Admin will be placed in that room on emiting
|
|
|
|
' "conncect_admin".
|
|
|
|
'''
|
2019-09-18 12:05:56 +00:00
|
|
|
socketio.start_background_task(background_task,
|
2019-09-18 09:33:06 +00:00
|
|
|
current_app._get_current_object(),
|
2019-09-18 12:05:56 +00:00
|
|
|
user_id,
|
|
|
|
request.sid,
|
|
|
|
True)
|
2019-09-18 09:33:06 +00:00
|
|
|
|
|
|
|
|
2019-08-28 15:27:43 +00:00
|
|
|
@socketio.on('disconnect')
|
|
|
|
@login_required
|
|
|
|
def disconnect():
|
2019-09-02 07:17:00 +00:00
|
|
|
'''
|
|
|
|
' On disconnect the session id (sid) of the connection gets placed in the
|
|
|
|
' disconnected list (see above).
|
|
|
|
'''
|
2019-08-29 08:42:53 +00:00
|
|
|
disconnected.append(request.sid)
|
2019-08-28 15:27:43 +00:00
|
|
|
|
|
|
|
|
2019-11-04 12:18:55 +00:00
|
|
|
@socketio.on('query_event')
|
|
|
|
def recv_query(message):
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.warning(message)
|
|
|
|
|
|
|
|
|
2019-09-18 12:05:56 +00:00
|
|
|
def background_task(app, user_id, session_id, foreign=False):
|
2019-09-18 09:33:06 +00:00
|
|
|
'''
|
|
|
|
' Sends initial corpus and job lists to the client. Afterwards it checks
|
|
|
|
' every 3 seconds if changes to the initial values appeared. If changes are
|
|
|
|
' detected, a RFC 6902 compliant JSON patch gets send.
|
|
|
|
'
|
|
|
|
' NOTE: The initial values are send as a init-* events.
|
|
|
|
' The JSON patches are send as update-* events.
|
|
|
|
'
|
|
|
|
' > where '*' is either 'corpora' or 'jobs'
|
|
|
|
'''
|
|
|
|
with app.app_context():
|
2019-10-29 08:51:47 +00:00
|
|
|
user = db.session.query(User).get_or_404(user_id)
|
2019-09-18 09:33:06 +00:00
|
|
|
''' Get current values from the database. '''
|
|
|
|
corpora = user.corpora_as_dict()
|
|
|
|
jobs = user.jobs_as_dict()
|
|
|
|
''' Send initial values. '''
|
2019-09-18 12:05:56 +00:00
|
|
|
socketio.emit('init-foreign-corpora' if foreign else 'init-corpora',
|
2019-09-18 09:33:06 +00:00
|
|
|
json.dumps(corpora),
|
|
|
|
room=session_id)
|
2019-09-18 12:05:56 +00:00
|
|
|
socketio.emit('init-foreign-jobs' if foreign else 'init-jobs',
|
2019-09-18 09:33:06 +00:00
|
|
|
json.dumps(jobs),
|
|
|
|
room=session_id)
|
|
|
|
''' TODO: Implement maximum runtime for this loop. '''
|
|
|
|
while session_id not in disconnected:
|
|
|
|
''' Get current values from the database '''
|
|
|
|
new_corpora = user.corpora_as_dict()
|
|
|
|
new_jobs = user.jobs_as_dict()
|
|
|
|
''' Compute JSON patches. '''
|
|
|
|
corpus_patch = jsonpatch.JsonPatch.from_diff(corpora, new_corpora)
|
|
|
|
jobs_patch = jsonpatch.JsonPatch.from_diff(jobs, new_jobs)
|
|
|
|
''' In case there are patches, send them to the user. '''
|
|
|
|
if corpus_patch:
|
2019-09-18 12:05:56 +00:00
|
|
|
socketio.emit('update-foreign-corpora' if foreign else 'update-corpora',
|
2019-09-18 09:33:06 +00:00
|
|
|
corpus_patch.to_string(),
|
|
|
|
room=session_id)
|
|
|
|
if jobs_patch:
|
2019-09-18 12:05:56 +00:00
|
|
|
socketio.emit('update-foreign-jobs' if foreign else 'update-jobs',
|
2019-09-18 09:33:06 +00:00
|
|
|
jobs_patch.to_string(),
|
|
|
|
room=session_id)
|
|
|
|
''' Set new values as references for the next iteration. '''
|
|
|
|
corpora = new_corpora
|
|
|
|
jobs = new_jobs
|
|
|
|
socketio.sleep(3)
|
|
|
|
disconnected.remove(session_id)
|