]> iEval git - gruntmaster-data.git/blobdiff - lib/Gruntmaster/Data.pm
Improve user_list performance
[gruntmaster-data.git] / lib / Gruntmaster / Data.pm
index e8b5c73b5ed90becf07f8b3e8caa491062862722..4935e024d2920a8718de26d0d0092a1bee922e16 100644 (file)
@@ -15,7 +15,7 @@ __PACKAGE__->load_namespaces;
 # Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-03-05 13:11:39
 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dAEmtAexvUaNXLgYz2rNEg
 
-our $VERSION = '5999.000_011';
+our $VERSION = '5999.000_012';
 
 use Lingua::EN::Inflect qw/PL_N/;
 use JSON::MaybeXS qw/decode_json/;
@@ -42,12 +42,22 @@ BEGIN {
 }
 
 sub user_list {
-       my $rs = $_[0]->users->search(undef, {columns => USER_PUBLIC_COLUMNS, prefetch => [qw/problem_statuses contest_statuses/]} );
-       [ sort { $b->{solved} <=> $a->{solved} or $b->{attempted} <=> $a->{attempted} }map {
-               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 ($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 $id = $_->id;
+               +{ $_->get_columns,
+                  solved => ($solved{$id} // 0),
+                  attempted => ($attempted{$id} // 0),
+                  contests => ($contests{$id} // 0) }
        } $rs->all ]
 }
 
This page took 0.01959 seconds and 4 git commands to generate.