Add "SET search_path" command in dbinit
[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_016';
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 rerun_problem take_job finish_job open_problem/;
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_problem_sth => 'UPDATE jobs SET daemon=NULL,result=-2,result_text=NULL,results=NULL,errors=NULL WHERE problem = ?',
40 rerun_job_sth => 'UPDATE jobs SET daemon=NULL,result=-2,result_text=NULL,results=NULL,errors=NULL WHERE id = ?',
41 take_job_sth => 'UPDATE jobs SET daemon=? WHERE id = (SELECT id FROM jobs WHERE daemon IS NULL LIMIT 1 FOR UPDATE) RETURNING id',
42 );
43
44 our $db;
45 sub db () { $db }
46
47 sub dbinit {
48 $db = DBIx::Simple->new(@_);
49 $db->keep_statements = 100;
50 $db->dbh->do('SET search_path TO gruntmaster, public');
51 };
52
53 sub purge;
54
55 sub _query {
56 my ($stat, @extra) = @_;
57 $db->query($statements{$stat}, @extra)
58 }
59
60 my (%name_cache, %name_cache_time);
61 use constant NAME_CACHE_MAX_AGE => 5;
62
63 sub _object_name {
64 my ($table, $id) = @_;
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} = {};
69 $name_cache{$table} = $db->select($table, 'id,name')->map;
70 }
71
72 $name_cache{$table}{$id}
73 }
74
75
76 sub _add_names ($) { ## no critic (ProhibitSubroutinePrototypes)
77 my ($el) = @_;
78 return unless defined $el;
79 if (ref $el eq 'ARRAY') {
80 &_add_names ($_) for @$el ## no critic (ProhibitAmpersandSigils)
81 } else {
82 for my $object (qw/contest owner problem/) {
83 my $table = $object eq 'owner' ? 'users' : "${object}s";
84 $el->{"${object}_name"} = _object_name $table, $el->{$object} if defined $el->{$object}
85 }
86 }
87
88 $el
89 }
90
91 sub user_list { scalar _query('user_list_sth')->hashes }
92
93 sub user_entry {
94 my ($id) = @_;
95 my $ret = _query('user_entry_sth', $id)->hash;
96 $ret->{problems} = _add_names _query('problem_status_sth', $id)->hashes;
97 $ret->{contests} = _add_names _query('contest_status_sth', $id)->hashes;
98
99 $ret;
100 }
101
102 sub problem_list {
103 my (%args) = @_;
104 my @columns = @{PROBLEM_PUBLIC_COLUMNS()};
105 push @columns, 'solution' if $args{solution};
106 my %where;
107 $where{private} = 0 unless $args{contest} || $args{private};
108 $where{'cp.contest'} = $args{contest} if $args{contest};
109 $where{owner} = $args{owner} if $args{owner};
110
111 my $table = $args{contest} ? 'problems JOIN contest_problems cp ON cp.problem = id' : 'problems';
112 _add_names $db->select(\$table, \@columns, \%where, 'name')->hashes
113 }
114
115 sub problem_entry {
116 my ($id, $contest) = @_;
117 $contest = contest_entry ($contest) if $contest;
118 my $ret = _add_names _query(problem_entry_sth => $id)->hash;
119 my $limits = _query(limits_sth => $id)->hashes;
120 $ret->{limits} = $limits if @$limits;
121
122 if ($contest) {
123 $ret->{contest_start} = $contest->{start};
124 $ret->{contest_stop} = $contest->{stop};
125 delete $ret->{solution}
126 }
127
128 $ret
129 }
130
131 sub contest_list { _add_names _query('contest_list_sth')->hashes }
132
133 sub contest_entry { _add_names _query(contest_entry_sth => $_[0])->hash }
134
135 sub contest_has_problem { _query('contest_has_problem_sth', @_[0, 1])->flat }
136
137 sub job_list {
138 my (%args) = @_;
139 $args{page} = int ($args{page} // 1);
140 my %where = (
141 maybe contest => $args{contest},
142 maybe owner => $args{owner},
143 maybe problem => $args{problem},
144 maybe result => $args{result},
145 );
146 $where{private} = 0 unless $args{private};
147
148 my $rows = $db->select('job_entry', 'COUNT(*)', \%where)->list;
149 my $pages = int (($rows + JOBS_PER_PAGE - 1) / JOBS_PER_PAGE);
150 my ($stmt, @bind) = $db->abstract->select('job_entry', '*', \%where, {-desc => 'id'});
151 my $jobs = _add_names $db->query("$stmt LIMIT " . JOBS_PER_PAGE . ' OFFSET ' . ($args{page} - 1) * JOBS_PER_PAGE, @bind)->hashes;
152 my $pageinfo = {
153 current_page => $args{page},
154 last_page => $pages,
155 ($args{page} - 1) ? (previous_page => $args{page} - 1) : (),
156 ($args{page} < $pages) ? (next_page => $args{page} + 1) : (),
157 };
158 wantarray ? ($jobs, $pageinfo) : $jobs;
159 }
160
161 sub job_entry {
162 my $ret = _add_names _query(job_entry_sth => $_[0])->hash;
163 $ret->{results} = decode_json $ret->{results} if $ret->{results};
164 $ret
165 }
166
167 sub create_job {
168 my (%args) = @_;
169 $db->update('users', {lastjob => time}, {id => $args{owner}});
170 purge '/log/';
171 scalar $db->insert('jobs', \%args, {returning => 'id'})->list
172 }
173
174 sub _calc_score {
175 my ($mxscore, $time, $tries, $totaltime) = @_;
176 my $score = $mxscore;
177 $time = 300 if $time > $totaltime; # uncoverable branch true does not happen anymore (only possible if opens are broken)
178 $score = ($totaltime - $time) / $totaltime * $score;
179 $score -= $tries / 10 * $mxscore;
180 $score = $mxscore * 3 / 10 if $score < $mxscore * 3 / 10;
181 int $score + 0.5
182 }
183
184 sub standings {
185 my ($ct) = @_;
186 my @problems = sort { $a->{value} <=> $b->{value} } @{problem_list contest => $ct};
187 my %values = map { $_->{id} => $_->{value} } @problems;
188 $ct = contest_entry $ct;
189
190 my (%scores, %tries, %opens);
191 my $opens = _query(opens_sth => $ct->{id});
192 while ($opens->into(my ($problem, $owner, $time))) {
193 $opens{$problem, $owner} = $time;
194 }
195
196 # result IS NULL if job was never run
197 # result = -2 if job is being rerun
198 my %where = (contest => $ct->{id}, result => {'>=', 0});
199 my $jobs = $db->select('job_entry', '*', \%where, 'id');
200
201 while (my $job = $jobs->hash) {
202 my $open = $opens{$job->{problem}, $job->{owner}} // $ct->{start};
203 my $time = $job->{date} - $open;
204 next if $time < 0; # uncoverable branch true job sent before contest is deprecated
205 my $value = $values{$job->{problem}};
206 my $factor = $job->{result} ? 0 : 1;
207 $factor = $1 / 100 if $job->{result_text} =~ /^(\d+ )/s;
208 $scores{$job->{owner}}{$job->{problem}} = int ($factor * _calc_score ($value, $time, $tries{$job->{owner}}{$job->{problem}}++, $ct->{stop} - $ct->{start}));
209 }
210
211 my @st = sort { $b->{score} <=> $a->{score} or $a->{user} cmp $b->{user} } map { ## no critic (ProhibitReverseSortBlock)
212 my $user = $_;
213 +{
214 user => $user,
215 user_name => _object_name(users => $user),
216 score => sum (values %{$scores{$user}}),
217 scores => [map { $scores{$user}{$_->{id}} // '-'} @problems],
218 }
219 } keys %scores;
220
221 $st[0]->{rank} = 1 if @st;
222 $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st;
223
224 \@st
225 }
226
227 sub update_status {
228 my $jobs = $db->select('jobs', 'id,owner,problem,result', {-not_bool => 'private'}, 'id');
229
230 my %hash;
231 while ($jobs->into(my ($id, $owner, $problem, $result))) {
232 $hash{$problem, $owner} = [$id, $result ? 0 : 1];
233 }
234
235 my @problem_statuses = map { [split ($;), @{$hash{$_}} ] } keys %hash;
236
237 my @contest_statuses = map {
238 my $ct = $_;
239 map { [$ct, $_->{user}, $_->{score}, $_->{rank}] } @{standings $ct}
240 } $db->select('contests', 'id')->flat;
241
242 $db->begin;
243 $db->delete('problem_status');
244 $db->query('INSERT INTO problem_status (problem,owner,job,solved) VALUES (??)', @$_) for @problem_statuses;
245 $db->delete('contest_status');
246 $db->query('INSERT INTO contest_status (contest,owner,score,rank) VALUES (??)', @$_) for @contest_statuses;
247 $db->commit
248 }
249
250 sub rerun_problem {
251 my ($problem) = @_;
252 _query rerun_problem_sth => $problem;
253 purge '/log/';
254 }
255
256 sub rerun_job {
257 my ($id) = @_;
258 _query rerun_job_sth => $id;
259 purge '/log/';
260 purge "/log/$id";
261 }
262
263 sub take_job {
264 my ($daemon) = @_;
265 my $id = _query(take_job_sth => $daemon)->list;
266 return unless $id;
267 purge '/log/';
268 purge "/log/$id";
269 db->select(jobs => '*', {id => $id})->hash
270 }
271
272 sub finish_job {
273 my ($job, $private, %args) = @_;
274 db->update(jobs => \%args, {id => $job->{id}});
275 purge '/log/';
276 purge '/log/' . $job->{id};
277 purge '/st/' . $job->{contest} if $job->{contest};
278 return if $private;
279 my $status = {
280 problem => $job->{problem},
281 owner => $job->{owner},
282 job => $job->{id},
283 solved => ($args{result} ? 0 : 1),
284 };
285 eval {
286 db->insert(problem_status => $status)
287 } or db->update(problem_status => $status, {owner => $job->{owner}, problem => $job->{problem}});
288 purge '/us/' . $job->{owner};
289 }
290
291 sub open_problem {
292 my ($contest, $problem, $owner, $time) = @_;
293 my $ct = contest_entry($contest);
294 return unless $ct->{id} && $time >= $ct->{start} && $time < $ct->{stop}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
295 eval { db->insert(opens => { ## no critic (RequireCheckingReturnValueOfEval)
296 contest => $contest,
297 problem => $problem,
298 owner => $owner,
299 time => $time}) };
300 }
301
302 my @PURGE_HOSTS = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : ();
303 my $ht = HTTP::Tiny->new;
304
305 sub purge {
306 $ht->request(PURGE => "http://$_$_[0]") for @PURGE_HOSTS;
307 }
308
309 1;
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
322 =head1 DESCRIPTION
323
324 Gruntmaster::Data is the interface to the Gruntmaster 6000 database.
325
326 All functions are exported by default.
327
328 =over
329
330 =item B<dbinit>(I<@args>)
331
332 This function connects to the database. I<@args> are the arguments
333 passed to the L<DBIx::Simple> constructor.
334
335 =item B<purge>(I<$url_path>)
336
337 Purges a relative URL from the Varnish Cache by sending PURGE
338 $url_path requests to all hosts in the PURGE_HOSTS environment
339 variable.
340
341 =item B<db>
342
343 Returns a L<DBIx::Simple> object for interacting with the database
344 directly. Use this when no other function in this module is suitable.
345
346 =item B<user_list>
347
348 Returns an arrayref of the top 200 users.
349
350 =item B<user_entry>(I<$id>)
351
352 Returns a hashref describing the user I<$id>.
353
354 =item B<problem_list>([I<%args>])
355
356 Returns an arrayref of problems.
357
358 Takes the following named arguments:
359
360 =over
361
362 =item owner
363
364 Only show problems owned by this user
365
366 =item contest
367
368 Only show problems in this contest
369
370 =item private
371
372 If true, include private problems. Always true if contest is present.
373
374 =item solution
375
376 If true, include problem solutions
377
378 =back
379
380 =item B<problem_entry>(i<$id>, [I<$contest>])
381
382 Returns a hashref describing the problem I<$id>. If $contest is
383 present, contest start and stop times are included, and the solution
384 is deleted.
385
386 =item B<contest_list>
387
388 Returns an arrayref of contests.
389
390 =item B<contest_entry>(I<$id>)
391
392 Returns a hashref describing the contest I<$id>.
393
394 =item B<contest_has_problem>(I<$contest>, I<$problem>)
395
396 Returns true if the contest I<$contest> includes the problem
397 I<$problem>, false otherwise.
398
399 =item B<job_list>([I<%args>])
400
401 In scalar context, returns an arrayref of jobs. In list context,
402 returns an arrayref of jobs and a hashref of information about pages.
403
404 Takes the following named arguments:
405
406 =over
407
408 =item page
409
410 Show this page of the job log. Defaults to 1.
411
412 =item owner
413
414 Only show jobs submitted by this user.
415
416 =item contest
417
418 Only show jobs submitted in this contest.
419
420 =item problem
421
422 Only show jobs submitted for this problem.
423
424 =item result
425
426 Only show jobs with this result (see the constants in
427 L<Gruntmaster::Daemon::Constants>).
428
429 =item private
430
431 If true, include private jobs. Defaults to false.
432
433 =back
434
435 =item B<job_entry>(I<$id>)
436
437 Returns a hashref describing the job I<$id>.
438
439 =item B<create_job>(I<%args>)
440
441 Insert a new job into the database. This function also updates the
442 lastjob field for the job's owner.
443
444 =item B<standings>(I<$ct>)
445
446 Returns an arrayref of the standings of contest I<$ct>.
447
448 =item B<update_status>
449
450 Rebuilds the problem_status and contest_status tables.
451
452 =item B<rerun_job>(I<$id>)
453
454 Marks the job $id as pending and clears its results, so that it will
455 be run again by the daemon.
456
457 =item B<take_job>(I<$daemon>)
458
459 Marks a random job as being run by I<$daemon>. Returns a hashref
460 describing the job, or undef if no job was available.
461
462 =item B<finish_job>(I<$job>, I<$private>, I<%results>)
463
464 Updates the job $job with the results in %results. If $private is
465 false, also updates the problem_status table.
466
467 =item B<open_problem>(I<$contest>, I<$problem>, I<$owner>, I<$time>)
468
469 Notes that I<$owner> has opened the problem I<$problem> of contest
470 I<$contest> at time I<$time>. If the C<opens> table already contains
471 this (I<$contest>, I<$problem>, I<$owner>) triplet, this function does
472 nothing.
473
474 =back
475
476 =head1 AUTHOR
477
478 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
479
480 =head1 COPYRIGHT AND LICENSE
481
482 Copyright (C) 2014-2016 by Marius Gavrilescu
483
484 This library is free software; you can redistribute it and/or modify
485 it under the same terms as Perl itself, either Perl version 5.20.1 or,
486 at your option, any later version of Perl 5 you may have available.
487
488
489 =cut
This page took 0.048515 seconds and 4 git commands to generate.