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