From a2aa46e6fb478ba94f05d5ce36b7848e7a8bf807 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Fri, 12 Dec 2014 14:16:10 +0200 Subject: [PATCH] Improve performance of standings/update_status. Break standings API --- lib/Gruntmaster/Data.pm | 25 +++++++++++-------- lib/Gruntmaster/Data/Result/ContestProblem.pm | 3 ++- lib/Gruntmaster/Data/Result/Job.pm | 4 +++ lib/Gruntmaster/Data/Result/Open.pm | 3 ++- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/Gruntmaster/Data.pm b/lib/Gruntmaster/Data.pm index 2422e77..3258c85 100644 --- a/lib/Gruntmaster/Data.pm +++ b/lib/Gruntmaster/Data.pm @@ -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 { diff --git a/lib/Gruntmaster/Data/Result/ContestProblem.pm b/lib/Gruntmaster/Data/Result/ContestProblem.pm index bf57e09..5ec1dfe 100644 --- a/lib/Gruntmaster/Data/Result/ContestProblem.pm +++ b/lib/Gruntmaster/Data/Result/ContestProblem.pm @@ -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__ diff --git a/lib/Gruntmaster/Data/Result/Job.pm b/lib/Gruntmaster/Data/Result/Job.pm index 66aed55..0fcf8c0 100644 --- a/lib/Gruntmaster/Data/Result/Job.pm +++ b/lib/Gruntmaster/Data/Result/Job.pm @@ -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}); } diff --git a/lib/Gruntmaster/Data/Result/Open.pm b/lib/Gruntmaster/Data/Result/Open.pm index 650f9cb..2d6af7c 100644 --- a/lib/Gruntmaster/Data/Result/Open.pm +++ b/lib/Gruntmaster/Data/Result/Open.pm @@ -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__ -- 2.39.2