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