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