From 0ac31458b390458d3f2fad9dfdfe1a518064b604 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 27 Jul 2023 09:40:03 +0000 Subject: [PATCH] Bug 34223: Fix filter performance We are now only fetching the existing statuses for the provided backend. Then, we fetch 1 illrequest object to make use of ->strings_map. Before, we were fetching all illrequests in the database and removing redundant afterwards --- Koha/Illbackend.pm | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Koha/Illbackend.pm b/Koha/Illbackend.pm index 8a6be8a7a6..7164edf42f 100644 --- a/Koha/Illbackend.pm +++ b/Koha/Illbackend.pm @@ -48,36 +48,37 @@ Return a list of existing ILL statuses sub existing_statuses { my ( $self, $backend_id ) = @_; - #FIXME: Currently fetching all requests, it'd be great if we could fetch distinct(status). - # Even doing it with distinct status, we need the ILL request object, so that strings_map works and - # the ILL request returns the correct status and info respective to its backend. - my $ill_requests = Koha::Illrequests->search( + # Fetch all existing statuses for the provided backend + my $backend_existing_statuses = Koha::Illrequests->search( {backend => $backend_id}, - # { - # columns => [ qw/status/ ], - # group_by => [ qw/status/ ], - # } + { + columns => [ qw/status/ ], + group_by => [ qw/status/ ], + } ); my @data; - while (my $request = $ill_requests->next) { - my $status_data = $request->strings_map; + while (my $backend_existing_status = $backend_existing_statuses->next) { + + my $illrequest = + Koha::Illrequests->search( + { backend => $backend_id, status => $backend_existing_status->status } + )->last; - foreach my $status_class ( qw(status_alias status) ){ - if ($status_data->{$status_class}){ + my $status_data = $illrequest->strings_map; + + foreach my $status_class (qw(status_alias status)) { + if ( $status_data->{$status_class} ) { push @data, { - $status_data->{$status_class}->{str} ? (str => $status_data->{$status_class}->{str}) : - $status_data->{$status_class}->{code} ? (str => $status_data->{$status_class}->{code}) : (), - $status_data->{$status_class}->{code} ? (code => $status_data->{$status_class}->{code}) : (), - } + $status_data->{$status_class}->{str} ? ( str => $status_data->{$status_class}->{str} ) + : $status_data->{$status_class}->{code} ? ( str => $status_data->{$status_class}->{code} ) + : (), + $status_data->{$status_class}->{code} ? ( code => $status_data->{$status_class}->{code} ) : (), + }; } } } - # Remove duplicate statuses - my %seen; - @data = grep { my $e = $_; my $key = join '___', map { $e->{$_}; } sort keys %$_;!$seen{$key}++ } @data; - return \@data; } -- 2.30.2