This commit is contained in:
leo.selig
2016-02-04 11:02:56 +01:00
21 changed files with 782 additions and 43 deletions

View File

@ -7,7 +7,7 @@ gem 'carrierwave'
gem 'coffee-rails', '~> 4.0.0' gem 'coffee-rails', '~> 4.0.0'
gem 'concurrent-ruby', '~> 1.0.0' gem 'concurrent-ruby', '~> 1.0.0'
gem 'concurrent-ruby-ext', '~> 1.0.0', platform: :ruby gem 'concurrent-ruby-ext', '~> 1.0.0', platform: :ruby
gem 'docker-api','~> 1.21.1', require: 'docker' gem 'docker-api','~> 1.25.0', require: 'docker'
gem 'factory_girl_rails', '~> 4.0' gem 'factory_girl_rails', '~> 4.0'
gem 'forgery' gem 'forgery'
gem 'highline' gem 'highline'

View File

@ -103,13 +103,13 @@ GEM
debug_inspector (0.0.2) debug_inspector (0.0.2)
diff-lcs (1.2.5) diff-lcs (1.2.5)
docile (1.1.5) docile (1.1.5)
docker-api (1.21.1) docker-api (1.25.0)
excon (>= 0.38.0) excon (>= 0.38.0)
json json
erubis (2.7.0) erubis (2.7.0)
eventmachine (1.0.8) eventmachine (1.0.8)
eventmachine (1.0.8-java) eventmachine (1.0.8-java)
excon (0.45.2) excon (0.45.4)
execjs (2.5.2) execjs (2.5.2)
factory_girl (4.5.0) factory_girl (4.5.0)
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
@ -350,7 +350,7 @@ DEPENDENCIES
concurrent-ruby (~> 1.0.0) concurrent-ruby (~> 1.0.0)
concurrent-ruby-ext (~> 1.0.0) concurrent-ruby-ext (~> 1.0.0)
database_cleaner database_cleaner
docker-api (~> 1.21.1) docker-api (~> 1.25.0)
factory_girl_rails (~> 4.0) factory_girl_rails (~> 4.0)
faye-websocket faye-websocket
forgery forgery

View File

