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