Bug 20996: Further enhance performance
authorAndrew Isherwood <andrew.isherwood@ptfs-europe.com>
Thu, 5 Jul 2018 13:46:16 +0000 (14:46 +0100)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 2 Nov 2018 10:33:04 +0000 (10:33 +0000)
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 <magnus@libriotech.no>
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Koha/REST/V1/Illrequests.pm

index 59e3f82..0a6dee0 100644 (file)
@@ -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;