@ -30,7 +30,7 @@ $(function() {
numMessages = 0, numMessages = 0,
turtlecanvas = $('#turtlecanvas'), turtlecanvas = $('#turtlecanvas'),
prompt = $('#prompt'), prompt = $('#prompt'),
commands = ['input', 'write', 'turtle', 'turtlebatch', 'exit', 'timeout', 'status'], commands = ['input', 'write', 'turtle', 'turtlebatch', 'render', 'exit', 'timeout', 'status'],
streams = ['stdin', 'stdout', 'stderr']; streams = ['stdin', 'stdout', 'stderr'];
var ENTER_KEY_CODE = 13; var ENTER_KEY_CODE = 13;
@ -245,6 +245,18 @@ $(function() {
} }
}; };
var findOrCreateRenderElement = function(index) {
if ($('#render-' + index).isPresent()) {
return $('#render-' + index);
} else {
var element = $('<div>').attr('id', 'render-' + index);
$('#render').append(element);
return element;
}
};
var getPanelClass = function(result) { var getPanelClass = function(result) {
if (result.stderr && !result.score) { if (result.stderr && !result.score) {
return 'panel-danger'; return 'panel-danger';
@ -1172,6 +1184,9 @@ $(function() {
showCanvas(); showCanvas();
handleTurtlebatchCommand(msg); handleTurtlebatchCommand(msg);
break; break;
case 'render':
renderWebsocketOutput(msg);
break;
case 'exit': case 'exit':
killWebsocketAndContainer(); killWebsocketAndContainer();
break; break;
@ -1185,6 +1200,11 @@ $(function() {
} }
}; };
var renderWebsocketOutput = function(msg){
var element = findOrCreateRenderElement(0);
element.append(msg.data);
};
var printWebsocketOutput = function(msg) { var printWebsocketOutput = function(msg) {
if (!msg.data) { if (!msg.data) {
return; return;

View File

@ -0,0 +1,498 @@
$(document).ready(function(){
(function vendorTableSorter(){
/*
SortTable
version 2
7th April 2007
Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
Instructions:
Download this file
Add <script src="sorttable.js"></script> to your HTML
Add class="sortable" to any table you'd like to make sortable
Click on the headers to sort
Thanks to many, many people for contributions and suggestions.
Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
This basically means: do what you want with it.
*/
var stIsIE = /*@cc_on!@*/false;
sorttable = {
init: function() {
// quit if this function has already been called
if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice
arguments.callee.done = true;
// kill the timer
if (_timer) clearInterval(_timer);
if (!document.createElement || !document.getElementsByTagName) return;
sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
forEach(document.getElementsByTagName('table'), function(table) {
if (table.className.search(/\bsortable\b/) != -1) {
sorttable.makeSortable(table);
}
});
},
makeSortable: function(table) {
if (table.getElementsByTagName('thead').length == 0) {
// table doesn't have a tHead. Since it should have, create one and
// put the first table row in it.
the = document.createElement('thead');
the.appendChild(table.rows[0]);
table.insertBefore(the,table.firstChild);
}
// Safari doesn't support table.tHead, sigh
if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
if (table.tHead.rows.length != 1) return; // can't cope with two header rows
// Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
// "total" rows, for example). This is B&R, since what you're supposed
// to do is put them in a tfoot. So, if there are sortbottom rows,
// for backwards compatibility, move them to tfoot (creating it if needed).
sortbottomrows = [];
for (var i=0; i<table.rows.length; i++) {
if (table.rows[i].className.search(/\bsortbottom\b/) != -1) {
sortbottomrows[sortbottomrows.length] = table.rows[i];
}
}
if (sortbottomrows) {
if (table.tFoot == null) {
// table doesn't have a tfoot. Create one.
tfo = document.createElement('tfoot');
table.appendChild(tfo);
}
for (var i=0; i<sortbottomrows.length; i++) {
tfo.appendChild(sortbottomrows[i]);
}
delete sortbottomrows;
}
// work through each column and calculate its type
headrow = table.tHead.rows[0].cells;
for (var i=0; i<headrow.length; i++) {
// manually override the type with a sorttable_type attribute
if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
if (mtch) { override = mtch[1]; }
if (mtch && typeof sorttable["sort_"+override] == 'function') {
headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
} else {
headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
}
// make it clickable to sort
headrow[i].sorttable_columnindex = i;
headrow[i].sorttable_tbody = table.tBodies[0];
dean_addEvent(headrow[i],"click", sorttable.innerSortFunction = function(e) {
if (this.className.search(/\bsorttable_sorted\b/) != -1) {
// if we're already sorted by this column, just
// reverse the table, which is quicker
sorttable.reverse(this.sorttable_tbody);
this.className = this.className.replace('sorttable_sorted',
'sorttable_sorted_reverse');
this.removeChild(document.getElementById('sorttable_sortfwdind'));
sortrevind = document.createElement('span');
sortrevind.id = "sorttable_sortrevind";
sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
this.appendChild(sortrevind);
return;
}
if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {
// if we're already sorted by this column in reverse, just
// re-reverse the table, which is quicker
sorttable.reverse(this.sorttable_tbody);
this.className = this.className.replace('sorttable_sorted_reverse',
'sorttable_sorted');
this.removeChild(document.getElementById('sorttable_sortrevind'));
sortfwdind = document.createElement('span');
sortfwdind.id = "sorttable_sortfwdind";
sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
this.appendChild(sortfwdind);
return;
}
// remove sorttable_sorted classes
theadrow = this.parentNode;
forEach(theadrow.childNodes, function(cell) {
if (cell.nodeType == 1) { // an element
cell.className = cell.className.replace('sorttable_sorted_reverse','');
cell.className = cell.className.replace('sorttable_sorted','');
}
});
sortfwdind = document.getElementById('sorttable_sortfwdind');
if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }
sortrevind = document.getElementById('sorttable_sortrevind');
if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }
this.className += ' sorttable_sorted';
sortfwdind = document.createElement('span');
sortfwdind.id = "sorttable_sortfwdind";
sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
this.appendChild(sortfwdind);
// build an array to sort. This is a Schwartzian transform thing,
// i.e., we "decorate" each row with the actual sort key,
// sort based on the sort keys, and then put the rows back in order
// which is a lot faster because you only do getInnerText once per row
row_array = [];
col = this.sorttable_columnindex;
rows = this.sorttable_tbody.rows;
for (var j=0; j<rows.length; j++) {
row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
}
/* If you want a stable sort, uncomment the following line */
//sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
/* and comment out this one */
row_array.sort(this.sorttable_sortfunction);
tb = this.sorttable_tbody;
for (var j=0; j<row_array.length; j++) {
tb.appendChild(row_array[j][1]);
}
delete row_array;
});
}
}
},
guessType: function(table, column) {
// guess the type of a column based on its first non-blank row
sortfn = sorttable.sort_alpha;
for (var i=0; i<table.tBodies[0].rows.length; i++) {
text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
if (text != '') {
if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
return sorttable.sort_numeric;
}
// check for a date: dd/mm/yyyy or dd/mm/yy
// can have / or . or - as separator
// can be mm/dd as well
possdate = text.match(sorttable.DATE_RE)
if (possdate) {
// looks like a date
first = parseInt(possdate[1]);
second = parseInt(possdate[2]);
if (first > 12) {
// definitely dd/mm
return sorttable.sort_ddmm;
} else if (second > 12) {
return sorttable.sort_mmdd;
} else {
// looks like a date, but we can't tell which, so assume
// that it's dd/mm (English imperialism!) and keep looking
sortfn = sorttable.sort_ddmm;
}
}
}
}
return sortfn;
},
getInnerText: function(node) {
// gets the text we want to use for sorting for a cell.
// strips leading and trailing whitespace.
// this is *not* a generic getInnerText function; it's special to sorttable.
// for example, you can override the cell text with a customkey attribute.
// it also gets .value for <input> fields.
if (!node) return "";
hasInputs = (typeof node.getElementsByTagName == 'function') &&
node.getElementsByTagName('input').length;
if (node.getAttribute("sorttable_customkey") != null) {
return node.getAttribute("sorttable_customkey");
}
else if (typeof node.textContent != 'undefined' && !hasInputs) {
return node.textContent.replace(/^\s+|\s+$/g, '');
}
else if (typeof node.innerText != 'undefined' && !hasInputs) {
return node.innerText.replace(/^\s+|\s+$/g, '');
}
else if (typeof node.text != 'undefined' && !hasInputs) {
return node.text.replace(/^\s+|\s+$/g, '');
}
else {
switch (node.nodeType) {
case 3:
if (node.nodeName.toLowerCase() == 'input') {
return node.value.replace(/^\s+|\s+$/g, '');
}
case 4:
return node.nodeValue.replace(/^\s+|\s+$/g, '');
break;
case 1:
case 11:
var innerText = '';
for (var i = 0; i < node.childNodes.length; i++) {
innerText += sorttable.getInnerText(node.childNodes[i]);
}
return innerText.replace(/^\s+|\s+$/g, '');
break;
default:
return '';
}
}
},
reverse: function(tbody) {
// reverse the rows in a tbody
newrows = [];
for (var i=0; i<tbody.rows.length; i++) {
newrows[newrows.length] = tbody.rows[i];
}
for (var i=newrows.length-1; i>=0; i--) {
tbody.appendChild(newrows[i]);
}
delete newrows;
},
/* sort functions
each sort function takes two parameters, a and b
you are comparing a[0] and b[0] */
sort_numeric: function(a,b) {
aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
if (isNaN(aa)) aa = 0;
bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
if (isNaN(bb)) bb = 0;
return aa-bb;
},
sort_alpha: function(a,b) {
if (a[0]==b[0]) return 0;
if (a[0]<b[0]) return -1;
return 1;
},
sort_ddmm: function(a,b) {
mtch = a[0].match(sorttable.DATE_RE);
y = mtch[3]; m = mtch[2]; d = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt1 = y+m+d;
mtch = b[0].match(sorttable.DATE_RE);
y = mtch[3]; m = mtch[2]; d = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt2 = y+m+d;
if (dt1==dt2) return 0;
if (dt1<dt2) return -1;
return 1;
},
sort_mmdd: function(a,b) {
mtch = a[0].match(sorttable.DATE_RE);
y = mtch[3]; d = mtch[2]; m = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt1 = y+m+d;
mtch = b[0].match(sorttable.DATE_RE);
y = mtch[3]; d = mtch[2]; m = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt2 = y+m+d;
if (dt1==dt2) return 0;
if (dt1<dt2) return -1;
return 1;
},
shaker_sort: function(list, comp_func) {
// A stable sort function to allow multi-level sorting of data
// see: http://en.wikipedia.org/wiki/Cocktail_sort
// thanks to Joseph Nahmias
var b = 0;
var t = list.length - 1;
var swap = true;
while(swap) {
swap = false;
for(var i = b; i < t; ++i) {
if ( comp_func(list[i], list[i+1]) > 0 ) {
var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
swap = true;
}
} // for
t--;
if (!swap) break;
for(var i = t; i > b; --i) {
if ( comp_func(list[i], list[i-1]) < 0 ) {
var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
swap = true;
}
} // for
b++;
} // while(swap)
}
}
/* ******************************************************************
Supporting functions: bundled here to avoid depending on a library
****************************************************************** */
// Dean Edwards/Matthias Miller/John Resig
/* for Mozilla/Opera9 */
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", sorttable.init, false);
}
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") {
sorttable.init(); // call the onload handler
}
};
/*@end @*/
/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
sorttable.init(); // call the onload handler
}
}, 10);
}
/* for other browsers */
window.onload = sorttable.init;
// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini
// http://dean.edwards.name/weblog/2005/10/add-event/
function dean_addEvent(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
// assign each event handler a unique ID
if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
// create a hash table of event types for the element
if (!element.events) element.events = {};
// create a hash table of event handlers for each element/event pair
var handlers = element.events[type];
if (!handlers) {
handlers = element.events[type] = {};
// store the existing event handler (if there is one)
if (element["on" + type]) {
handlers[0] = element["on" + type];
}
}
// store the event handler in the hash table
handlers[handler.$$guid] = handler;
// assign a global event handler to do all the work
element["on" + type] = handleEvent;
}
};
// a counter used to create unique IDs
dean_addEvent.guid = 1;
function removeEvent(element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else {
// delete the event handler from the hash table
if (element.events && element.events[type]) {
delete element.events[type][handler.$$guid];
}
}
};
function handleEvent(event) {
var returnValue = true;
// grab the event object (IE uses a global event object)
event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
// get a reference to the hash table of event handlers
var handlers = this.events[event.type];
// execute each event handler
for (var i in handlers) {
this.$$handleEvent = handlers[i];
if (this.$$handleEvent(event) === false) {
returnValue = false;
}
}
return returnValue;
};
function fixEvent(event) {
// add W3C standard event methods
event.preventDefault = fixEvent.preventDefault;
event.stopPropagation = fixEvent.stopPropagation;
return event;
};
fixEvent.preventDefault = function() {
this.returnValue = false;
};
fixEvent.stopPropagation = function() {
this.cancelBubble = true;
}
// Dean's forEach: http://dean.edwards.name/base/forEach.js
/*
forEach, version 1.0
Copyright 2006, Dean Edwards
License: http://www.opensource.org/licenses/mit-license.php
*/
// array-like enumeration
if (!Array.forEach) { // mozilla already supports this
Array.forEach = function(array, block, context) {
for (var i = 0; i < array.length; i++) {
block.call(context, array[i], i, array);
}
};
}
// generic enumeration
Function.prototype.forEach = function(object, block, context) {
for (var key in object) {
if (typeof this.prototype[key] == "undefined") {
block.call(context, object[key], key, object);
}
}
};
// character enumeration
String.forEach = function(string, block, context) {
Array.forEach(string.split(""), function(chr, index) {
block.call(context, chr, index, string);
});
};
// globally resolve forEach enumeration
var forEach = function(object, block, context) {
if (object) {
var resolve = Object; // default
if (object instanceof Function) {
// functions have a "length" property
resolve = Function;
} else if (object.forEach instanceof Function) {
// the object implements a custom forEach method so use that
object.forEach(block, context);
return;
} else if (typeof object == "string") {
// the object is a string
resolve = String;
} else if (typeof object.length == "number") {
// the object is array-like
resolve = Array;
}
resolve.forEach(object, block, context);
}
};
}());
});

