]>
Commit | Line | Data |
---|---|---|
1 | package Gruntmaster::Data; | |
2 | use 5.014; | |
3 | use warnings; | |
4 | ||
5 | use parent qw/Exporter/; | |
6 | our $VERSION = '5999.000_013'; | |
7 | our @EXPORT = qw/dbinit purge db user_list user_entry problem_list problem_entry contest_list contest_entry contest_has_problem job_list job_entry create_job standings update_status rerun_job take_job finish_job/; ## no critic (ProhibitAutomaticExportation) | |
8 | ||
9 | use JSON::MaybeXS qw/decode_json/; | |
10 | use HTTP::Tiny; | |
11 | use PerlX::Maybe qw/maybe/; | |
12 | ||
13 | use DBI; | |
14 | use DBIx::Simple; | |
15 | use List::Util qw/sum/; | |
16 | use SQL::Abstract; | |
17 | ||
18 | use constant PROBLEM_PUBLIC_COLUMNS => [qw/id author writer level name owner private timeout olimit value/]; | |
19 | use constant JOBS_PER_PAGE => 50; | |
20 | ||
21 | my %statements = ( | |
22 | user_list_sth => 'SELECT * FROM user_list LIMIT 200', | |
23 | user_entry_sth => 'SELECT * FROM user_data WHERE id = ?', | |
24 | ||
25 | problem_status_sth => 'SELECT problem,solved FROM problem_status WHERE owner = ?', | |
26 | contest_status_sth => 'SELECT contest,score,rank FROM contest_status WHERE owner = ?', | |
27 | ||
28 | contest_list_sth => 'SELECT * FROM contest_entry', | |
29 | contest_entry_sth => 'SELECT * FROM contest_entry WHERE id = ?', | |
30 | contest_has_problem_sth => 'SELECT EXISTS(SELECT 1 FROM contest_problems WHERE contest = ? AND problem = ?)', | |
31 | opens_sth => 'SELECT problem,owner,time FROM opens WHERE contest = ?', | |
32 | ||
33 | problem_entry_sth => 'SELECT ' . (join ',', @{PROBLEM_PUBLIC_COLUMNS()}, 'statement', 'solution') . ' FROM problems WHERE id = ?', | |
34 | limits_sth => 'SELECT format,timeout FROM limits WHERE problem = ?', | |
35 | ||
36 | job_entry_sth => 'SELECT * FROM job_entry WHERE id = ?', | |
37 | ||
38 | rerun_job_sth => 'UPDATE jobs SET daemon=NULL,result=-2,result_text=NULL,results=NULL,errors=NULL WHERE id = ?', | |
39 | take_job_sth => 'UPDATE jobs SET daemon=? WHERE id = (SELECT id FROM jobs WHERE daemon IS NULL LIMIT 1 FOR UPDATE) RETURNING id', | |
40 | ); | |
41 | ||
42 | our $db; | |
43 | sub db () { $db } | |
44 | ||
45 | sub dbinit { | |
46 | $db = DBIx::Simple->new(@_); | |
47 | $db->keep_statements = 100; | |
48 | }; | |
49 | ||
50 | sub purge; | |
51 | ||
52 | sub query { | |
53 | my ($stat, @extra) = @_; | |
54 | $db->query($statements{$stat}, @extra) | |
55 | } | |
56 | ||
57 | my (%name_cache, %name_cache_time); | |
58 | use constant NAME_CACHE_MAX_AGE => 5; | |
59 | ||
60 | sub object_name { | |
61 | my ($table, $id) = @_; | |
62 | $name_cache_time{$table} //= 0; | |
63 | if (time - $name_cache_time{$table} > NAME_CACHE_MAX_AGE) { | |
64 | $name_cache_time{$table} = time; | |
65 | $name_cache{$table} = {}; | |
66 | $name_cache{$table} = $db->select($table, 'id,name')->map; | |
67 | } | |
68 | ||
69 | $name_cache{$table}{$id} | |
70 | } | |
71 | ||
72 | ||
73 | sub add_names ($) { ## no critic (ProhibitSubroutinePrototypes) | |
74 | my ($el) = @_; | |
75 | return unless defined $el; | |
76 | if (ref $el eq 'ARRAY') { | |
77 | &add_names ($_) for @$el ## no critic (ProhibitAmpersandSigils) | |
78 | } else { | |
79 | for my $object (qw/contest owner problem/) { | |
80 | my $table = $object eq 'owner' ? 'users' : "${object}s"; | |
81 | $el->{"${object}_name"} = object_name $table, $el->{$object} if defined $el->{$object} | |
82 | } | |
83 | } | |
84 | ||
85 | $el | |
86 | } | |
87 | ||
88 | sub user_list { scalar query('user_list_sth')->hashes } | |
89 | ||
90 | sub user_entry { | |
91 | my ($id) = @_; | |
92 | my $ret = query('user_entry_sth', $id)->hash; | |
93 | $ret->{problems} = add_names query('problem_status_sth', $id)->hashes; | |
94 | $ret->{contests} = add_names query('contest_status_sth', $id)->hashes; | |
95 | ||
96 | $ret; | |
97 | } | |
98 | ||
99 | sub problem_list { | |
100 | my (%args) = @_; | |
101 | my @columns = @{PROBLEM_PUBLIC_COLUMNS()}; | |
102 | push @columns, 'solution' if $args{solution}; | |
103 | my %where; | |
104 | $where{private} = 0 unless $args{contest} || $args{private}; | |
105 | $where{'cp.contest'} = $args{contest} if $args{contest}; | |
106 | $where{owner} = $args{owner} if $args{owner}; | |
107 | ||
108 | my $table = $args{contest} ? 'problems JOIN contest_problems cp ON cp.problem = id' : 'problems'; | |
109 | add_names $db->select(\$table, \@columns, \%where, 'name')->hashes | |
110 | } | |
111 | ||
112 | sub problem_entry { | |
113 | my ($id, $contest) = @_; | |
114 | $contest = contest_entry ($contest) if $contest; | |
115 | my $ret = add_names query(problem_entry_sth => $id)->hash; | |
116 | my $limits = query(limits_sth => $id)->hashes; | |
117 | $ret->{limits} = $limits if @$limits; | |
118 | ||
119 | if ($contest) { | |
120 | $ret->{contest_start} = $contest->{start}; | |
121 | $ret->{contest_stop} = $contest->{stop}; | |
122 | delete $ret->{solution} | |
123 | } | |
124 | ||
125 | $ret | |
126 | } | |
127 | ||
128 | sub contest_list { add_names query('contest_list_sth')->hashes } | |
129 | ||
130 | sub contest_entry { add_names query(contest_entry_sth => $_[0])->hash } | |
131 | ||
132 | sub contest_has_problem { query('contest_has_problem_sth', @_[0, 1])->flat } | |
133 | ||
134 | sub job_list { | |
135 | my (%args) = @_; | |
136 | $args{page} = int ($args{page} // 1); | |
137 | my %where = ( | |
138 | maybe contest => $args{contest}, | |
139 | maybe owner => $args{owner}, | |
140 | maybe problem => $args{problem}, | |
141 | maybe result => $args{result}, | |
142 | ); | |
143 | $where{private} = 0 unless $args{private}; | |
144 | ||
145 | my $rows = $db->select('job_entry', 'COUNT(*)', \%where)->list; | |
146 | my $pages = int (($rows + JOBS_PER_PAGE - 1) / JOBS_PER_PAGE); | |
147 | my ($stmt, @bind) = $db->abstract->select('job_entry', '*', \%where, {-desc => 'id'}); | |
148 | my $jobs = add_names $db->query("$stmt LIMIT " . JOBS_PER_PAGE . ' OFFSET ' . ($args{page} - 1) * JOBS_PER_PAGE, @bind)->hashes; | |
149 | my $pageinfo = { | |
150 | current_page => $args{page}, | |
151 | last_page => $pages, | |
152 | ($args{page} - 1) ? (previous_page => $args{page} - 1) : (), | |
153 | ($args{page} < $pages) ? (next_page => $args{page} + 1) : (), | |
154 | }; | |
155 | wantarray ? ($jobs, $pageinfo) : $jobs; | |
156 | } | |
157 | ||
158 | sub job_entry { | |
159 | my $ret = add_names query(job_entry_sth => $_[0])->hash; | |
160 | $ret->{results} = decode_json $ret->{results} if $ret->{results}; | |
161 | $ret | |
162 | } | |
163 | ||
164 | sub create_job { | |
165 | my (%args) = @_; | |
166 | $db->update('users', {lastjob => time}, {id => $args{owner}}); | |
167 | purge '/log/'; | |
168 | scalar $db->insert('jobs', \%args, {returning => 'id'})->list | |
169 | } | |
170 | ||
171 | sub _calc_score { | |
172 | my ($mxscore, $time, $tries, $totaltime) = @_; | |
173 | my $score = $mxscore; | |
174 | $time = 300 if $time > $totaltime; # uncoverable branch true does not happen anymore (only possible if opens are broken) | |
175 | $score = ($totaltime - $time) / $totaltime * $score; | |
176 | $score -= $tries / 10 * $mxscore; | |
177 | $score = $mxscore * 3 / 10 if $score < $mxscore * 3 / 10; | |
178 | int $score + 0.5 | |
179 | } | |
180 | ||
181 | sub standings { | |
182 | my ($ct) = @_; | |
183 | my @problems = sort { $a->{value} <=> $b->{value} } @{problem_list contest => $ct}; | |
184 | my %values = map { $_->{id} => $_->{value} } @problems; | |
185 | $ct = contest_entry $ct; | |
186 | ||
187 | my (%scores, %tries, %opens); | |
188 | my $opens = query(opens_sth => $ct->{id}); | |
189 | while ($opens->into(my ($problem, $owner, $time))) { | |
190 | $opens{$problem, $owner} = $time; | |
191 | } | |
192 | ||
193 | my $jobs = $db->select('job_entry', '*', {contest => $ct->{id}}, 'id'); | |
194 | ||
195 | while (my $job = $jobs->hash) { | |
196 | my $open = $opens{$job->{problem}, $job->{owner}} // $ct->{start}; | |
197 | my $time = $job->{date} - $open; | |
198 | next if $time < 0; # uncoverable branch true job sent before contest is deprecated | |
199 | my $value = $values{$job->{problem}}; | |
200 | my $factor = $job->{result} ? 0 : 1; | |
201 | $factor = $1 / 100 if $job->{result_text} =~ /^(\d+ )/s; | |
202 | $scores{$job->{owner}}{$job->{problem}} = int ($factor * _calc_score ($value, $time, $tries{$job->{owner}}{$job->{problem}}++, $ct->{stop} - $ct->{start})); | |
203 | } | |
204 | ||
205 | my @st = sort { $b->{score} <=> $a->{score} or $a->{user} cmp $b->{user} } map { ## no critic (ProhibitReverseSortBlock) | |
206 | my $user = $_; | |
207 | +{ | |
208 | user => $user, | |
209 | user_name => object_name(users => $user), | |
210 | score => sum (values %{$scores{$user}}), | |
211 | scores => [map { $scores{$user}{$_->{id}} // '-'} @problems], | |
212 | } | |
213 | } keys %scores; | |
214 | ||
215 | $st[0]->{rank} = 1 if @st; | |
216 | $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st; | |
217 | ||
218 | \@st | |
219 | } | |
220 | ||
221 | sub update_status { | |
222 | my $jobs = $db->select('jobs', 'id,owner,problem,result', {-not_bool => 'private'}, 'id'); | |
223 | ||
224 | my %hash; | |
225 | while ($jobs->into(my ($id, $owner, $problem, $result))) { | |
226 | $hash{$problem, $owner} = [$id, $result ? 0 : 1]; | |
227 | } | |
228 | ||
229 | my @problem_statuses = map { [split ($;), @{$hash{$_}} ] } keys %hash; | |
230 | ||
231 | my @contest_statuses = map { | |
232 | my $ct = $_; | |
233 | map { [$ct, $_->{user}, $_->{score}, $_->{rank}] } @{standings $ct} | |
234 | } $db->select('contests', 'id')->flat; | |
235 | ||
236 | $db->begin; | |
237 | $db->delete('problem_status'); | |
238 | $db->query('INSERT INTO problem_status (problem,owner,job,solved) VALUES (??)', @$_) for @problem_statuses; | |
239 | $db->delete('contest_status'); | |
240 | $db->query('INSERT INTO contest_status (contest,owner,score,rank) VALUES (??)', @$_) for @contest_statuses; | |
241 | $db->commit | |
242 | } | |
243 | ||
244 | sub rerun_job { | |
245 | my ($id) = @_; | |
246 | query rerun_job_sth => $id; | |
247 | purge '/log/'; | |
248 | purge "/log/$id"; | |
249 | } | |
250 | ||
251 | sub take_job { | |
252 | my ($daemon) = @_; | |
253 | my $id = query(take_job_sth => $daemon)->list; | |
254 | return $id ? db->select(jobs => '*', {id => $id})->hash : undef; | |
255 | } | |
256 | ||
257 | sub finish_job { | |
258 | my ($job, $private, %args) = @_; | |
259 | db->update(jobs => \%args, {id => $job->{id}}); | |
260 | return if $private; | |
261 | my $status = { | |
262 | problem => $job->{problem}, | |
263 | owner => $job->{owner}, | |
264 | job => $job->{id}, | |
265 | solved => ($args{result} ? 0 : 1), | |
266 | }; | |
267 | eval { | |
268 | db->insert(problem_status => $status) | |
269 | } or db->update(problem_status => $status, {owner => $job->{owner}, problem => $job->{problem}}); | |
270 | } | |
271 | ||
272 | my @PURGE_HOSTS = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : (); | |
273 | my $ht = HTTP::Tiny->new; | |
274 | ||
275 | sub purge { | |
276 | $ht->request(PURGE => "http://$_$_[0]") for @PURGE_HOSTS; | |
277 | } | |
278 | ||
279 | 1; | |
280 | ||
281 | __END__ | |
282 | ||
283 | =encoding utf-8 | |
284 | ||
285 | =head1 NAME | |
286 | ||
287 | Gruntmaster::Data - Gruntmaster 6000 Online Judge -- database interface and tools | |
288 | ||
289 | =head1 SYNOPSIS | |
290 | ||
291 | my $db = Gruntmaster::Data->connect('dbi:Pg:'); | |
292 | ||
293 | my $problem = $db->problem('my_problem'); | |
294 | $problem->update({timeout => 2.5}); # Set time limit to 2.5 seconds | |
295 | $problem->rerun; # And rerun all jobs for this problem | |
296 | ||
297 | # ... | |
298 | ||
299 | my $contest = $db->contests->create({ # Create a new contest | |
300 | id => 'my_contest', | |
301 | name => 'My Awesome Contest', | |
302 | start => time + 100, | |
303 | end => time + 1900, | |
304 | }); | |
305 | $db->contest_problems->create({ # Add a problem to the contest | |
306 | contest => 'my_contest', | |
307 | problem => 'my_problem', | |
308 | }); | |
309 | ||
310 | say 'The contest has not started yet' if $contest->is_pending; | |
311 | ||
312 | # ... | |
313 | ||
314 | my @jobs = $db->jobs->search({contest => 'my_contest', owner => 'MGV'})->all; | |
315 | $_->rerun for @jobs; # Rerun all jobs sent by MGV in my_contest | |
316 | ||
317 | =head1 DESCRIPTION | |
318 | ||
319 | Gruntmaster::Data is the interface to the Gruntmaster 6000 database. Read the L<DBIx::Class> documentation for usage information. | |
320 | ||
321 | In addition to the typical DBIx::Class::Schema methods, this module contains several convenience methods: | |
322 | ||
323 | =over | |
324 | ||
325 | =item contests | |
326 | ||
327 | Equivalent to C<< $schema->resultset('Contest') >> | |
328 | ||
329 | =item contest_problems | |
330 | ||
331 | Equivalent to C<< $schema->resultset('ContestProblem') >> | |
332 | ||
333 | =item jobs | |
334 | ||
335 | Equivalent to C<< $schema->resultset('Job') >> | |
336 | ||
337 | =item problems | |
338 | ||
339 | Equivalent to C<< $schema->resultset('Problem') >> | |
340 | ||
341 | =item users | |
342 | ||
343 | Equivalent to C<< $schema->resultset('User') >> | |
344 | ||
345 | =item contest($id) | |
346 | ||
347 | Equivalent to C<< $schema->resultset('Contest')->find($id) >> | |
348 | ||
349 | =item job($id) | |
350 | ||
351 | Equivalent to C<< $schema->resultset('Job')->find($id) >> | |
352 | ||
353 | =item problem($id) | |
354 | ||
355 | Equivalent to C<< $schema->resultset('Problem')->find($id) >> | |
356 | ||
357 | =item user($id) | |
358 | ||
359 | Equivalent to C<< $schema->resultset('User')->find($id) >> | |
360 | ||
361 | =item user_list | |
362 | ||
363 | Returns a list of users as an arrayref containing hashrefs. | |
364 | ||
365 | =item user_entry($id) | |
366 | ||
367 | Returns a hashref with information about the user $id. | |
368 | ||
369 | =item problem_list([%args]) | |
370 | ||
371 | Returns a list of problems grouped by level. A hashref with levels as keys. | |
372 | ||
373 | Takes the following arguments: | |
374 | ||
375 | =over | |
376 | ||
377 | =item owner | |
378 | ||
379 | Only show problems owned by this user | |
380 | ||
381 | =item contest | |
382 | ||
383 | Only show problems in this contest | |
384 | ||
385 | =back | |
386 | ||
387 | =item problem_entry($id, [$contest, $user]) | |
388 | ||
389 | Returns a hashref with information about the problem $id. If $contest and $user are present, problem open data is updated. | |
390 | ||
391 | =item contest_list([%args]) | |
392 | ||
393 | Returns a list of contests grouped by state. A hashref with the following keys: | |
394 | ||
395 | =over | |
396 | ||
397 | =item pending | |
398 | ||
399 | An arrayref of hashrefs representing pending contests | |
400 | ||
401 | =item running | |
402 | ||
403 | An arrayref of hashrefs representing running contests | |
404 | ||
405 | =item finished | |
406 | ||
407 | An arrayref of hashrefs representing finished contests | |
408 | ||
409 | =back | |
410 | ||
411 | Takes the following arguments: | |
412 | ||
413 | =over | |
414 | ||
415 | =item owner | |
416 | ||
417 | Only show contests owned by this user. | |
418 | ||
419 | =back | |
420 | ||
421 | =item contest_entry($id) | |
422 | ||
423 | Returns a hashref with information about the contest $id. | |
424 | ||
425 | =item job_list([%args]) | |
426 | ||
427 | Returns a list of jobs as an arrayref containing hashrefs. Takes the following arguments: | |
428 | ||
429 | =over | |
430 | ||
431 | =item owner | |
432 | ||
433 | Only show jobs submitted by this user. | |
434 | ||
435 | =item contest | |
436 | ||
437 | Only show jobs submitted in this contest. | |
438 | ||
439 | =item problem | |
440 | ||
441 | Only show jobs submitted for this problem. | |
442 | ||
443 | =item page | |
444 | ||
445 | Show this page of results. Defaults to 1. Pages have 10 entries, and the first page has the most recent jobs. | |
446 | ||
447 | =back | |
448 | ||
449 | =item job_entry($id) | |
450 | ||
451 | Returns a hashref with information about the job $id. | |
452 | ||
453 | =back | |
454 | ||
455 | =head1 AUTHOR | |
456 | ||
457 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
458 | ||
459 | =head1 COPYRIGHT AND LICENSE | |
460 | ||
461 | Copyright (C) 2014 by Marius Gavrilescu | |
462 | ||
463 | This library is free software; you can redistribute it and/or modify | |
464 | it under the same terms as Perl itself, either Perl version 5.18.1 or, | |
465 | at your option, any later version of Perl 5 you may have available. | |
466 | ||
467 | ||
468 | =cut |