Handle scoring presentation to client via websockets

This commit is contained in:
Ralf Teusner
2016-06-17 16:42:07 +02:00
parent 83d5e82e31
commit 96de763b83
2 changed files with 15 additions and 10 deletions

View File

@ -184,8 +184,8 @@ $(function() {
(streamed ? evaluateCodeWithStreamedResponse : evaluateCodeWithoutStreamedResponse)(url, callback); (streamed ? evaluateCodeWithStreamedResponse : evaluateCodeWithoutStreamedResponse)(url, callback);
}; };
var evaluateCodeWithStreamedResponse = function(url, callback) { var evaluateCodeWithStreamedResponse = function(url, onmessageFunction) {
initWebsocketConnection(url); initWebsocketConnection(url, onmessageFunction);
// TODO only init turtle when required // TODO only init turtle when required
initTurtle(); initTurtle();
@ -306,9 +306,10 @@ $(function() {
} }
}; };
var handleScoringResponse = function(response) { var handleScoringResponse = function(websocket_event) {
printScoringResults(response); results = JSON.parse(websocket_event.data);
var score = _.reduce(response, function(sum, result) { printScoringResults(results);
var score = _.reduce(results, function(sum, result) {
return sum + result.score * result.weight; return sum + result.score * result.weight;
}, 0).toFixed(2); }, 0).toFixed(2);
$('#score').data('score', score); $('#score').data('score', score);
@ -743,7 +744,7 @@ $(function() {
showSpinner($('#run')); showSpinner($('#run'));
toggleButtonStates(); toggleButtonStates();
var url = response.run_url.replace(FILENAME_URL_PLACEHOLDER, active_file.filename); var url = response.run_url.replace(FILENAME_URL_PLACEHOLDER, active_file.filename);
evaluateCode(url, true, printChunk); evaluateCode(url, true, function(evt) { parseCanvasMessage(evt.data, true); });
}); });
} }
}; };
@ -778,7 +779,7 @@ $(function() {
createSubmission(this, null, function(response) { createSubmission(this, null, function(response) {
showSpinner($('#assess')); showSpinner($('#assess'));
var url = response.score_url; var url = response.score_url;
evaluateCode(url, false, handleScoringResponse); evaluateCode(url, true, handleScoringResponse);
}); });
}; };
@ -974,14 +975,14 @@ $(function() {
$('#test').toggle(isActiveFileTestable()); $('#test').toggle(isActiveFileTestable());
}; };
var initWebsocketConnection = function(url) { var initWebsocketConnection = function(url, onmessageFunction) {
//TODO: get the protocol from config file dependent on environment. (dev: ws, prod: wss) //TODO: get the protocol from config file dependent on environment. (dev: ws, prod: wss)
//causes: Puma::HttpParserError: Invalid HTTP format, parsing fails. //causes: Puma::HttpParserError: Invalid HTTP format, parsing fails.
//TODO: make sure that this gets cached. //TODO: make sure that this gets cached.
websocket = new WebSocket('<%= DockerClient.config['ws_client_protocol'] %>' + window.location.hostname + ':' + window.location.port + url); websocket = new WebSocket('<%= DockerClient.config['ws_client_protocol'] %>' + window.location.hostname + ':' + window.location.port + url);
websocket.onopen = function(evt) { resetOutputTab(); }; // todo show some kind of indicator for established connection websocket.onopen = function(evt) { resetOutputTab(); }; // todo show some kind of indicator for established connection
websocket.onclose = function(evt) { /* expected at some point */ }; websocket.onclose = function(evt) { /* expected at some point */ };
websocket.onmessage = function(evt) { parseCanvasMessage(evt.data, true); }; websocket.onmessage = onmessageFunction;
websocket.onerror = function(evt) { showWebsocketError(); }; websocket.onerror = function(evt) { showWebsocketError(); };
websocket.flush = function() { this.send('\n'); } websocket.flush = function() { this.send('\n'); }
}; };

View File

@ -214,7 +214,11 @@ class SubmissionsController < ApplicationController
end end
def score def score
render(json: score_submission(@submission)) hijack do |tubesock|
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
# tubesock is the socket to the client
tubesock.send_data JSON.dump(score_submission(@submission))
end
end end
def set_docker_client def set_docker_client