View File

@ -90,6 +90,12 @@ $(function() {
showActiveFile(); showActiveFile();
}); });
stopReplay = function() {
clearInterval(playInterval);
playInterval = undefined;
playButton.find('span.fa').removeClass('fa-pause').addClass('fa-play')
}
playButton.on('click', function(event) { playButton.on('click', function(event) {
if (playInterval == undefined) { if (playInterval == undefined) {
playInterval = setInterval(function() { playInterval = setInterval(function() {
@ -97,13 +103,12 @@ $(function() {
slider.val(parseInt(slider.val()) + 1); slider.val(parseInt(slider.val()) + 1);
slider.change() slider.change()
} else { } else {
clearInterval(playInterval); stopReplay();
} }
}, 5000); }, 5000);
playButton.find('span.fa').removeClass('fa-play').addClass('fa-pause') playButton.find('span.fa').removeClass('fa-play').addClass('fa-pause')
} else { } else {
clearInterval(playInterval); stopReplay();
playButton.find('span.fa').removeClass('fa-pause').addClass('fa-play')
} }
}); });

View File

@ -28,7 +28,76 @@ class ExecutionEnvironmentsController < ApplicationController
render(json: @docker_client.execute_arbitrary_command(params[:command])) render(json: @docker_client.execute_arbitrary_command(params[:command]))
end end
def working_time_query
"""
SELECT exercise_id, avg(working_time) as average_time, stddev_samp(extract('epoch' from working_time)) * interval '1 second' as stddev_time
FROM
(
SELECT user_id,
exercise_id,
sum(working_time_new) AS working_time
FROM
(SELECT user_id,
exercise_id,
CASE WHEN working_time >= '0:30:00' THEN '0' ELSE working_time END AS working_time_new
FROM
(SELECT user_id,
exercise_id,
id,
(created_at - lag(created_at) over (PARTITION BY user_id, exercise_id
ORDER BY created_at)) AS working_time
FROM submissions
WHERE exercise_id IN (SELECT ID FROM exercises WHERE execution_environment_id = #{@execution_environment.id})
GROUP BY exercise_id, user_id, id) AS foo) AS bar
GROUP BY user_id, exercise_id
) AS baz GROUP BY exercise_id;
"""
end
def user_query
"""
SELECT
id AS exercise_id,
COUNT(DISTINCT user_id) AS users,
AVG(score) AS average_score,
MAX(score) AS maximum_score,
stddev_samp(score) as stddev_score,
CASE
WHEN MAX(score)=0 THEN 0
ELSE 100 / MAX(score) * AVG(score)
END AS percent_correct,
SUM(submission_count) / COUNT(DISTINCT user_id) AS average_submission_count
FROM
(SELECT e.id,
s.user_id,
MAX(s.score) AS score,
COUNT(s.id) AS submission_count
FROM submissions s
JOIN exercises e ON e.id = s.exercise_id
WHERE e.execution_environment_id = #{@execution_environment.id}
GROUP BY e.id,
s.user_id) AS inner_query
GROUP BY id;
"""
end
def statistics def statistics
working_time_statistics = {}
user_statistics = {}
ActiveRecord::Base.connection.execute(working_time_query).each do |tuple|
working_time_statistics[tuple["exercise_id"].to_i] = tuple
end
ActiveRecord::Base.connection.execute(user_query).each do |tuple|
user_statistics[tuple["exercise_id"].to_i] = tuple
end
render locals: {
working_time_statistics: working_time_statistics,
user_statistics: user_statistics
}
end end
def execution_environment_params def execution_environment_params

