Improve performance of standings/update_status. Break standings API
authorMarius Gavrilescu <marius@ieval.ro>
Fri, 12 Dec 2014 12:16:10 +0000 (14:16 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Fri, 12 Dec 2014 12:16:10 +0000 (14:16 +0200)
lib/Gruntmaster/Data.pm
lib/Gruntmaster/Data/Result/ContestProblem.pm
lib/Gruntmaster/Data/Result/Job.pm
lib/Gruntmaster/Data/Result/Open.pm

index 2422e77e5f0a57d0dce196dbc9921fc3dd124169..3258c85c32b769b460c769188c4300e0e79d4135 100644 (file)
@@ -57,29 +57,33 @@ sub standings {
        my ($self, $ct) = @_;
        $ct &&= $self->contest($ct);
 
-       my @problems = map { $_->problem } $self->contest_problems->search({contest => $ct && $ct->id}, {qw/join problem order_by problem.level/});
-       my (%scores, %tries);
-       for my $job ($self->jobs->search({contest => $ct && $ct->id}, {order_by => 'id'})) {
+       my @problems = map { $_->rawproblem } $self->contest_problems->search({contest => $ct && $ct->id}, {qw/join problem order_by problem.level/});
+       my (%scores, %tries, %opens);
+       $opens{$_->rawproblem, $_->rawowner} = $_ for $self->opens->search({contest => $ct && $ct->id});
+       for my $job ($self->jobs->search({contest => $ct && $ct->id}, {qw/order_by me.id prefetch/ => [qw/problem/]})) {
                if ($ct) {
-                       my $open = $self->opens->find($ct->id, $job->problem->id, $job->owner->id);
+                       my $open = $opens{$job->rawproblem, $job->rawowner};
                        my $time = $job->date - ($open ? $open->time : $ct->start);
                        next if $time < 0;
                        my $value = $job->problem->value;
                        my $factor = $job->result ? 0 : 1;
                        $factor = $1 / 100 if $job->result_text =~ /^(\d+ )/s;
-                       $scores{$job->owner->id}{$job->problem->id} = int ($factor * calc_score ($value, $time, $tries{$job->owner->id}{$job->problem->id}++, $ct->stop - $ct->start));
+                       $scores{$job->rawowner}{$job->rawproblem} = int ($factor * calc_score ($value, $time, $tries{$job->rawowner}{$job->rawproblem}++, $ct->stop - $ct->start));
                } else {
                        no warnings 'numeric'; ## no critic (ProhibitNoWarnings)
-                       $scores{$job->owner->id}{$job->problem->id} = 0 + $job->result_text || ($job->result ? 0 : 100)
+                       $scores{$job->rawowner}{$job->rawproblem} = 0 + $job->result_text || ($job->result ? 0 : 100)
                }
        }
 
-       my @st = sort { $b->{score} <=> $a->{score} or $a->{user}->id cmp $b->{user}->id} map { ## no critic (ProhibitReverseSortBlock)
+       my %user_to_name = map { $_ => $_->name } $self->users->all;
+
+       my @st = sort { $b->{score} <=> $a->{score} or $a->{user} cmp $b->{user} } map { ## no critic (ProhibitReverseSortBlock)
                my $user = $_;
                +{
-                       user => $self->user($user),
+                       user => $user,
+                       user_name => $user_to_name{$user},
                        score => sum (values %{$scores{$user}}),
-                       scores => [map { $scores{$user}{$_->id} // '-'} @problems],
+                       scores => [map { $scores{$user}{$_} // '-'} @problems],
                        problems => $ct,
                }
        } keys %scores;
@@ -193,8 +197,9 @@ sub job_entry {
 sub update_status {
        my ($self) = @_;
        my @jobs = $self->jobs->search(undef, {cache => 1})->all;
+
        my %hash;
-       $hash{$_->get_column('problem'), $_->get_column('owner')} = [$_, $_->result ? 1 : 0] for @jobs;
+       $hash{$_->get_column('problem'), $_->get_column('owner')} = [$_->id, $_->result ? 1 : 0] for @jobs;
        my @problem_statuses = map { [split ($;), @{$hash{$_}} ] } keys %hash;
 
        my @contest_statuses = map {
index bf57e09ef37a71a258a833f83c8cc0afead951f9..5ec1dfe998d870545575085d9878cda6e70d8cad 100644 (file)
@@ -94,8 +94,9 @@ __PACKAGE__->belongs_to(
 # Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-05-16 15:03:32
 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:fG3PNI7Ar318nxMchtJNuA
 
+sub rawcontest { shift->get_column('contest') }
+sub rawproblem { shift->get_column('problem') }
 
-# You can replace this text with custom code or comments, and it will be preserved on regeneration
 1;
 
 __END__
index 66aed554fb875568f2eadd23e51539ddc7198351..0fcf8c0221035e632447d331065eb0c16e13296a 100644 (file)
@@ -220,6 +220,10 @@ __PACKAGE__->has_many(
 # Created by DBIx::Class::Schema::Loader v0.07042 @ 2014-12-11 23:51:27
 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:D49ekK0vGg/7b8xXZoYTWQ
 
+sub rawcontest { shift->get_column('contest') }
+sub rawowner { shift->get_column('owner') }
+sub rawproblem { shift->get_column('problem') }
+
 sub rerun {
        shift->update({daemon => undef, result => -2, result_text => undef});
 }
index 650f9cb4c3aeabd090c88a7b95085a9bb6f3449b..2d6af7cb48df4fdceeb89594408223871800dc4a 100644 (file)
@@ -126,8 +126,9 @@ __PACKAGE__->belongs_to(
 # Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-05-16 15:03:32
 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:VihrUa/CI0cg8k8wpHxQDg
 
+sub rawowner { shift->get_column('owner') }
+sub rawproblem { shift->get_column('problem') }
 
-# You can replace this text with custom code or comments, and it will be preserved on regeneration
 1;
 
 __END__
This page took 0.013912 seconds and 4 git commands to generate.