[Rt-commit] rt branch 5.0/combine-search-and-count created. rt-5.0.3-226-ga4635a203a

BPS Git Server git at git.bestpractical.com
Wed Jan 11 20:35:34 UTC 2023


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/combine-search-and-count has been created
        at  a4635a203a7596a22d169f025de4e391cd00e192 (commit)

- Log -----------------------------------------------------------------
commit a4635a203a7596a22d169f025de4e391cd00e192
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Jan 12 04:17:03 2023 +0800

    Combine search and count for saved searches on dashboards if possible

diff --git a/share/html/Elements/ShowSearch b/share/html/Elements/ShowSearch
index c6f4f4ea76..6bad808336 100644
--- a/share/html/Elements/ShowSearch
+++ b/share/html/Elements/ShowSearch
@@ -195,6 +195,23 @@ if ($ShowCount) {
     }
 
     $collection->FromSQL($query);
+    if ( $ProcessedSearchArg->{OrderBy} ) {
+        my @order_by;
+        my @order;
+        if ( $ProcessedSearchArg->{OrderBy} =~ /\|/ ) {
+            @order_by = split /\|/, $ProcessedSearchArg->{OrderBy};
+            @order    = split /\|/, $ProcessedSearchArg->{Order};
+        }
+        else {
+            @order_by = $ProcessedSearchArg->{OrderBy};
+            @order    = $ProcessedSearchArg->{Order};
+        }
+        @order_by = grep length, @order_by;
+        $collection->OrderByCols( map { { FIELD => $order_by[$_], ORDER => $order[$_] } } ( 0 .. $#order_by ) );
+    }
+    $collection->RowsPerPage( $ProcessedSearchArg->{Rows} ) if $ProcessedSearchArg->{Rows};
+    $collection->CombineSearchAndCount(1) if $collection->can('CombineSearchAndCount');
+
     my $count = $collection->CountAll();
 
     my $title;

commit ab06c8f651136f9b5b0f3ccae068f7610d06859f
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Aug 27 01:12:45 2022 +0800

    Combine search and count for search result pages if possible
    
    This commit also fixes the handling of $OrderBy on search result page to
    exclude empty values, to make combined SQL work: search result page
    calls CountAll only, of which SQL doesn't include the "ORDER BY ..."
    part, the issue wouldn't be exposed without combined SQL.
    
    To make sure the combined search returns the demanded resultset, we need
    to call CountAll after all setups like OrderByCols, RowsPerPage, etc.

diff --git a/share/html/Elements/CollectionList b/share/html/Elements/CollectionList
index 46c8fcccc7..7d99531dc7 100644
--- a/share/html/Elements/CollectionList
+++ b/share/html/Elements/CollectionList
@@ -66,19 +66,6 @@ elsif (!$Collection && $Class eq 'RT::Assets') {
 # dashboard mail can be suppressed if there are no results
 $$HasResults = 0 if $HasResults && !defined($$HasResults);
 
-$TotalFound = $Collection->CountAll() unless defined $TotalFound;
-return '' if !$TotalFound && !$ShowEmpty;
-
-if ( $Rows ) {
-    if ( $TotalFound <= $Rows ) {
-        $Page = 1;
-    }
-    else {
-        my $MaxPage = int( $TotalFound / $Rows ) + ( $TotalFound % $Rows ? 1 : 0 );
-        $Page = $MaxPage if $Page > $MaxPage;
-    }
-}
-
 # XXX: ->{'order_by'} is hacky, but there is no way to check if
 # collection is ordered or not
 if ( @OrderBy && ($AllowSorting || $PreferOrderBy || !$Collection->{'order_by'}) ) {
@@ -97,6 +84,24 @@ $Collection->RowsPerPage( $Rows ) if $Rows;
 $Page = 1 unless $Page && $Page > 0;    # workaround problems with $Page = '' or undef
 $Collection->GotoPage( $Page - 1 ); # SB uses page 0 as the first page
 
+$Collection->CombineSearchAndCount(1) if $Collection->can('CombineSearchAndCount');
+$TotalFound = $Collection->CountAll() unless defined $TotalFound;
+return '' if !$TotalFound && !$ShowEmpty;
+
+# Adjust page after CountAll, otherwise $TotalFound could be not defined.
+# On the other hand, CountAll needs page info if it's a combined search,
+# thus we have to set page first and then adjust it here.
+if ( $Rows ) {
+    if ( $TotalFound <= $Rows ) {
+        $Page = 1;
+    }
+    else {
+        my $MaxPage = int( $TotalFound / $Rows ) + ( $TotalFound % $Rows ? 1 : 0 );
+        $Page = $MaxPage if $Page > $MaxPage;
+    }
+}
+$Collection->GotoPage( $Page - 1 );
+
 # DisplayFormat lets us use a "temporary" format for display, while 
 # still using our original format for next/prev page links.
 # bulk update uses this feature to add checkboxes
diff --git a/share/html/Search/Results.html b/share/html/Search/Results.html
index 1d1639e60e..e843e83a44 100644
--- a/share/html/Search/Results.html
+++ b/share/html/Search/Results.html
@@ -62,6 +62,7 @@
 
 <& /Elements/CollectionList, 
     Query => $Query,
+    Collection => $session{$session_name},
     TotalFound => $count,
     AllowSorting => 1,
     AllowFiltering => 1,
@@ -200,13 +201,14 @@ if ($OrderBy =~ /\|/) {
     my @OrderBy = split /\|/,$OrderBy;
     my @Order = split /\|/,$Order;
     $session{$session_name}->OrderByCols(
-        map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } } ( 0
-        .. $#OrderBy ) );; 
+        map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } }
+        grep { $OrderBy[$_] } ( 0 .. $#OrderBy ) );
 } else {
     $session{$session_name}->OrderBy(FIELD => $OrderBy, ORDER => $Order); 
 }
 $session{$session_name}->RowsPerPage( $Rows ) if $Rows;
 $session{$session_name}->GotoPage( $Page - 1 );
+$session{$session_name}->CombineSearchAndCount(1) if $session{$session_name}->can('CombineSearchAndCount');
 
 $session{$hash_name} = {
     Format      => $Format,

-----------------------------------------------------------------------


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list