View File

@ -155,7 +155,16 @@ class ExercisesController < ApplicationController
if(@external_user) if(@external_user)
render 'exercises/external_users/statistics' render 'exercises/external_users/statistics'
else else
render 'exercises/statistics' user_statistics = {}
query = "SELECT user_id, MAX(score) AS maximum_score, COUNT(id) AS runs
FROM submissions WHERE exercise_id = #{@exercise.id} GROUP BY
user_id;"
ActiveRecord::Base.connection.execute(query).each do |tuple|
user_statistics[tuple["user_id"].to_i] = tuple
end
render locals: {
user_statistics: user_statistics
}
end end
end end

View File

@ -14,9 +14,53 @@ class ExternalUsersController < ApplicationController
authorize! authorize!
end end
def working_time_query
"""
SELECT user_id,
exercise_id,
max(score) as maximum_score,
count(id) as runs,
sum(working_time_new) AS working_time
FROM
(SELECT user_id,
exercise_id,
score,
id,
CASE
WHEN working_time >= '0:30:00' THEN '0'
ELSE working_time
END AS working_time_new
FROM
(SELECT user_id,
exercise_id,
max(score) AS score,
id,
(created_at - lag(created_at) over (PARTITION BY user_id, exercise_id
ORDER BY created_at)) AS working_time
FROM submissions
WHERE user_id = #{@user.id}
AND user_type = 'ExternalUser'
GROUP BY exercise_id,
user_id,
id) AS foo) AS bar
GROUP BY user_id,
exercise_id;
"""
end
def statistics def statistics
@user = ExternalUser.find(params[:id]) @user = ExternalUser.find(params[:id])
authorize! authorize!
statistics = {}
ActiveRecord::Base.connection.execute(working_time_query).each do |tuple|
statistics[tuple["exercise_id"].to_i] = tuple
end
render locals: {
statistics: statistics
}
end end
end end

