diff --git a/app/controllers/student_work_controller.rb b/app/controllers/student_work_controller.rb index 89f67ddf4..e40475d51 100644 --- a/app/controllers/student_work_controller.rb +++ b/app/controllers/student_work_controller.rb @@ -55,12 +55,11 @@ class StudentWorkController < ApplicationController render :json => resultObj end - $test_result = {} - $test_status = {} +#由于负载问题 不要使用全局变量 #根据传入的tIndex确定是第几次测试 def program_test_ex is_test = params[:is_test] == 'true' - resultObj = {status: 0, results: [], error_msg: '', time: Time.now.strftime('%Y-%m-%d %T'),tseq:1,tcount:1} #保存每测试一次返回的结果 + resultObj = {status: 0, results: [], error_msg: '', time: Time.now.strftime('%Y-%m-%d %T'),tseq:1,tcount:1,testid:1} #保存每测试一次返回的结果 student_work = find_or_save_student_work(is_test) @@ -81,7 +80,7 @@ class StudentWorkController < ApplicationController #-1 默认值 0全部正确并结束 2 超时 -2 编译错误 resultObj[:status] = -1 - resultObj[:results] = result["results"][0] #本次测试结果 + resultObj[:results] = result["results"].first #本次测试结果 resultObj[:error_msg] = result["error_msg"] #编译错误时的信息 if result["status"].to_i == -2 #编译错误 @@ -94,24 +93,6 @@ class StudentWorkController < ApplicationController resultObj[:status] = 200 else - #索引 - work_id = student_work.id - - #测试第一个时初始化下全局变量 - if index == 1 - $test_result[work_id] = [] #保存本次测试的结果 输入输出 - $test_status[work_id] = 0 #保存本次测试的结果 正确还是错误 - end - - if result["status"].to_i == -2 - $test_result[work_id] = [result["error_msg"]] - $test_status[work_id] = -2 - else - #存下每次的结果 只有每次都为0才全部正确 - $test_status[work_id] = result["status"] != 0 ? result["status"]:$test_status[work_id] - $test_result[work_id][index - 1] = resultObj[:results] - end - student_work.name = params[:title] student_work.description = params[:src] @@ -121,20 +102,31 @@ class StudentWorkController < ApplicationController student_work.late_penalty = 0 end + #每次都要把数据存到数据库中 + status = resultObj[:status] + if index == 1 + student_work_test = student_work.student_work_tests.build(status: resultObj[:status], + results: [resultObj[:results]],src: params[:src]) + student_work_test.save! + resultObj[:testid] = student_work_test.id + else + student_work_test = StudentWorkTest.find(params[:testid]) + results = student_work_test.results + results << resultObj[:results] + student_work_test.results = results + student_work_test.status = (result["status"] != 0 ? result["status"] : student_work_test.status) + student_work_test.save! + status = student_work_test.status + resultObj[:testid] = student_work_test.id + end + #超时或编译错误则直接返回了并存入数据库 if resultObj[:status] == 2 || resultObj[:status] == -2 || index == @homework.homework_tests.size - if $test_status[work_id] == 0 + if status == 0 resultObj[:status] = 0 end - - student_work_test = student_work.student_work_tests.build(status: $test_status[work_id], - results: $test_result[work_id],src: params[:src]) - student_work.save resultObj[:time] = student_work_test.created_at.to_s(:db) resultObj[:index] = student_work.student_work_tests.count - - $test_result[work_id] = nil - $test_status[work_id] = nil end #渲染返回结果 diff --git a/public/javascripts/homework.js b/public/javascripts/homework.js index 3bca1606c..5b7f1b046 100644 --- a/public/javascripts/homework.js +++ b/public/javascripts/homework.js @@ -57,10 +57,12 @@ $(function(){ ); */ //先测试一次并返回测试集个数及结果再判断是否需要继续进行测试 - var test_post = function(i){ + var test_post = function(i, testid){ $.post( '/student_work/program_test_ex', - {homework: homework_id, student_work_id: student_work_id, src: src, title: title, is_test: is_test,tIndex:i}, + {homework: homework_id, student_work_id: student_work_id, + src: src, title: title, is_test: is_test,tIndex:i, + testid: testid}, function(data,status){ var tSeq = data.tseq; var tCount = data.tcount; @@ -90,7 +92,7 @@ $(function(){ return; } - test_post(i+1); + test_post(i+1, data.testid); } ).fail(function(xhr, status){ if(status == 'timeout'){ @@ -100,9 +102,10 @@ $(function(){ } return; }); + }; - test_post(1); + test_post(1, 0); }; $('#test-program-btn').on('click', test_program);