From a74c1b26bd1dc8789472420a830f8b9270d8a623 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Wed, 8 Sep 2021 11:59:25 -0400 Subject: [PATCH] Bug 28974: Add pagination to holds queue viewer We have a partner processing thousands of holds per day, per branch. At this number of holds, the script loads very slow if it manages to load at all. Paginating the results of the holds queue will allow this page more flexible and load more quickly. Test Plan 1) Generate a lot of holds in the holds queue 2) Apply this patch 3) Try out the new pagination bar 4) Ensure the limit and page features function correctly Signed-off-by: Emmi Takkinen --- C4/HoldsQueue.pm | 39 +++++++++++++------ circ/view_holdsqueue.pl | 29 +++++++++++--- .../prog/en/modules/circ/view_holdsqueue.tt | 27 +++++++++++++ t/db_dependent/HoldsQueue.t | 36 ++++++++++------- 4 files changed, 102 insertions(+), 29 deletions(-) diff --git a/C4/HoldsQueue.pm b/C4/HoldsQueue.pm index 56e07cbbca1..ff0d45bb2cb 100644 --- a/C4/HoldsQueue.pm +++ b/C4/HoldsQueue.pm @@ -126,13 +126,15 @@ Returns hold queue for a holding branch. If branch is omitted, then whole queue sub GetHoldsQueueItems { my $params = shift; - my $dbh = C4::Context->dbh; + my $dbh = C4::Context->dbh; my $search_params; - $search_params->{'me.holdingbranch'} = $params->{branchlimit} if $params->{branchlimit}; - $search_params->{'itype'} = $params->{itemtypeslimit} if $params->{itemtypeslimit}; - $search_params->{'ccode'} = $params->{ccodeslimit} if $params->{ccodeslimit}; - $search_params->{'location'} = $params->{locationslimit} if $params->{locationslimit}; + $search_params->{'me.holdingbranch'} = $params->{branchlimit} if $params->{branchlimit}; + $search_params->{'itype'} = $params->{itemtypeslimit} if $params->{itemtypeslimit}; + $search_params->{'ccode'} = $params->{ccodeslimit} if $params->{ccodeslimit}; + $search_params->{'location'} = $params->{locationslimit} if $params->{locationslimit}; + my $rows = $params->{limit} || 20; + my $page = $params->{page} || 1; my $results = Koha::Hold::HoldsQueueItems->search( $search_params, @@ -143,20 +145,35 @@ sub GetHoldsQueueItems { prefetch => [ 'biblio', 'biblioitem', - { - 'item' => { - 'item_group_item' => 'item_group' - } - } + { 'item' => { 'item_group_item' => 'item_group' } } ], order_by => [ 'ccode', 'location', 'item.cn_sort', 'author', 'biblio.title', 'pickbranch', 'reservedate' ], + rows => $rows, + page => $page } ); + my $total_results = Koha::Hold::HoldsQueueItems->search( + $search_params, + { + join => [ + 'borrower', + ], + prefetch => [ + 'biblio', + 'biblioitem', + { 'item' => { 'item_group_item' => 'item_group' } } + ], + order_by => [ + 'ccode', 'location', 'item.cn_sort', 'author', + 'biblio.title', 'pickbranch', 'reservedate' + ], + } + )->count; - return $results; + return ( $results, $total_results ); } =head2 CreateQueue diff --git a/circ/view_holdsqueue.pl b/circ/view_holdsqueue.pl index 241697ab034..5de503c1579 100755 --- a/circ/view_holdsqueue.pl +++ b/circ/view_holdsqueue.pl @@ -25,7 +25,7 @@ This script displays items in the tmp_holdsqueue table use Modern::Perl; use CGI qw ( -utf8 ); use C4::Auth qw( get_template_and_user ); -use C4::Output qw( output_html_with_http_headers ); +use C4::Output qw( output_html_with_http_headers pagination_bar ); use C4::HoldsQueue qw( GetHoldsQueueItems ); use Koha::BiblioFrameworks; use Koha::ItemTypes; @@ -44,27 +44,46 @@ my $params = $query->Vars; my $run_report = $params->{'run_report'}; my $branchlimit = $params->{'branchlimit'}; my $itemtypeslimit = $params->{'itemtypeslimit'}; -my $ccodeslimit = $params->{'ccodeslimit'}; +my $ccodeslimit = $params->{'ccodeslimit'}; my $locationslimit = $params->{'locationslimit'}; +my $limit = $params->{'limit'} || 20; +my $page = $params->{'page'} || 1; if ($run_report) { - my $items = GetHoldsQueueItems( + my ( $items, $total ) = GetHoldsQueueItems( { branchlimit => $branchlimit, itemtypeslimit => $itemtypeslimit, ccodeslimit => $ccodeslimit, - locationslimit => $locationslimit + locationslimit => $locationslimit, + limit => $limit, + page => $page, } ); + my $pages = int( $total / $limit ) + ( ( $total % $limit ) > 0 ? 1 : 0 ); $template->param( branchlimit => $branchlimit, itemtypeslimit => $itemtypeslimit, ccodeslimit => $ccodeslimit, locationslimit => $locationslimit, - total => $items->count, + total => $total, itemsloop => $items, run_report => $run_report, + page => $page, + limit => $limit, + pagination_bar => pagination_bar( + 'view_holdsqueue.pl', + $pages, $page, 'page', + { + branchlimit => $branchlimit, + itemtypeslimit => $itemtypeslimit, + ccodeslimit => $ccodeslimit, + locationslimit => $locationslimit, + limit => $limit, + run_report => 1, + } + ), ); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/view_holdsqueue.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/view_holdsqueue.tt index 3a88c14e873..9315548348e 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/view_holdsqueue.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/view_holdsqueue.tt @@ -68,6 +68,29 @@ [% IF ( ccodeslimit ) %] and collection: [% AuthorisedValues.GetDescriptionByKohaField( kohafield => 'items.ccode' authorised_value = ccodeslimit ) | html %][% END %] [% IF ( locationslimit ) %] and shelving location: [% AuthorisedValues.GetDescriptionByKohaField( kohafield => 'items.location' authorised_value = locationslimit ) | html %][% END %] + +
+ + + + + + + + + +
+
[% pagination_bar | $raw %]
+ [% ELSE %]
No items found.
[% END %] @@ -310,6 +333,10 @@ $(document).ready(function() { var holdst; + $('#limit').change(function() { + $('#limitselect').submit(); + }); + // Setup filters before DataTables initialisation, in case some columns are // hidden by default var filterColumnTimeoutId; diff --git a/t/db_dependent/HoldsQueue.t b/t/db_dependent/HoldsQueue.t index 11bcad4156c..a1f7cb88708 100755 --- a/t/db_dependent/HoldsQueue.t +++ b/t/db_dependent/HoldsQueue.t @@ -163,7 +163,7 @@ $dbh->do("DELETE FROM items WHERE holdingbranch = '$borrower_branchcode'"); # Frst branch from StaticHoldsQueueWeight test_queue ('take from lowest cost branch', 0, $borrower_branchcode, $other_branches[0]); test_queue ('take from lowest cost branch', 1, $borrower_branchcode, $least_cost_branch_code); -my $queue = C4::HoldsQueue::GetHoldsQueueItems({ branchlimit => $least_cost_branch_code}) || []; +my ( $queue, undef ) = C4::HoldsQueue::GetHoldsQueueItems( { branchlimit => $least_cost_branch_code } ); my $queue_item = $queue->next; ok( $queue_item && $queue_item->pickbranch eq $borrower_branchcode @@ -1889,7 +1889,7 @@ subtest 'Remove holds on check-in match' => sub { }; subtest "GetHoldsQueueItems" => sub { - plan tests => 4; + plan tests => 8; $schema->storage->txn_begin; @@ -1943,27 +1943,37 @@ subtest "GetHoldsQueueItems" => sub { ($itemnumber_3,$biblionumber_3,'','','',42,'','') " ); - my $queue_items = GetHoldsQueueItems(); + my ( $queue_items, $queue_count ) = GetHoldsQueueItems(); is( $queue_items->count, $count + 3, 'Three items added to queue' ); + is( $queue_count, $count + 3, 'Correct count of items in queue' ); - $queue_items = GetHoldsQueueItems( { itemtypeslimit => $item_1->itype } ); - is( $queue_items->count, - 3, 'Three items of same itemtype found when itemtypeslimit passed' ); + ( $queue_items, $queue_count ) = GetHoldsQueueItems( { itemtypeslimit => $item_1->itype } ); + is( + $queue_items->count, + 3, 'Three items of same itemtype found when itemtypeslimit passed' + ); + is( $queue_count, $count + 3, 'Correct count of items in queue' ); - $queue_items = GetHoldsQueueItems( - { itemtypeslimit => $item_1->itype, ccodeslimit => $item_2->ccode } ); - is( $queue_items->count, - 2, 'Two items of same collection found when ccodeslimit passed' ); + ( $queue_items, $queue_count ) = + GetHoldsQueueItems( { itemtypeslimit => $item_1->itype, ccodeslimit => $item_2->ccode } ); + is( + $queue_items->count, + 2, 'Two items of same collection found when ccodeslimit passed' + ); + is( $queue_count, $count + 2, 'Correct count of items in queue' ); - $queue_items = GetHoldsQueueItems( + ( $queue_items, $queue_count ) = GetHoldsQueueItems( { itemtypeslimit => $item_1->itype, ccodeslimit => $item_2->ccode, locationslimit => $item_3->location } ); - is( scalar $queue_items->count, - 1, 'One item of shleving location found when locationslimit passed' ); + is( + scalar $queue_items->count, + 1, 'One item of shelving location found when locationslimit passed' + ); + is( $queue_count, $count + 1, 'Correct count of items in queue' ); $schema->storage->txn_rollback; }; -- 2.39.2