View File

@ -176,7 +176,24 @@ class SubmissionsController < ApplicationController
for part in message.split("\n") for part in message.split("\n")
self.parse_message(part,output_stream,socket,false) self.parse_message(part,output_stream,socket,false)
end end
elsif(message.include? "<img")
#Rails.logger.info('img foung')
@buffering = true
@buffer = ""
@buffer += message
#Rails.logger.info('Starting to buffer')
elsif(@buffering && (message.include? "/>"))
@buffer += message
parsed = {'cmd'=>'write','stream'=>output_stream,'data'=>@buffer}
socket.send_data JSON.dump(parsed)
#socket.send_data @buffer
@buffering = false
#Rails.logger.info('Sent complete buffer')
elsif(@buffering)
@buffer += message
#Rails.logger.info('Appending to buffer')
else else
#Rails.logger.info('else')
parsed = {'cmd'=>'write','stream'=>output_stream,'data'=>message} parsed = {'cmd'=>'write','stream'=>output_stream,'data'=>message}
socket.send_data JSON.dump(parsed) socket.send_data JSON.dump(parsed)
Rails.logger.info('parse_message sent: ' + JSON.dump(parsed)) Rails.logger.info('parse_message sent: ' + JSON.dump(parsed))

View File

@ -29,12 +29,12 @@ class Exercise < ActiveRecord::Base
def average_percentage def average_percentage
(average_score / maximum_score * 100).round if average_score (average_score / maximum_score * 100).round if average_score and maximum_score != 0.0 else 0
end end
def average_score def average_score
if submissions.exists?(cause: 'submit') if submissions.exists?(cause: 'submit')
maximum_scores_query = submissions.select('MAX(score) AS maximum_score').where(cause: 'submit').group(:user_id).to_sql.sub('$1', id.to_s) maximum_scores_query = submissions.select('MAX(score) AS maximum_score').group(:user_id).to_sql.sub('$1', id.to_s)
self.class.connection.execute("SELECT AVG(maximum_score) AS average_score FROM (#{maximum_scores_query}) AS maximum_scores").first['average_score'].to_f self.class.connection.execute("SELECT AVG(maximum_score) AS average_score FROM (#{maximum_scores_query}) AS maximum_scores").first['average_score'].to_f
else 0 end else 0 end
end end

View File

@ -1,5 +1,5 @@
class ExternalUserPolicy < AdminOnlyPolicy class ExternalUserPolicy < AdminOnlyPolicy
def statistics? def statistics?
admin? admin? || author? || team_member?
end end
end end

View File

@ -1,15 +1,25 @@
h1 = @execution_environment h1 = @execution_environment
.table-responsive .table-responsive
table.table table.table.table-striped.sortable
thead thead
tr tr
- ['.exercise', '.score', '.runs', '.worktime'].each do |title| - ['.exercise', '.users', '.score', '.maximum_score', '.stddev_score', '.percentage_correct', '.runs', '.worktime', '.stddev_worktime'].each do |title|
th.header = t(title) th.header = t(title)
tbody tbody
- @execution_environment.exercises.each do |exercise| - @execution_environment.exercises.each do |exercise|
- us = user_statistics[exercise.id]
- if not us then us = {"users" => 0, "average_score" => 0.0, "maximum_score" => 0, "stddev_score" => 0.0, "percent_correct" => nil, "average_submission_count" => 0}
- wts = working_time_statistics[exercise.id]
- if wts then average_time = wts["average_time"] else 0
- if wts then stddev_time = wts["stddev_time"] else 0
tr tr
td = link_to exercise.title, controller: "exercises", action: "statistics", id: exercise.id td = link_to exercise.title, controller: "exercises", action: "statistics", id: exercise.id
td = exercise.average_score td = us["users"]
td = exercise.average_number_of_submissions td = us["average_score"].to_f.round(4)
td = exercise.average_working_time td = us["maximum_score"].to_f.round(2)
td = us["stddev_score"].to_f.round(4)
td = (us["percent_correct"].to_f or 0).round(4)
td = us["average_submission_count"].to_f.round(2)
td = average_time
td = stddev_time

View File

@ -1,5 +1,5 @@
h1 = "#{@exercise} (external user #{@external_user})" h1 = "#{@exercise} (external user #{@external_user})"
- submissions = Submission.where("user_id = ? AND exercise_id = ?", @external_user.id, @exercise.id) - submissions = Submission.where("user_id = ? AND exercise_id = ?", @external_user.id, @exercise.id).order("created_at")
- current_submission = submissions.first - current_submission = submissions.first
- if current_submission - if current_submission
- initial_files = current_submission.files.to_a - initial_files = current_submission.files.to_a

