Sort problem and contest lists in user_entry
[gruntmaster-data.git] / lib / Gruntmaster / Data.pm
CommitLineData
4ed3f8e7 1use utf8;
bbf8209c 2package Gruntmaster::Data;
4ed3f8e7
MG
3
4# Created by DBIx::Class::Schema::Loader
5# DO NOT MODIFY THE FIRST PART OF THIS FILE
6
7use strict;
bbf8209c 8use warnings;
014ee8a6 9
4ed3f8e7
MG
10use base 'DBIx::Class::Schema';
11
12__PACKAGE__->load_namespaces;
014ee8a6 13
f7386aab 14
4ed3f8e7
MG
15# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-03-05 13:11:39
16# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dAEmtAexvUaNXLgYz2rNEg
17
625a7bd0 18our $VERSION = '5999.000_012';
4ed3f8e7
MG
19
20use Lingua::EN::Inflect qw/PL_N/;
91203bd5 21use JSON::MaybeXS qw/decode_json/;
3473324b 22use PerlX::Maybe qw/maybe/;
4ed3f8e7 23use Sub::Name qw/subname/;
014ee8a6 24
de625c9b
MG
25use constant PROBLEM_PUBLIC_COLUMNS => [qw/id author writer level name owner private statement timeout olimit value/];
26use constant USER_PUBLIC_COLUMNS => [qw/id admin name town university level/];
27use constant JOBS_PER_PAGE => 10;
28
014ee8a6 29sub dynsub{
fb6a4e3d 30 our ($name, $sub) = @_;
dcd72991 31 no strict 'refs'; ## no critic (Strict)
fb6a4e3d 32 *$name = subname $name => $sub
014ee8a6
MG
33}
34
35BEGIN {
adb42d4d 36 for my $rs (qw/contest contest_problem job open problem user problem_status contest_status/) {
4ed3f8e7 37 my $rsname = ucfirst $rs;
dcd72991 38 $rsname =~ s/_([a-z])/\u$1/gs;
4ed3f8e7
MG
39 dynsub PL_N($rs) => sub { $_[0]->resultset($rsname) };
40 dynsub $rs => sub { $_[0]->resultset($rsname)->find($_[1]) };
014ee8a6
MG
41 }
42}
43
de625c9b 44sub user_list {
c9311d50
MG
45 my ($self) = @_;
46 my $rs = $self->users->search(undef, {columns => USER_PUBLIC_COLUMNS} );
47 my (%solved, %attempted, %contests);
48
49 for my $row ($self->problem_statuses->all) {
50 $solved {$row->rawowner}++ if $row->solved;
51 $attempted{$row->rawowner}++ unless $row->solved;
52 }
53 $contests{$_->rawowner}++ for $self->contest_statuses->all;
54
1db27a21 55 [ sort { $b->{solved} <=> $a->{solved} or $b->{attempted} <=> $a->{attempted} } map { ## no critic (ProhibitReverseSort)
c9311d50
MG
56 my $id = $_->id;
57 +{ $_->get_columns,
58 solved => ($solved{$id} // 0),
59 attempted => ($attempted{$id} // 0),
60 contests => ($contests{$id} // 0) }
281c3952 61 } $rs->all ]
de625c9b
MG
62}
63
64sub user_entry {
65 my ($self, $id) = @_;
adb42d4d 66 my $user = $self->users->find($id, {columns => USER_PUBLIC_COLUMNS, prefetch => [qw/problem_statuses contest_statuses/]});
197d1959
MG
67 my @problems = map { {problem => $_->get_column('problem'), solved => $_->solved} } $user->problem_statuses->search(undef, {order_by => 'problem'});
68 my @contests = map { {contest => $_->contest->id, contest_name => $_->contest->name, rank => $_->rank, score => $_->score} } $user->contest_statuses->search(undef, {prefetch => 'contest', order_by => 'contest.start DESC'});
adb42d4d 69 +{ $user->get_columns, problems => \@problems, contests => \@contests }
de625c9b
MG
70}
71
72sub problem_list {
73 my ($self, %args) = @_;
74 my $rs = $self->problems->search(undef, {order_by => 'me.name', columns => PROBLEM_PUBLIC_COLUMNS, prefetch => 'owner'});
75 $rs = $rs->search({-or => ['contest_problems.contest' => undef, 'contest.stop' => {'<=', time}], 'me.private' => 0}, {join => {'contest_problems' => 'contest'}, distinct => 1}) unless $args{contest};
76 $rs = $rs->search({'contest_problems.contest' => $args{contest}}, {join => 'contest_problems'}) if $args{contest};
77 $rs = $rs->search({'me.owner' => $args{owner}}) if $args{owner};
78 my %params;
79 $params{contest} = $args{contest} if $args{contest};
80 for ($rs->all) {
81 $params{$_->level} //= [];
35596ea8 82 push @{$params{$_->level}}, {$_->get_columns, owner_name => $_->owner->name} ;
de625c9b
MG
83 }
84 \%params
85}
86
87sub problem_entry {
88 my ($self, $id, $contest, $user) = @_;
de625c9b 89 my $running = $contest && $self->contest($contest)->is_running;
8bd66733
MG
90 my $columns = PROBLEM_PUBLIC_COLUMNS;
91 push @$columns, 'solution' unless $running;
92 my $pb = $self->problems->find($id, {columns => $columns, prefetch => 'owner'});
dcd72991 93 eval { ## no critic (RequireCheckingReturnValueOfEval)
de625c9b
MG
94 $self->opens->create({
95 contest => $contest,
96 problem => $id,
97 owner => $user,
98 time => time,
99 })
100 } if $running;
2431260b
MG
101 $contest &&= $self->contest($contest);
102 +{ $pb->get_columns, owner_name => $pb->owner->name, cansubmit => $contest ? $running : 1, $running ? (contest_start => $contest->start, contest_stop => $contest->stop) : () }
de625c9b
MG
103}
104
105sub contest_list {
106 my ($self, %args) = @_;
107 my $rs = $self->contests->search(undef, {order_by => {-desc => 'start'}, prefetch => 'owner'});
108 $rs = $rs->search({owner => $args{owner}}) if $args{owner};
109 my %params;
110 for ($rs->all) {
111 my $state = $_->is_pending ? 'pending' : $_->is_running ? 'running' : 'finished';
112 $params{$state} //= [];
35596ea8 113 push @{$params{$state}}, { $_->get_columns, started => !$_->is_pending, owner_name => $_->owner->name };
de625c9b
MG
114 }
115 \%params
116}
117
118sub contest_entry {
119 my ($self, $id) = @_;
120 my $ct = $self->contest($id);
121 +{ $ct->get_columns, started => !$ct->is_pending, owner_name => $ct->owner->name }
122}
123
124sub job_list {
125 my ($self, %args) = @_;
126 $args{page} //= 1;
3473324b 127 my $rs = $self->jobs->search(undef, {order_by => {-desc => 'me.id'}, prefetch => ['problem', 'owner'], rows => JOBS_PER_PAGE, page => $args{page}});
526b9e80
MG
128 $rs = $rs->search({'me.owner' => $args{owner}}) if $args{owner};
129 $rs = $rs->search({contest => $args{contest}}) if $args{contest};
130 $rs = $rs->search({problem => $args{problem}}) if $args{problem};
3473324b
MG
131 return {
132 log => [map {
133 my %params = $_->get_columns;
134 $params{owner_name} = $_->owner->name;
135 $params{problem_name} = $_->problem->name;
136 $params{results} &&= decode_json $params{results};
137 $params{size} = length $params{source};
138 delete $params{source};
139 \%params
140 } $rs->all],
141 current_page => $rs->pager->current_page,
142 maybe previous_page => $rs->pager->previous_page,
143 maybe next_page => $rs->pager->next_page,
144 maybe last_page => $rs->pager->last_page,
145 }
de625c9b
MG
146}
147
148sub job_entry {
149 my ($self, $id) = @_;
150 my $job = $self->jobs->find($id, {prefetch => ['problem', 'owner']});
151 my %params = $job->get_columns;
152 $params{owner_name} = $job->owner->name;
153 $params{problem_name} = $job->problem->name;
154 $params{results} &&= decode_json $params{results};
155 $params{size} = length $params{source};
156 delete $params{source};
157 \%params
158}
159
adb42d4d
MG
160sub update_status {
161 my ($self) = @_;
162 my @jobs = $self->jobs->search(undef, {cache => 1})->all;
a2aa46e6 163
adb42d4d 164 my %hash;
9bfb38e0 165 $hash{$_->get_column('problem'), $_->get_column('owner')} = [$_->id, $_->result ? 0 : 1] for @jobs;
adb42d4d
MG
166 my @problem_statuses = map { [split ($;), @{$hash{$_}} ] } keys %hash;
167
168 my @contest_statuses = map {
169 my $contest = $_->id;
751c70b5 170 map { [$contest, $_->{user}, $_->{score}, $_->{rank}] } $_->standings
adb42d4d
MG
171 } $self->contests->all;
172
173 my $txn = sub {
174 $self->problem_statuses->delete;
175 $self->problem_statuses->populate([[qw/problem owner job solved/], @problem_statuses]);
176 $self->contest_statuses->delete;
177 $self->contest_statuses->populate([[qw/contest owner score rank/], @contest_statuses]);
178 };
179
180 $self->txn_do($txn);
181}
182
f7386aab 1831;
4a8747ef
MG
184
185__END__
186
187=encoding utf-8
188
189=head1 NAME
190
191Gruntmaster::Data - Gruntmaster 6000 Online Judge -- database interface and tools
192
193=head1 SYNOPSIS
194
195 my $db = Gruntmaster::Data->connect('dbi:Pg:');
cbb36c78
MG
196
197 my $problem = $db->problem('my_problem');
198 $problem->update({timeout => 2.5}); # Set time limit to 2.5 seconds
199 $problem->rerun; # And rerun all jobs for this problem
200
201 # ...
202
203 my $contest = $db->contests->create({ # Create a new contest
204 id => 'my_contest',
205 name => 'My Awesome Contest',
206 start => time + 100,
207 end => time + 1900,
208 });
209 $db->contest_problems->create({ # Add a problem to the contest
210 contest => 'my_contest',
211 problem => 'my_problem',
212 });
213
214 say 'The contest has not started yet' if $contest->is_pending;
215
216 # ...
217
218 my @jobs = $db->jobs->search({contest => 'my_contest', owner => 'MGV'})->all;
219 $_->rerun for @jobs; # Rerun all jobs sent by MGV in my_contest
4a8747ef
MG
220
221=head1 DESCRIPTION
222
cbb36c78
MG
223Gruntmaster::Data is the interface to the Gruntmaster 6000 database. Read the L<DBIx::Class> documentation for usage information.
224
225In addition to the typical DBIx::Class::Schema methods, this module contains several convenience methods:
226
227=over
228
229=item contests
230
231Equivalent to C<< $schema->resultset('Contest') >>
232
233=item contest_problems
234
235Equivalent to C<< $schema->resultset('ContestProblem') >>
236
237=item jobs
238
239Equivalent to C<< $schema->resultset('Job') >>
240
241=item problems
242
243Equivalent to C<< $schema->resultset('Problem') >>
244
245=item users
246
247Equivalent to C<< $schema->resultset('User') >>
248
249=item contest($id)
250
251Equivalent to C<< $schema->resultset('Contest')->find($id) >>
252
253=item job($id)
254
255Equivalent to C<< $schema->resultset('Job')->find($id) >>
256
257=item problem($id)
258
259Equivalent to C<< $schema->resultset('Problem')->find($id) >>
260
261=item user($id)
262
263Equivalent to C<< $schema->resultset('User')->find($id) >>
264
de625c9b
MG
265=item user_list
266
267Returns a list of users as an arrayref containing hashrefs.
268
269=item user_entry($id)
270
271Returns a hashref with information about the user $id.
272
273=item problem_list([%args])
274
275Returns a list of problems grouped by level. A hashref with levels as keys.
276
277Takes the following arguments:
278
279=over
280
281=item owner
282
283Only show problems owned by this user
284
285=item contest
286
287Only show problems in this contest
288
289=back
290
291=item problem_entry($id, [$contest, $user])
292
293Returns a hashref with information about the problem $id. If $contest and $user are present, problem open data is updated.
294
295=item contest_list([%args])
296
297Returns a list of contests grouped by state. A hashref with the following keys:
298
299=over
300
301=item pending
302
303An arrayref of hashrefs representing pending contests
304
305=item running
306
307An arrayref of hashrefs representing running contests
308
309=item finished
310
311An arrayref of hashrefs representing finished contests
312
313=back
314
315Takes the following arguments:
316
317=over
318
319=item owner
320
321Only show contests owned by this user.
322
323=back
324
325=item contest_entry($id)
326
327Returns a hashref with information about the contest $id.
328
329=item job_list([%args])
330
331Returns a list of jobs as an arrayref containing hashrefs. Takes the following arguments:
332
333=over
334
335=item owner
336
337Only show jobs submitted by this user.
338
339=item contest
340
341Only show jobs submitted in this contest.
342
343=item problem
344
345Only show jobs submitted for this problem.
346
347=item page
348
349Show this page of results. Defaults to 1. Pages have 10 entries, and the first page has the most recent jobs.
350
351=back
352
353=item job_entry($id)
354
355Returns a hashref with information about the job $id.
356
cbb36c78 357=back
4a8747ef
MG
358
359=head1 AUTHOR
360
361Marius Gavrilescu E<lt>marius@ieval.roE<gt>
362
363=head1 COPYRIGHT AND LICENSE
364
365Copyright (C) 2014 by Marius Gavrilescu
366
9d2e740e
MG
367This library is free software; you can redistribute it and/or modify
368it under the same terms as Perl itself, either Perl version 5.18.1 or,
369at your option, any later version of Perl 5 you may have available.
4a8747ef
MG
370
371
372=cut
This page took 0.039092 seconds and 4 git commands to generate.