From 10038be29fac47435f1f6ac412d8cf0e7c996ac6 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Thu, 5 Jul 2018 14:46:16 +0100 Subject: [PATCH] Bug 20996: Further enhance performance We were immediately unblessing all request objects after retrieval then, at a later stage, re-instantiating each one so we could call methods on that. This was a very bad idea. We now preserve the objects for as long as we can. Signed-off-by: Magnus Enger --- Koha/REST/V1/Illrequests.pm | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/Koha/REST/V1/Illrequests.pm b/Koha/REST/V1/Illrequests.pm index 59e3f82a44..0a6dee0e2b 100644 --- a/Koha/REST/V1/Illrequests.pm +++ b/Koha/REST/V1/Illrequests.pm @@ -51,18 +51,20 @@ sub list { delete $args->{embed}; } - my $requests = Koha::Illrequests->unblessed; + # Get all requests + my @requests = Koha::Illrequests->as_list; # Identify patrons & branches that # we're going to need and get them - my $to_fetch = {}; - $to_fetch->{patrons} = {} if $embed{patron}; - $to_fetch->{branches} = {} if $embed{library}; - $to_fetch->{capabilities} = {} if $embed{capabilities}; - foreach my $req(@{$requests}) { - $to_fetch->{patrons}->{$req->{borrowernumber}} = 1 if $embed{patron}; - $to_fetch->{branches}->{$req->{branchcode}} = 1 if $embed{library}; - $to_fetch->{capabilities}->{$req->{backend}} = 1 if $embed{capabilities}; + my $to_fetch = { + patrons => {}, + branches => {}, + capabilities => {} + }; + foreach my $req(@requests) { + $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron}; + $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library}; + $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities}; } # Fetch the patrons we need @@ -100,15 +102,14 @@ sub list { } } - # Now we've got all associated users and branches, # we can augment the request objects - foreach my $req(@{$requests}) { - my $r = Koha::Illrequests->new->find($req->{illrequest_id}); - $req->{id_prefix} = $r->id_prefix; + my @output = (); + foreach my $req(@requests) { + my $to_push = $req->unblessed; foreach my $p(@{$patron_arr}) { - if ($p->{borrowernumber} == $req->{borrowernumber}) { - $req->{patron} = { + if ($p->{borrowernumber} == $req->borrowernumber) { + $to_push->{patron} = { firstname => $p->{firstname}, surname => $p->{surname}, cardnumber => $p->{cardnumber} @@ -117,28 +118,29 @@ sub list { } } foreach my $b(@{$branch_arr}) { - if ($b->{branchcode} eq $req->{branchcode}) { - $req->{library} = $b; + if ($b->{branchcode} eq $req->branchcode) { + $to_push->{library} = $b; last; } } if ($embed{metadata}) { my $metadata = Koha::Illrequestattributes->search( - { illrequest_id => $req->{illrequest_id} }, + { illrequest_id => $req->illrequest_id }, { columns => [qw/type value/] } )->unblessed; my $meta_hash = {}; foreach my $meta(@{$metadata}) { $meta_hash->{$meta->{type}} = $meta->{value}; } - $req->{metadata} = $meta_hash; + $to_push->{metadata} = $meta_hash; } if ($embed{capabilities}) { - $req->{capabilities} = $to_fetch->{$req->{backend}}; + $to_push->{capabilities} = $to_fetch->{$req->backend}; } + push @output, $to_push; } - return $c->render( status => 200, openapi => $requests ); + return $c->render( status => 200, openapi => \@output ); } 1; -- 2.11.0