View File

@ -1,9 +1,9 @@
h1 = @exercise h1 = @exercise
= row(label: '.participants', value: @exercise.users.count) = row(label: '.participants', value: @exercise.users.distinct.count)
- [:intermediate, :final].each do |scope| - [:intermediate, :final].each do |scope|
= row(label: ".#{scope}_submissions") do = row(label: ".#{scope}_submissions") do
= "#{@exercise.submissions.send(scope).count} (#{t('.users', count: @exercise.submissions.send(scope).distinct.count(:user_id, :user_type))})" = "#{@exercise.submissions.send(scope).count} (#{t('.users', count: @exercise.submissions.send(scope).distinct.count(:user_id))})"
= row(label: '.average_score') do = row(label: '.average_score') do
p == @exercise.average_score ? t('shared.out_of', maximum_value: @exercise.maximum_score, value: @exercise.average_score.round(2)) : empty p == @exercise.average_score ? t('shared.out_of', maximum_value: @exercise.maximum_score, value: @exercise.average_score.round(2)) : empty
p = progress_bar(@exercise.average_percentage) p = progress_bar(@exercise.average_percentage)
@ -14,16 +14,16 @@ h1 = @exercise
- Hash[:internal_users => t('.internal_users'), :external_users => t('.external_users')].each_pair do |symbol, label| - Hash[:internal_users => t('.internal_users'), :external_users => t('.external_users')].each_pair do |symbol, label|
strong = label strong = label
.table-responsive .table-responsive
table.table table.table.table-striped.sortable
thead thead
tr tr
- ['.user', '.score', '.runs', '.worktime'].each do |title| - ['.user', '.score', '.runs', '.worktime'].each do |title|
th.header = t(title) th.header = t(title)
tbody tbody
- @exercise.send(symbol).distinct().each do |user| - @exercise.send(symbol).distinct().each do |user|
- if user_statistics[user.id] then us = user_statistics[user.id] else us = {"maximum_score" => nil, "runs" => nil}
tr tr
- submissions = @exercise.submissions.where('user_id=?', user.id)
td = link_to_if symbol==:external_users, "#{user.name} (#{user.email})", {controller: "exercises", action: "statistics", external_user_id: user.id, id: @exercise.id} td = link_to_if symbol==:external_users, "#{user.name} (#{user.email})", {controller: "exercises", action: "statistics", external_user_id: user.id, id: @exercise.id}
td = submissions.maximum('score') or 0 td = us['maximum_score'] or 0
td = submissions.count('id') td = us['runs']
td = @exercise.average_working_time_for(user.id) or 0 td = @exercise.average_working_time_for(user.id) or 0

View File

@ -3,3 +3,6 @@ h1 = @user.name
= row(label: 'external_user.name', value: @user.name) = row(label: 'external_user.name', value: @user.name)
= row(label: 'external_user.email', value: @user.email) = row(label: 'external_user.email', value: @user.email)
= row(label: 'external_user.consumer', value: link_to(@user.consumer, @user.consumer)) = row(label: 'external_user.consumer', value: link_to(@user.consumer, @user.consumer))
br
= link_to(t('shared.statistics'), statistics_external_user_path(@user))

View File

@ -10,9 +10,9 @@ h1 = t('.title')
th.header = t(title) th.header = t(title)
tbody tbody
- exercises.each do |exercise| - exercises.each do |exercise|
- submissions = @user.submissions.where(:exercise_id => exercise.id, :cause => ['submit', 'run']) - if statistics[exercise.id] then stats = statistics[exercise.id] else stats = {"working_time" => 0, "runs" => 0, "score" => 0}
tr tr
td = link_to exercise, controller: "exercises", action: "statistics", external_user_id: @user.id, id: exercise.id td = link_to exercise, controller: "exercises", action: "statistics", external_user_id: @user.id, id: exercise.id
td = submissions.maximum(:score) or 0 td = stats["maximum_score"] or 0
td = submissions.count td = stats["runs"] or 0
td = exercise.average_working_time_for_only(@user.id) or 0 td = stats["working_time"] or 0

View File

@ -169,7 +169,12 @@ de:
headline: Shell headline: Shell
statistics: statistics:
exercise: Übung exercise: Übung
users: Anzahl (externer) Nutzer
score: Durchschnittliche Punktzahl score: Durchschnittliche Punktzahl
stddev_score: stdabw (Punktzahl)
stddev_worktime: stdabw (Arbeitszeit)
maximum_score: Maximale Punktzahl
percentage_correct: Prozent Korrekt
runs: Durchschnittliche Anzahl von Versuchen runs: Durchschnittliche Anzahl von Versuchen
worktime: Durchschnittliche Arbeitszeit worktime: Durchschnittliche Arbeitszeit
exercises: exercises:
@ -264,7 +269,7 @@ de:
title: Statistiken für Externe Benutzer title: Statistiken für Externe Benutzer
exercise: Übung exercise: Übung
score: Bewertung score: Bewertung
runs: Versuche runs: Abgaben
worktime: Arbeitszeit worktime: Arbeitszeit
files: files:
roles: roles:

View File

