Description:
added timestamp to source download git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@365 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r168:1a0b47f4c2a4 - - 2 files changed: 10 inserted, 6 deleted

@@ -1,300 +1,295
1 1 class MainController < ApplicationController
2 2
3 3 SYSTEM_MODE_CONF_KEY = 'system.mode'
4 4
5 5 before_filter :authenticate, :except => [:index, :login]
6 6 before_filter :check_viewability, :except => [:index, :login]
7 7
8 8 # COMMENTED OUT: filter in each action instead
9 9 # before_filter :verify_time_limit, :only => [:submit]
10 10
11 11 verify :method => :post, :only => [:submit],
12 12 :redirect_to => { :action => :index }
13 13
14 14 # COMMENT OUT: only need when having high load
15 15 # caches_action :index, :login
16 16
17 17 # NOTE: This method is not actually needed, 'config/routes.rb' has
18 18 # assigned action login as a default action.
19 19 def index
20 20 redirect_to :action => 'login'
21 21 end
22 22
23 23 def login
24 24 saved_notice = flash[:notice]
25 25 reset_session
26 26 flash[:notice] = saved_notice
27 27
28 28 # EXPERIMENT:
29 29 # Hide login if in single user mode and the url does not
30 30 # explicitly specify /login
31 31 #
32 32 # logger.info "PATH: #{request.path}"
33 33 # if Configuration['system.single_user_mode'] and
34 34 # request.path!='/main/login'
35 35 # @hidelogin = true
36 36 # end
37 37
38 38 @announcements = Announcement.find_for_frontpage
39 39 render :action => 'login', :layout => 'empty'
40 40 end
41 41
42 42 def list
43 43 prepare_list_information
44 44 end
45 45
46 46 def help
47 47 @user = User.find(session[:user_id])
48 48 end
49 49
50 50 def submit
51 51 user = User.find(session[:user_id])
52 52
53 53 @submission = Submission.new(params[:submission])
54 54 @submission.user = user
55 55 @submission.language_id = 0
56 56 if params['file']!=''
57 57 @submission.source = params['file'].read
58 58 @submission.source_filename = params['file'].original_filename
59 59 end
60 60 @submission.submitted_at = Time.new.gmtime
61 61
62 62 if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and
63 63 user.site!=nil and user.site.finished?
64 64 @submission.errors.add_to_base "The contest is over."
65 65 prepare_list_information
66 66 render :action => 'list' and return
67 67 end
68 68
69 69 if @submission.valid?
70 70 if @submission.save == false
71 71 flash[:notice] = 'Error saving your submission'
72 72 elsif Task.create(:submission_id => @submission.id,
73 73 :status => Task::STATUS_INQUEUE) == false
74 74 flash[:notice] = 'Error adding your submission to task queue'
75 75 end
76 76 else
77 77 prepare_list_information
78 78 render :action => 'list' and return
79 79 end
80 80 redirect_to :action => 'list'
81 81 end
82 82
83 83 def source
84 84 submission = Submission.find(params[:id])
85 85 if submission.user_id == session[:user_id]
86 - if submission.problem.output_only
87 - fname = submission.source_filename
88 - else
89 - fname = submission.problem.name + '.' + submission.language.ext
90 - end
91 86 send_data(submission.source,
92 - {:filename => fname,
87 + {:filename => submission.download_filename,
93 88 :type => 'text/plain'})
94 89 else
95 90 flash[:notice] = 'Error viewing source'
96 91 redirect_to :action => 'list'
97 92 end
98 93 end
99 94
100 95 def compiler_msg
101 96 @submission = Submission.find(params[:id])
102 97 if @submission.user_id == session[:user_id]
103 98 render :action => 'compiler_msg', :layout => 'empty'
104 99 else
105 100 flash[:notice] = 'Error viewing source'
106 101 redirect_to :action => 'list'
107 102 end
108 103 end
109 104
110 105 def submission
111 106 @user = User.find(session[:user_id])
112 107 @problems = Problem.find_available_problems
113 108 if params[:id]==nil
114 109 @problem = nil
115 110 @submissions = nil
116 111 else
117 112 @problem = Problem.find_by_name(params[:id])
118 113 if not @problem.available
119 114 redirect_to :action => 'list'
120 115 flash[:notice] = 'Error: submissions for that problem are not viewable.'
121 116 return
122 117 end
123 118 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
124 119 end
125 120 end
126 121
127 122 def result
128 123 if !Configuration.show_grading_result
129 124 redirect_to :action => 'list' and return
130 125 end
131 126 @user = User.find(session[:user_id])
132 127 @submission = Submission.find(params[:id])
133 128 if @submission.user!=@user
134 129 flash[:notice] = 'You are not allowed to view result of other users.'
135 130 redirect_to :action => 'list' and return
136 131 end
137 132 prepare_grading_result(@submission)
138 133 end
139 134
140 135 def load_output
141 136 if !Configuration.show_grading_result or params[:num]==nil
142 137 redirect_to :action => 'list' and return
143 138 end
144 139 @user = User.find(session[:user_id])
145 140 @submission = Submission.find(params[:id])
146 141 if @submission.user!=@user
147 142 flash[:notice] = 'You are not allowed to view result of other users.'
148 143 redirect_to :action => 'list' and return
149 144 end
150 145 case_num = params[:num].to_i
151 146 out_filename = output_filename(@user.login,
152 147 @submission.problem.name,
153 148 @submission.id,
154 149 case_num)
155 150 if !FileTest.exists?(out_filename)
156 151 flash[:notice] = 'Output not found.'
157 152 redirect_to :action => 'list' and return
158 153 end
159 154
160 155 response.headers['Content-Type'] = "application/force-download"
161 156 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
162 157 response.headers["X-Sendfile"] = out_filename
163 158 response.headers['Content-length'] = File.size(out_filename)
164 159 render :nothing => true
165 160 end
166 161
167 162 def error
168 163 @user = User.find(session[:user_id])
169 164 end
170 165
171 166 protected
172 167 def prepare_list_information
173 168 @problems = Problem.find_available_problems
174 169 @prob_submissions = Array.new
175 170 @user = User.find(session[:user_id])
176 171 @problems.each do |p|
177 172 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
178 173 if sub!=nil
179 174 @prob_submissions << { :count => sub.number, :submission => sub }
180 175 else
181 176 @prob_submissions << { :count => 0, :submission => nil }
182 177 end
183 178 end
184 179 if Configuration.show_tasks_to?(@user)
185 180 @announcements = Announcement.find_published(true)
186 181 else
187 182 @announcements = Announcement.find_published
188 183 end
189 184 end
190 185
191 186 def check_viewability
192 187 @user = User.find(session[:user_id])
193 188 if (!Configuration.show_tasks_to?(@user)) and
194 189 ((action_name=='submission') or (action_name=='submit'))
195 190 redirect_to :action => 'list' and return
196 191 end
197 192 end
198 193
199 194 def prepare_grading_result(submission)
200 195 grading_info = Configuration.task_grading_info[submission.problem.name]
201 196 @test_runs = []
202 197 if grading_info['testruns'].is_a? Integer
203 198 trun_count = grading_info['testruns']
204 199 trun_count.times do |i|
205 200 @test_runs << [ read_grading_result(@user.login,
206 201 submission.problem.name,
207 202 submission.id,
208 203 i+1) ]
209 204 end
210 205 else
211 206 grading_info['testruns'].keys.sort.each do |num|
212 207 run = []
213 208 testrun = grading_info['testruns'][num]
214 209 testrun.each do |c|
215 210 run << read_grading_result(@user.login,
216 211 submission.problem.name,
217 212 submission.id,
218 213 c)
219 214 end
220 215 @test_runs << run
221 216 end
222 217 end
223 218 end
224 219
225 220 def grading_result_dir(user_name, problem_name, submission_id, case_num)
226 221 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
227 222 end
228 223
229 224 def output_filename(user_name, problem_name, submission_id, case_num)
230 225 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
231 226 return "#{dir}/output.txt"
232 227 end
233 228
234 229 def read_grading_result(user_name, problem_name, submission_id, case_num)
235 230 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
236 231 result_file_name = "#{dir}/result"
237 232 if !FileTest.exists?(result_file_name)
238 233 return {:num => case_num, :msg => 'program did not run'}
239 234 else
240 235 results = File.open(result_file_name).readlines
241 236 run_stat = extract_running_stat(results)
242 237 output_filename = "#{dir}/output.txt"
243 238 if FileTest.exists?(output_filename)
244 239 output_file = true
245 240 output_size = File.size(output_filename)
246 241 else
247 242 output_file = false
248 243 output_size = 0
249 244 end
250 245
251 246 return {
252 247 :num => case_num,
253 248 :msg => results[0],
254 249 :run_stat => run_stat,
255 250 :output => output_file,
256 251 :output_size => output_size
257 252 }
258 253 end
259 254 end
260 255
261 256 # copied from grader/script/lib/test_request_helper.rb
262 257 def extract_running_stat(results)
263 258 running_stat_line = results[-1]
264 259
265 260 # extract exit status line
266 261 run_stat = ""
267 262 if !(/[Cc]orrect/.match(results[0]))
268 263 run_stat = results[0].chomp
269 264 else
270 265 run_stat = 'Program exited normally'
271 266 end
272 267
273 268 logger.info "Stat line: #{running_stat_line}"
274 269
275 270 # extract running time
276 271 if res = /r(.*)u(.*)s/.match(running_stat_line)
277 272 seconds = (res[1].to_f + res[2].to_f)
278 273 time_stat = "Time used: #{seconds} sec."
279 274 else
280 275 seconds = nil
281 276 time_stat = "Time used: n/a sec."
282 277 end
283 278
284 279 # extract memory usage
285 280 if res = /s(.*)m/.match(running_stat_line)
286 281 memory_used = res[1].to_i
287 282 else
288 283 memory_used = -1
289 284 end
290 285
291 286 return {
292 287 :msg => "#{run_stat}\n#{time_stat}",
293 288 :running_time => seconds,
294 289 :exit_status => run_stat,
295 290 :memory_usage => memory_used
296 291 }
297 292 end
298 293
299 294 end
300 295
@@ -1,157 +1,166
1 1 class Submission < ActiveRecord::Base
2 2
3 3 belongs_to :language
4 4 belongs_to :problem
5 5 belongs_to :user
6 6
7 7 before_validation :assign_problem
8 8 before_validation :assign_language
9 9
10 10 validates_presence_of :source
11 11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
12 12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
13 13 validate :must_have_valid_problem
14 14 validate :must_specify_language
15 15
16 16 before_save :assign_latest_number_if_new_recond
17 17
18 18 def self.find_last_by_user_and_problem(user_id, problem_id)
19 19 last_sub = find(:first,
20 20 :conditions => {:user_id => user_id,
21 21 :problem_id => problem_id},
22 22 :order => 'number DESC')
23 23 return last_sub
24 24 end
25 25
26 26 def self.find_all_last_by_problem(problem_id)
27 27 # need to put in SQL command, maybe there's a better way
28 28 Submission.find_by_sql("SELECT * FROM submissions " +
29 29 "WHERE id = " +
30 30 "(SELECT MAX(id) FROM submissions AS subs " +
31 31 "WHERE subs.user_id = submissions.user_id AND " +
32 32 "problem_id = " + problem_id.to_s + " " +
33 33 "GROUP BY user_id) " +
34 34 "ORDER BY user_id")
35 35 end
36 36
37 37 def self.find_last_for_all_available_problems(user_id)
38 38 submissions = Array.new
39 39 problems = Problem.find_available_problems
40 40 problems.each do |problem|
41 41 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
42 42 submissions << sub if sub!=nil
43 43 end
44 44 submissions
45 45 end
46 46
47 47 def self.find_by_user_problem_number(user_id, problem_id, number)
48 48 Submission.find(:first,
49 49 :conditions => {
50 50 :user_id => user_id,
51 51 :problem_id => problem_id,
52 52 :number => number
53 53 })
54 54 end
55 55
56 56 def self.find_all_by_user_problem(user_id, problem_id)
57 57 Submission.find(:all,
58 58 :conditions => {
59 59 :user_id => user_id,
60 60 :problem_id => problem_id,
61 61 })
62 62 end
63 63
64 + def download_filename
65 + if self.problem.output_only
66 + return self.source_filename
67 + else
68 + timestamp = self.submitted_at.localtime.strftime("%H%M%S")
69 + return "#{self.problem.name}-#{timestamp}.#{self.language.ext}"
70 + end
71 + end
72 +
64 73 protected
65 74
66 75 def self.find_option_in_source(option, source)
67 76 if source==nil
68 77 return nil
69 78 end
70 79 i = 0
71 80 source.each_line do |s|
72 81 if s =~ option
73 82 words = s.split
74 83 return words[1]
75 84 end
76 85 i = i + 1
77 86 if i==10
78 87 return nil
79 88 end
80 89 end
81 90 return nil
82 91 end
83 92
84 93 def self.find_language_in_source(source, source_filename="")
85 94 langopt = find_option_in_source(/^LANG:/,source)
86 95 if langopt
87 96 return (Language.find_by_name(langopt) ||
88 97 Language.find_by_pretty_name(langopt))
89 98 else
90 99 if source_filename
91 100 return Language.find_by_extension(source_filename.split('.').last)
92 101 else
93 102 return nil
94 103 end
95 104 end
96 105 end
97 106
98 107 def self.find_problem_in_source(source, source_filename="")
99 108 prob_opt = find_option_in_source(/^TASK:/,source)
100 109 if problem = Problem.find_by_name(prob_opt)
101 110 return problem
102 111 else
103 112 if source_filename
104 113 return Problem.find_by_name(source_filename.split('.').first)
105 114 else
106 115 return nil
107 116 end
108 117 end
109 118 end
110 119
111 120 def assign_problem
112 121 if self.problem_id!=-1
113 122 begin
114 123 self.problem = Problem.find(self.problem_id)
115 124 rescue ActiveRecord::RecordNotFound
116 125 self.problem = nil
117 126 end
118 127 else
119 128 self.problem = Submission.find_problem_in_source(self.source,
120 129 self.source_filename)
121 130 end
122 131 end
123 132
124 133 def assign_language
125 134 self.language = Submission.find_language_in_source(self.source,
126 135 self.source_filename)
127 136 end
128 137
129 138 # validation codes
130 139 def must_specify_language
131 140 return if self.source==nil
132 141
133 142 # for output_only tasks
134 143 return if self.problem!=nil and self.problem.output_only
135 144
136 145 if self.language==nil
137 146 errors.add('source',"must specify programming language") unless self.language!=nil
138 147 end
139 148 end
140 149
141 150 def must_have_valid_problem
142 151 return if self.source==nil
143 152 if self.problem==nil
144 153 errors.add('problem',"must be specified.")
145 154 elsif (!self.problem.available) and (self.new_record?)
146 155 errors.add('problem',"must be valid.")
147 156 end
148 157 end
149 158
150 159 # callbacks
151 160 def assign_latest_number_if_new_recond
152 161 return if !self.new_record?
153 162 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
154 163 self.number = (latest==nil) ? 1 : latest.number + 1;
155 164 end
156 165
157 166 end
You need to be logged in to leave comments. Login now