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