@ -169,7 +169,12 @@ en:
headline: Shell headline: Shell
statistics: statistics:
exercise: Exercise exercise: Exercise
users: (External) Users Count
score: Average Score score: Average Score
stddev_score: stddev (score)
stddev_worktime: stddev (working time)
maximum_score: Maximum Score
percentage_correct: Percentage Correct
runs: Average Number of Runs runs: Average Number of Runs
worktime: Average Working Time worktime: Average Working Time
exercises: exercises:
@ -264,7 +269,7 @@ en:
title: External User Statistics title: External User Statistics
exercise: Exercise exercise: Exercise
score: Score score: Score
runs: Runs runs: Submissions
worktime: Working Time worktime: Working Time
files: files:
roles: roles:

View File

@ -2,7 +2,7 @@ require 'concurrent'
require 'pathname' require 'pathname'
class DockerClient class DockerClient
CONTAINER_WORKSPACE_PATH = '/workspace' CONTAINER_WORKSPACE_PATH = '/workspace' #'/home/python/workspace' #'/tmp/workspace'
DEFAULT_MEMORY_LIMIT = 256 DEFAULT_MEMORY_LIMIT = 256
# Ralf: I suggest to replace this with the environment variable. Ask Hauke why this is not the case! # Ralf: I suggest to replace this with the environment variable. Ask Hauke why this is not the case!
LOCAL_WORKSPACE_ROOT = Rails.root.join('tmp', 'files', Rails.env) LOCAL_WORKSPACE_ROOT = Rails.root.join('tmp', 'files', Rails.env)
@ -21,6 +21,9 @@ class DockerClient
end end
def self.clean_container_workspace(container) def self.clean_container_workspace(container)
# remove files when using transferral via Docker API archive_in (transmit)
#container.exec(['bash', '-c', 'rm -rf ' + CONTAINER_WORKSPACE_PATH + '/*'])
local_workspace_path = local_workspace_path(container) local_workspace_path = local_workspace_path(container)
if local_workspace_path && Pathname.new(local_workspace_path).exist? if local_workspace_path && Pathname.new(local_workspace_path).exist?
Pathname.new(local_workspace_path).children.each{ |p| p.rmtree} Pathname.new(local_workspace_path).children.each{ |p| p.rmtree}
@ -42,6 +45,8 @@ class DockerClient
'Image' => find_image_by_tag(execution_environment.docker_image).info['RepoTags'].first, 'Image' => find_image_by_tag(execution_environment.docker_image).info['RepoTags'].first,
'Memory' => execution_environment.memory_limit.megabytes, 'Memory' => execution_environment.memory_limit.megabytes,
'NetworkDisabled' => !execution_environment.network_enabled?, 'NetworkDisabled' => !execution_environment.network_enabled?,
#'HostConfig' => { 'CpusetCpus' => '0', 'CpuQuota' => 10000 },
#DockerClient.config['allowed_cpus']
'OpenStdin' => true, 'OpenStdin' => true,
'StdinOnce' => true, 'StdinOnce' => true,
# required to expose standard streams over websocket # required to expose standard streams over websocket
@ -88,8 +93,8 @@ class DockerClient
def self.create_container(execution_environment) def self.create_container(execution_environment)
tries ||= 0 tries ||= 0
Rails.logger.info "docker_client: self.create_container with creation options:" #Rails.logger.info "docker_client: self.create_container with creation options:"
Rails.logger.info(container_creation_options(execution_environment)) #Rails.logger.info(container_creation_options(execution_environment))
container = Docker::Container.create(container_creation_options(execution_environment)) container = Docker::Container.create(container_creation_options(execution_environment))
local_workspace_path = generate_local_workspace_path local_workspace_path = generate_local_workspace_path
# container.start always creates the passed local_workspace_path on disk. Seems like we have to live with that, therefore we can also just create the empty folder ourselves. # container.start always creates the passed local_workspace_path on disk. Seems like we have to live with that, therefore we can also just create the empty folder ourselves.
@ -128,6 +133,54 @@ class DockerClient
end end
private :create_workspace_file private :create_workspace_file
def create_workspace_files_transmit(container, submission)
begin
# create a temporary dir, put all files in it, and put it into the container. the dir is automatically removed when leaving the block.
Dir.mktmpdir {|dir|
submission.collect_files.each do |file|
disk_file = File.new(dir + '/' + (file.path || '') + file.name_with_extension, 'w')
disk_file.write(file.content)
disk_file.close
end
begin
# create target folder, TODO re-active this when we remove shared folder bindings
#container.exec(['bash', '-c', 'mkdir ' + CONTAINER_WORKSPACE_PATH])
#container.exec(['bash', '-c', 'chown -R python ' + CONTAINER_WORKSPACE_PATH])
#container.exec(['bash', '-c', 'chgrp -G python ' + CONTAINER_WORKSPACE_PATH])
rescue StandardError => error
Rails.logger.error('create workspace folder: Rescued from StandardError: ' + error.to_s)
end
#sleep 1000
begin
# tar the files in dir and put the tar to CONTAINER_WORKSPACE_PATH in the container
container.archive_in(dir, CONTAINER_WORKSPACE_PATH, overwrite: false)
rescue StandardError => error
Rails.logger.error('insert tar: Rescued from StandardError: ' + error.to_s)
end
#Rails.logger.info('command: tar -xf ' + CONTAINER_WORKSPACE_PATH + '/' + dir.split('/tmp/')[1] + ' -C ' + CONTAINER_WORKSPACE_PATH)
begin
# untar the tar file placed in the CONTAINER_WORKSPACE_PATH
container.exec(['bash', '-c', 'tar -xf ' + CONTAINER_WORKSPACE_PATH + '/' + dir.split('/tmp/')[1] + ' -C ' + CONTAINER_WORKSPACE_PATH])
rescue StandardError => error
Rails.logger.error('untar: Rescued from StandardError: ' + error.to_s)
end
#sleep 1000
}
rescue StandardError => error
Rails.logger.error('create_workspace_files_transmit: Rescued from StandardError: ' + error.to_s)
end
end
def self.destroy_container(container) def self.destroy_container(container)
Rails.logger.info('destroying container ' + container.to_s) Rails.logger.info('destroying container ' + container.to_s)
container.stop.kill container.stop.kill
@ -191,8 +244,8 @@ class DockerClient
We need to start a second thread to kill the websocket connection, We need to start a second thread to kill the websocket connection,
as it is impossible to determine whether further input is requested. as it is impossible to determine whether further input is requested.
""" """
#begin
@thread = Thread.new do @thread = Thread.new do
#begin
timeout = @execution_environment.permitted_execution_time.to_i # seconds timeout = @execution_environment.permitted_execution_time.to_i # seconds
sleep(timeout) sleep(timeout)
if container.status != :returned if container.status != :returned
@ -203,12 +256,12 @@ class DockerClient
end end
kill_container(container) kill_container(container)
end end
end
#ensure #ensure
# guarantee that the thread is releasing the DB connection after it is done # guarantee that the thread is releasing the DB connection after it is done
# ActiveRecord::Base.connectionpool.releaseconnection # ActiveRecord::Base.connectionpool.releaseconnection
#end #end
end end
end
def exit_container(container) def exit_container(container)
Rails.logger.debug('exiting container ' + container.to_s) Rails.logger.debug('exiting container ' + container.to_s)

