From 89559c0af7ea86a2d0c1598c16b6c9a00631ac90 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 | 78 +++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/Koha/Illbackend.pm b/Koha/Illbackend.pm index 8a6be8a7a6..b51d665b97 100644 --- a/Koha/Illbackend.pm +++ b/Koha/Illbackend.pm @@ -48,35 +48,63 @@ 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( - {backend => $backend_id}, - # { - # columns => [ qw/status/ ], - # group_by => [ qw/status/ ], - # } - ); - my @data; - while (my $request = $ill_requests->next) { - my $status_data = $request->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}) : (), - } - } + + # 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/], + } + ); + + while (my $backend_existing_status = $backend_existing_statuses->next) { + + my $illrequest = + Koha::Illrequests->search( + { backend => $backend_id, status => $backend_existing_status->status } + )->last; + + my $status_data = $illrequest->strings_map; + + if ( $status_data->{status} ) { + push @data, { + $status_data->{status}->{str} ? ( str => $status_data->{status}->{str} ) + : $status_data->{status}->{code} ? ( str => $status_data->{status}->{code} ) + : (), + $status_data->{status}->{code} ? ( code => $status_data->{status}->{code} ) : (), + }; } } - # Remove duplicate statuses - my %seen; - @data = grep { my $e = $_; my $key = join '___', map { $e->{$_}; } sort keys %$_;!$seen{$key}++ } @data; + # Fetch all existing status_alias for the provided backend + my $backend_existing_status_aliases = Koha::Illrequests->search( + { backend => $backend_id }, + { + columns => [qw/status_alias/], + group_by => [qw/status_alias/], + } + ); + + while ( my $backend_existing_status_alias = $backend_existing_status_aliases->next ) { + + my $illrequest = + Koha::Illrequests->search( + { backend => $backend_id, status_alias => $backend_existing_status_alias->status_alias } ) + ->last; + + my $status_data = $illrequest->strings_map; + + if ( $status_data->{status_alias} ) { + push @data, { + $status_data->{status_alias}->{str} ? ( str => $status_data->{status_alias}->{str} ) + : $status_data->{status_alias}->{code} ? ( str => $status_data->{status_alias}->{code} ) + : (), + $status_data->{status_alias}->{code} ? ( code => $status_data->{status_alias}->{code} ) : (), + }; + } + } return \@data; } -- 2.30.2