From c9311d506d661ee510d8609e8c4c56bf1079a94d Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Tue, 16 Dec 2014 19:44:16 +0200 Subject: [PATCH] Improve user_list performance --- lib/Gruntmaster/Data.pm | 20 +++++++++++++++----- lib/Gruntmaster/Data/Result/ContestStatus.pm | 2 +- lib/Gruntmaster/Data/Result/ProblemStatus.pm | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/Gruntmaster/Data.pm b/lib/Gruntmaster/Data.pm index 59cd64b..4935e02 100644 --- a/lib/Gruntmaster/Data.pm +++ b/lib/Gruntmaster/Data.pm @@ -42,12 +42,22 @@ BEGIN { } sub user_list { - my $rs = $_[0]->users->search(undef, {columns => USER_PUBLIC_COLUMNS, prefetch => [qw/problem_statuses contest_statuses/]} ); + my ($self) = @_; + my $rs = $self->users->search(undef, {columns => USER_PUBLIC_COLUMNS} ); + my (%solved, %attempted, %contests); + + for my $row ($self->problem_statuses->all) { + $solved {$row->rawowner}++ if $row->solved; + $attempted{$row->rawowner}++ unless $row->solved; + } + $contests{$_->rawowner}++ for $self->contest_statuses->all; + [ sort { $b->{solved} <=> $a->{solved} or $b->{attempted} <=> $a->{attempted} } map { ## no critic (ProhibitReverseSort) - my $solved = $_->problem_statuses->count({solved => 1}); - my $attempted = $_->problem_statuses->count({solved => 0}); - my $contests = $_->contest_statuses->count; - +{ $_->get_columns, solved => $solved, attempted => $attempted, contests => $contests } + my $id = $_->id; + +{ $_->get_columns, + solved => ($solved{$id} // 0), + attempted => ($attempted{$id} // 0), + contests => ($contests{$id} // 0) } } $rs->all ] } diff --git a/lib/Gruntmaster/Data/Result/ContestStatus.pm b/lib/Gruntmaster/Data/Result/ContestStatus.pm index dae7cbe..2e52e65 100644 --- a/lib/Gruntmaster/Data/Result/ContestStatus.pm +++ b/lib/Gruntmaster/Data/Result/ContestStatus.pm @@ -108,6 +108,6 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07042 @ 2014-12-11 23:51:27 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:vfOfZeATPRODifpgHO4L0A +sub rawowner { shift->get_column('owner') } -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/lib/Gruntmaster/Data/Result/ProblemStatus.pm b/lib/Gruntmaster/Data/Result/ProblemStatus.pm index 09920a7..69be02f 100644 --- a/lib/Gruntmaster/Data/Result/ProblemStatus.pm +++ b/lib/Gruntmaster/Data/Result/ProblemStatus.pm @@ -133,6 +133,6 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07042 @ 2014-12-11 23:51:27 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:SUAwYQhgBtoCjtFSOMc4FQ +sub rawowner { shift->get_column('owner') } -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; -- 2.39.2