Description:
fixed timing stat bug when used with single user mode
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r245:4658efe40e96 - - 2 files changed: 7 inserted, 1 deleted

@@ -1,40 +1,39
1 1 class LoginController < ApplicationController
2 2
3 3 def index
4 4 # show login screen
5 5 reset_session
6 6 redirect_to :controller => 'main', :action => 'login'
7 7 end
8 8
9 9 def login
10 10 if user = User.authenticate(params[:login], params[:password])
11 11 session[:user_id] = user.id
12 12 session[:admin] = user.admin?
13 - UserContestStat.update_user_start_time(user)
14 13 redirect_to :controller => 'main', :action => 'list'
15 14 else
16 15 flash[:notice] = 'Wrong password'
17 16 redirect_to :controller => 'main', :action => 'login'
18 17 end
19 18 end
20 19
21 20 def site_login
22 21 begin
23 22 site = Site.find(params[:login][:site_id])
24 23 rescue ActiveRecord::RecordNotFound
25 24 site = nil
26 25 end
27 26 if site==nil
28 27 flash[:notice] = 'Wrong site'
29 28 redirect_to :controller => 'main', :action => 'login' and return
30 29 end
31 30 if (site.password) and (site.password == params[:login][:password])
32 31 session[:site_id] = site.id
33 32 redirect_to :controller => 'site', :action => 'index'
34 33 else
35 34 flash[:notice] = 'Wrong site password'
36 35 redirect_to :controller => 'site', :action => 'login'
37 36 end
38 37 end
39 38
40 39 end
@@ -1,101 +1,103
1 1 class MainController < ApplicationController
2 2
3 3 before_filter :authenticate, :except => [:index, :login]
4 4 before_filter :check_viewability, :except => [:index, :login]
5 5
6 + append_before_filter :update_user_start_time, :except => [:index, :login]
7 +
6 8 # COMMENTED OUT: filter in each action instead
7 9 # before_filter :verify_time_limit, :only => [:submit]
8 10
9 11 verify :method => :post, :only => [:submit, :download_input, :submit_solution],
10 12 :redirect_to => { :action => :index }
11 13
12 14 # COMMENT OUT: only need when having high load
13 15 # caches_action :index, :login
14 16
15 17 # NOTE: This method is not actually needed, 'config/routes.rb' has
16 18 # assigned action login as a default action.
17 19 def index
18 20 redirect_to :action => 'login'
19 21 end
20 22
21 23 def login
22 24 saved_notice = flash[:notice]
23 25 reset_session
24 26 flash.now[:notice] = saved_notice
25 27
26 28 # EXPERIMENT:
27 29 # Hide login if in single user mode and the url does not
28 30 # explicitly specify /login
29 31 #
30 32 # logger.info "PATH: #{request.path}"
31 33 # if Configuration['system.single_user_mode'] and
32 34 # request.path!='/main/login'
33 35 # @hidelogin = true
34 36 # end
35 37
36 38 @announcements = Announcement.find_for_frontpage
37 39 render :action => 'login', :layout => 'empty'
38 40 end
39 41
40 42 def list
41 43 prepare_list_information
42 44 end
43 45
44 46 def help
45 47 @user = User.find(session[:user_id])
46 48 end
47 49
48 50 def submit
49 51 user = User.find(session[:user_id])
50 52
51 53 @submission = Submission.new(params[:submission])
52 54 @submission.user = user
53 55 @submission.language_id = 0
54 56 if (params['file']) and (params['file']!='')
55 57 @submission.source = params['file'].read
56 58 @submission.source_filename = params['file'].original_filename
57 59 end
58 60 @submission.submitted_at = Time.new.gmtime
59 61
60 62 if Configuration.time_limit_mode? and user.contest_finished?
61 63 @submission.errors.add_to_base "The contest is over."
62 64 prepare_list_information
63 65 render :action => 'list' and return
64 66 end
65 67
66 68 if @submission.valid?
67 69 if @submission.save == false
68 70 flash[:notice] = 'Error saving your submission'
69 71 elsif Task.create(:submission_id => @submission.id,
70 72 :status => Task::STATUS_INQUEUE) == false
71 73 flash[:notice] = 'Error adding your submission to task queue'
72 74 end
73 75 else
74 76 prepare_list_information
75 77 render :action => 'list' and return
76 78 end
77 79 redirect_to :action => 'list'
78 80 end
79 81
80 82 def source
81 83 submission = Submission.find(params[:id])
82 84 if submission.user_id == session[:user_id]
83 85 send_data(submission.source,
84 86 {:filename => submission.download_filename,
85 87 :type => 'text/plain'})
86 88 else
87 89 flash[:notice] = 'Error viewing source'
88 90 redirect_to :action => 'list'
89 91 end
90 92 end
91 93
92 94 def compiler_msg
93 95 @submission = Submission.find(params[:id])
94 96 if @submission.user_id == session[:user_id]
95 97 render :action => 'compiler_msg', :layout => 'empty'
96 98 else
97 99 flash[:notice] = 'Error viewing source'
98 100 redirect_to :action => 'list'
99 101 end
100 102 end
101 103
@@ -362,98 +364,103
362 364 trun_count = grading_info['testruns']
363 365 trun_count.times do |i|
364 366 @test_runs << [ read_grading_result(@user.login,
365 367 submission.problem.name,
366 368 submission.id,
367 369 i+1) ]
368 370 end
369 371 else
370 372 grading_info['testruns'].keys.sort.each do |num|
371 373 run = []
372 374 testrun = grading_info['testruns'][num]
373 375 testrun.each do |c|
374 376 run << read_grading_result(@user.login,
375 377 submission.problem.name,
376 378 submission.id,
377 379 c)
378 380 end
379 381 @test_runs << run
380 382 end
381 383 end
382 384 end
383 385
384 386 def grading_result_dir(user_name, problem_name, submission_id, case_num)
385 387 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
386 388 end
387 389
388 390 def output_filename(user_name, problem_name, submission_id, case_num)
389 391 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
390 392 return "#{dir}/output.txt"
391 393 end
392 394
393 395 def read_grading_result(user_name, problem_name, submission_id, case_num)
394 396 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
395 397 result_file_name = "#{dir}/result"
396 398 if !FileTest.exists?(result_file_name)
397 399 return {:num => case_num, :msg => 'program did not run'}
398 400 else
399 401 results = File.open(result_file_name).readlines
400 402 run_stat = extract_running_stat(results)
401 403 output_filename = "#{dir}/output.txt"
402 404 if FileTest.exists?(output_filename)
403 405 output_file = true
404 406 output_size = File.size(output_filename)
405 407 else
406 408 output_file = false
407 409 output_size = 0
408 410 end
409 411
410 412 return {
411 413 :num => case_num,
412 414 :msg => results[0],
413 415 :run_stat => run_stat,
414 416 :output => output_file,
415 417 :output_size => output_size
416 418 }
417 419 end
418 420 end
419 421
420 422 # copied from grader/script/lib/test_request_helper.rb
421 423 def extract_running_stat(results)
422 424 running_stat_line = results[-1]
423 425
424 426 # extract exit status line
425 427 run_stat = ""
426 428 if !(/[Cc]orrect/.match(results[0]))
427 429 run_stat = results[0].chomp
428 430 else
429 431 run_stat = 'Program exited normally'
430 432 end
431 433
432 434 logger.info "Stat line: #{running_stat_line}"
433 435
434 436 # extract running time
435 437 if res = /r(.*)u(.*)s/.match(running_stat_line)
436 438 seconds = (res[1].to_f + res[2].to_f)
437 439 time_stat = "Time used: #{seconds} sec."
438 440 else
439 441 seconds = nil
440 442 time_stat = "Time used: n/a sec."
441 443 end
442 444
443 445 # extract memory usage
444 446 if res = /s(.*)m/.match(running_stat_line)
445 447 memory_used = res[1].to_i
446 448 else
447 449 memory_used = -1
448 450 end
449 451
450 452 return {
451 453 :msg => "#{run_stat}\n#{time_stat}",
452 454 :running_time => seconds,
453 455 :exit_status => run_stat,
454 456 :memory_usage => memory_used
455 457 }
456 458 end
457 459
460 + def update_user_start_time
461 + user = User.find(session[:user_id])
462 + UserContestStat.update_user_start_time(user)
458 463 end
459 464
465 + end
466 +
You need to be logged in to leave comments. Login now