View File

@ -41,7 +41,7 @@ class DockerContainerPool
@all_containers[execution_environment.id]+=[container] @all_containers[execution_environment.id]+=[container]
if(!@containers[execution_environment.id].include?(container)) if(!@containers[execution_environment.id].include?(container))
@containers[execution_environment.id]+=[container] @containers[execution_environment.id]+=[container]
Rails.logger.debug('Added container ' + container.to_s + ' to all_pool for execution environment ' + execution_environment.to_s + '. Containers in all_pool: ' + @all_containers[execution_environment.id].size.to_s) #Rails.logger.debug('Added container ' + container.to_s + ' to all_pool for execution environment ' + execution_environment.to_s + '. Containers in all_pool: ' + @all_containers[execution_environment.id].size.to_s)
else else
Rails.logger.info('failed trying to add existing container ' + container.to_s + ' to execution_environment ' + execution_environment.to_s) Rails.logger.info('failed trying to add existing container ' + container.to_s + ' to execution_environment ' + execution_environment.to_s)
end end
@ -50,7 +50,7 @@ class DockerContainerPool
def self.create_container(execution_environment) def self.create_container(execution_environment)
container = DockerClient.create_container(execution_environment) container = DockerClient.create_container(execution_environment)
container.status = 'available' container.status = 'available'
Rails.logger.debug('created container ' + container.to_s + ' for execution environment ' + execution_environment.to_s) #Rails.logger.debug('created container ' + container.to_s + ' for execution environment ' + execution_environment.to_s)
container container
end end
@ -120,11 +120,11 @@ class DockerContainerPool
if refill_count > 0 if refill_count > 0
Rails.logger.info('Adding ' + refill_count.to_s + ' containers for execution_environment ' + execution_environment.name ) Rails.logger.info('Adding ' + refill_count.to_s + ' containers for execution_environment ' + execution_environment.name )
c = refill_count.times.map { create_container(execution_environment) } c = refill_count.times.map { create_container(execution_environment) }
Rails.logger.info('Created containers: ' + c.to_s ) #Rails.logger.info('Created containers: ' + c.to_s )
@containers[execution_environment.id] += c @containers[execution_environment.id] += c
@all_containers[execution_environment.id] += c @all_containers[execution_environment.id] += c
Rails.logger.debug('@containers for ' + execution_environment.name.to_s + ' (' + @containers.object_id.to_s + ') has the following content: '+ @containers[execution_environment.id].to_s) #Rails.logger.debug('@containers for ' + execution_environment.name.to_s + ' (' + @containers.object_id.to_s + ') has the following content: '+ @containers[execution_environment.id].to_s)
Rails.logger.debug('@all_containers for ' + execution_environment.name.to_s + ' (' + @all_containers.object_id.to_s + ') has the following content: ' + @all_containers[execution_environment.id].to_s) #Rails.logger.debug('@all_containers for ' + execution_environment.name.to_s + ' (' + @all_containers.object_id.to_s + ') has the following content: ' + @all_containers[execution_environment.id].to_s)
end end
end end

View File

@ -34,6 +34,7 @@ class Xikolo::Client
end end
def self.url def self.url
#todo: JanR: set an environment variable here, fallback value: http://open.hpi.de/api/
'http://localhost:2000/api/' 'http://localhost:2000/api/'
end end