From 0f5fe51f97b4137e678961087ca9b901d7b4c8f9 Mon Sep 17 00:00:00 2001 From: yuanke <249218296@qq.com> Date: Wed, 20 Apr 2016 09:51:51 +0800 Subject: [PATCH] =?UTF-8?q?1.=E7=AE=A1=E7=90=86=E5=91=98=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B5=8B=E8=AF=95=E5=88=97=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=202.=E4=BB=A3=E7=A0=81=E6=9F=A5=E9=87=8D?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E4=BD=8D=E7=BD=AE=203.=E6=9F=A5=E9=87=8D?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin_controller.rb | 26 ++- app/controllers/student_work_controller.rb | 37 ++++- app/models/code_tests.rb | 3 + app/models/student_work_test.rb | 2 +- app/views/admin/code_work_tests.html.erb | 71 ++++---- app/views/courses/code_repeat.html.erb | 4 +- .../student_work/_student_work_list.html.erb | 155 +----------------- app/views/student_work/work_canrepeat.js.erb | 2 +- .../20160419061745_create_code_tests.rb | 13 ++ ...74016_add_student_work_id_to_code_tests.rb | 5 + db/schema.rb | 16 +- public/stylesheets/application.css | 27 +-- public/stylesheets/nyan.css | 25 +++ spec/factories/code_tests.rb | 10 ++ spec/models/code_tests_spec.rb | 5 + 15 files changed, 174 insertions(+), 227 deletions(-) create mode 100644 app/models/code_tests.rb create mode 100644 db/migrate/20160419061745_create_code_tests.rb create mode 100644 db/migrate/20160419074016_add_student_work_id_to_code_tests.rb create mode 100644 spec/factories/code_tests.rb create mode 100644 spec/models/code_tests_spec.rb diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index f663bc272..1a55c947b 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -586,11 +586,31 @@ class AdminController < ApplicationController #代码测试列表 def code_work_tests - @code_work_tests = StudentWorkTest.find_by_sql("select status,results,created_at, student_work_id from student_work_tests order by id desc ") - #@code_work_tests = StudentWorkTest.find_by_sql("select a.status,a.results,a.created_at ,b.id as homeworkid,d.language from student_work_tests as a , homework_commons as b ,student_works as c, homework_detail_programings as d where a.student_work_id = c.id and b.id = c.homework_common_id and c.homework_common_id = d.homework_common_id order by a.id desc ") - #@code_work_tests = StudentWorkTest.order('created_at desc') + #求出所有条数 + tCount = StudentWorkTest.find_by_sql("select count(*) from code_tests").first['count(*)'] + + #设置个空的数组 以便paginateHelper来分页 + @code_work_tests = [] + @code_work_tests[tCount-1] = {} + @code_work_tests = paginateHelper @code_work_tests,30 @page = (params['page'] || 1).to_i - 1 + + #取出需要的那一页数据 + tStart = @page*30 + @code_work_tests = CodeTests.find_by_sql("select * from code_tests order by id desc limit #{tStart},30 ") + + #取出各个作业是否是模拟答题的 + is_test = {} + @code_work_tests.each do |test| + if is_test[test['student_work_id']] != nil + test['is_test'] = is_test[test['student_work_id']] + else + test['is_test'] = CodeTests.find_by_sql("select is_test from student_works where id = #{test['student_work_id']}").first['is_test'] + is_test[test['student_work_id']] = test['is_test'] + end + end + respond_to do |format| format.html end diff --git a/app/controllers/student_work_controller.rb b/app/controllers/student_work_controller.rb index 89ee77053..43fe10bfe 100644 --- a/app/controllers/student_work_controller.rb +++ b/app/controllers/student_work_controller.rb @@ -100,8 +100,17 @@ class StudentWorkController < ApplicationController test = @homework.homework_tests[index - 1] #请求测试 - result = test_realtime_ex(test, params[:src]) + begin + result = test_realtime_ex(test, params[:src]) + rescue Timeout::Error + tEndtime = Time.now + tUsedtime = (tEndtime.to_i-tStarttime.to_i)*1000+(tEndtime.usec - tStarttime.usec)/1000 + logger.debug "program_test_ex user wait time = #{tUsedtime} 毫秒" + #status 0:答案正确 -3http超时 -2:编译错误 -1:答案错误 2:程序运行超时 + CodeTests.create(:homework_id=>@homework.id,:language=>@homework.homework_detail_programing.language,:status=>-3,:wait_time=>tUsedtime,:student_work_id=>student_work.id) + + end if result["status"].to_i != -2 #result["results"].first['output'] = result["results"].first['output'].gsub(" ","□") #result["results"].first['result'] = result["results"].first['result'].gsub(" ","□") @@ -116,11 +125,19 @@ class StudentWorkController < ApplicationController resultObj[:results] = result["results"].first #本次测试结果 resultObj[:error_msg] = result["error_msg"] #编译错误时的信息 + #该状态用于存入CodeTests + tmpstatus = -1 if result["status"].to_i == -2 #编译错误 resultObj[:results] = result["error_msg"] resultObj[:status] = -2 + tmpstatus = -2 elsif result["results"][0]["status"].to_i == 2 resultObj[:status] = 2 + tmpstatus = 2 + end + + if result["status"] == 0 + tmpstatus = 0 end unless student_work.save @@ -136,9 +153,6 @@ class StudentWorkController < ApplicationController end #每次从数据库取出上次的结果加上本次的结果再存入数据库 - tEndtime = Time.now - tUsedtime = (tEndtime.to_i-tStarttime.to_i)*1000+(tEndtime.usec - tStarttime.usec)/1000 - if result["status"].to_i != -2 result["results"].first['user_wait'] = tUsedtime @@ -175,11 +189,19 @@ class StudentWorkController < ApplicationController resultObj[:index] = student_work.student_work_tests.count end + #将每次用户等待时间都存起来以便管理界面显示用 tEndtime = Time.now tUsedtime = (tEndtime.to_i-tStarttime.to_i)*1000+(tEndtime.usec - tStarttime.usec)/1000 - logger.debug "program_test_ex user wait time = #{tUsedtime} 毫秒" + time_used = 0 + if result["status"].to_i != -2 + #至少一毫秒 + time_used = result["results"].first['time_used'] == 0 ? 1:result["results"].first['time_used'] + end + #0:答案正确 -3http超时 -2:编译错误 -1:答案错误 2:程序运行超时 + CodeTests.create(:homework_id=>@homework.id,:language=>@homework.homework_detail_programing.language,:status=>tmpstatus,:time_used=>time_used,:wait_time=>tUsedtime,:student_work_id=>student_work.id) + #渲染返回结果 render :json => resultObj end @@ -223,6 +245,7 @@ class StudentWorkController < ApplicationController JSON.parse(res.body) end + #点击代码查重按钮 def work_canrepeat @homework_id = params[:homework] @course_id = params[:course_id] @@ -280,13 +303,14 @@ class StudentWorkController < ApplicationController render :json => resultObj end + #上次代码查重时间 def last_codecomparetime resultObj = {status: 0} @homework = HomeworkCommon.find params[:homework] #转换一下 if @homework.simi_time != nil - resultObj[:comparetime] = Time.parse(@homework.simi_time.to_s).strftime("%Y-%m-%d %H:%M") + resultObj[:comparetime] = Time.parse(@homework.simi_time.to_s).strftime("%Y-%m-%d %H:%M:%S") else resultObj[:comparetime] = 0 end @@ -1277,7 +1301,6 @@ class StudentWorkController < ApplicationController request["Content-Type"] = "application/json" client.request(request) end - JSON.parse(res.body) end diff --git a/app/models/code_tests.rb b/app/models/code_tests.rb new file mode 100644 index 000000000..f5a358b07 --- /dev/null +++ b/app/models/code_tests.rb @@ -0,0 +1,3 @@ +class CodeTests < ActiveRecord::Base + attr_accessible :homework_id, :language, :status, :time_used, :wait_time, :student_work_id +end diff --git a/app/models/student_work_test.rb b/app/models/student_work_test.rb index 0246ecabf..54d5ab07a 100644 --- a/app/models/student_work_test.rb +++ b/app/models/student_work_test.rb @@ -1,6 +1,6 @@ # encoding: utf-8 class StudentWorkTest < ActiveRecord::Base - attr_accessible :student_work_id, :results, :status, :src + attr_accessible :student_work_id, :results, :status, :src, :uwait_time belongs_to :student_work serialize :results, Array diff --git a/app/views/admin/code_work_tests.html.erb b/app/views/admin/code_work_tests.html.erb index 29fb588c2..385f97169 100644 --- a/app/views/admin/code_work_tests.html.erb +++ b/app/views/admin/code_work_tests.html.erb @@ -10,51 +10,51 @@ - 作业id + 作业id + + + 作品id - 平均等待时间 + 用户等待时间 - 语言 + 语言 - - 提交测试时间 + + 测试完成时间 + + + 答题状态 - 答题状态 - - - 测试集数 - - - 最小耗时 - - - 最大耗时 + 耗时 <% @code_work_tests.each do |test| %> - <% infos = StudentWorkTest.find_by_sql("select a.homework_common_id as homeworkid,b.language from student_works as a, homework_detail_programings as b where a.id = #{test.student_work_id} and a.homework_common_id = b.homework_common_id - ").first %> - <% if infos != nil %> + <% if test['homework_id'] != nil %> "> - - <%=link_to(infos.homeworkid, student_work_index_path(:homework => infos.homeworkid))%> + '> + <%=link_to(test['homework_id'], student_work_index_path(:homework => test['homework_id']))%> + + '> + <% if test['is_test'] == 0 %> + <%=link_to(test['student_work_id'], student_work_index_path(:homework => test['homework_id'],:student_work_id=>test['student_work_id']))%> + <% else %> + <%=link_to(test['student_work_id'], new_user_commit_homework_users_path(homework_id: test['homework_id'], is_test: true))%> + <% end %> - <% if test.status != -2 && test.results.first['user_wait'] %> - <% wait_time = 0 %> - <% test.results.each do |result| wait_time = wait_time + result['user_wait'] end %> - <%=(wait_time/test.results.count).to_s+"毫秒" %> + <% if test.wait_time != 0 %> + <%=test.wait_time.to_s+"毫秒" %> <% else %> <%="未记录"%> <% end %> - <%=%W(C C++ Python Java).at(infos.language.to_i - 1)%> + <%=%W(C C++ Python Java).at(test['language'].to_i - 1)%> <%=Time.parse(test.created_at.to_s).strftime("%Y-%m-%d %H:%M:%S")%> @@ -64,26 +64,17 @@ <%= "答题正确" %> <% elsif test.status == -2 %> <%= "编译错误" %> - <% elsif test.status == 2 || test.results.last['status'] == 2 %> - <%= "超时" %> + <% elsif test.status == 2 %> + <%= "代码超时" %> + <% elsif test.status == -3 %> + <%= "请求超时" %> <% else %> <%= "答题错误" %> <% end %> - <% if test.status != -2 %> - <%=test.results.count%> - <% end %> - - - <% if test.status != -2 %> - <%test.results = test.results.sort_by {|result| result['time_used'] }%> - <%=test.results.first['time_used'] == 0 ? "1毫秒":test.results.first['time_used'].to_s+"毫秒"%> - <% end %> - - - <% if test.status != -2 %> - <%=test.results.last['time_used'] == 0 ? "1毫秒":test.results.last['time_used'].to_s+"毫秒"%> + <% if test.time_used > 0 %> + <%=test.time_used.to_s+"毫秒"%> <% end %> diff --git a/app/views/courses/code_repeat.html.erb b/app/views/courses/code_repeat.html.erb index 0748dc30f..87cf539fa 100644 --- a/app/views/courses/code_repeat.html.erb +++ b/app/views/courses/code_repeat.html.erb @@ -2,8 +2,8 @@

查重结果