LP#1549505: Assume all orgs when none supplied for sorting on popularity
authorMike Rylander <mrylander@gmail.com>
Mon, 25 Jan 2016 15:41:46 +0000 (10:41 -0500)
committerGalen Charlton <gmc@esilibrary.com>
Wed, 24 Feb 2016 22:38:17 +0000 (17:38 -0500)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm

index 3af02b5..6ff8db8 100644 (file)
@@ -854,15 +854,25 @@ sub toSQL {
                     )
                 JOIN
                 if ($sort_filter eq 'poprel') {
-                    $rank = $rel . ' * (1 + AVG(COALESCE(rbs.value::NUMERIC,0.0)) / 5.0)';
+                    $rank = '(' . $rel . ') * (1.0 + AVG(COALESCE(rbs.value::NUMERIC,0.0)) / 5.0)';
                 } else {
                     $rank = 'AVG(COALESCE(rbs.value::NUMERIC,0.0))';
                 }
+            } else { # no numeric badge_orgs filter arguments ... bad input, assume all orgs
+                $pop_join = 'LEFT JOIN rating.record_badge_score rbs ON ( m.source = rbs.record )';
+                if ($sort_filter eq 'poprel') {
+                    $rank = '(' . $rel . ') * (1.0 + AVG(COALESCE(rbs.value::NUMERIC,0.0)) / 5.0)';
+                } else {
+                    $rank = 'AVG(COALESCE(rbs.value::NUMERIC,0.0))';
+                }
+            }
+        } else { # no badge_orgs filter supplied, assume all orgs
+            $pop_join = 'LEFT JOIN rating.record_badge_score rbs ON ( m.source = rbs.record )';
+            if ($sort_filter eq 'poprel') {
+                $rank = '(' . $rel . ') * (1.0 + AVG(COALESCE(rbs.value::NUMERIC,0.0)) / 5.0)';
             } else {
-                $rank = $rel;
+                $rank = 'AVG(COALESCE(rbs.value::NUMERIC,0.0))';
             }
-        } else {
-            $rank = $rel;
         }
     } else {
         # default to rel ranking
index 13886a6..634fd7b 100644 (file)
@@ -3098,6 +3098,8 @@ __PACKAGE__->register_method(
     cachable    => 1,
 );
 
+my $top_org;
+
 sub query_parser_fts_wrapper {
     my $self = shift;
     my $client = shift;
@@ -3114,6 +3116,8 @@ sub query_parser_fts_wrapper {
         die "No search arguments were passed to ".$self->api_name;
     }
 
+    $top_org ||= actor::org_unit->search( { parent_ou => undef } )->next;
+
     $log->debug("Constructing QueryParser query from staged search hash ...", DEBUG);
     my $base_query = '';
     for my $sclass ( keys %{$args{searches}} ) {
@@ -3145,44 +3149,41 @@ sub query_parser_fts_wrapper {
 
 
     my $borgs = undef;
+    my $site = undef;
+
     my ($site_filter) = $base_plan->parse_tree->find_filter('site');
     if ($site_filter && @{$site_filter->args}) {
-        my $cstore = OpenSRF::AppSession->create( 'open-ils.cstore' );
-        my $site;
-        $site = $cstore->request(
-            'open-ils.cstore.direct.actor.org_unit.search',
-            { parent_ou => undef }
-        )->gather(1) if ($site_filter->args->[0] eq '-');
-
-        $site = $cstore->request(
-            'open-ils.cstore.direct.actor.org_unit.search',
-            { shortname => $site_filter->args->[0] }
-        )->gather(1) unless ($site);
-
-        $site = $cstore->request(
-            'open-ils.cstore.direct.actor.org_unit.retrieve',
-            $args{org_unit}
-        )->gather(1) if (!$site and $args{org_unit});
-
-        if ($site) {
-            my ($depth_filter) = $base_plan->parse_tree->find_filter('depth');
-            if ($depth_filter && $depth_filter->args->[0] =~ /^\d+$/) {
-                $borgs = $cstore->request(
-                    'open-ils.cstore.json_query.atomic',
-                    { from => [ 'actor.org_unit_descendants', $site->id, $depth_filter->args->[0] ] }
-                )->gather(1);
-            } else {
-                $borgs = $cstore->request(
-                    'open-ils.cstore.json_query.atomic',
-                    { from => [ 'actor.org_unit_descendants', $site->id ] }
-                )->gather(1);
-            }
+        $site = $top_org if ($site_filter->args->[0] eq '-');
+        $site = $top_org if ($site_filter->args->[0] eq $top_org->shortname);
+        $site = actor::org_unit->search( { shortname => $site_filter->args->[0] })->next unless ($site);
+
+    } elsif ($args{org_unit}) {
+        $site = $top_org if ($args{org_unit} eq '-');
+        $site = $top_org if ($args{org_unit} eq $top_org->shortname);
+        $site = actor::org_unit->search( { shortname => $args{org_unit} })->next unless ($site);
+    }
+
+    if ($site && $site->id <> $top_org->id) {
+        my ($depth_filter) = $base_plan->parse_tree->find_filter('depth');
+        my $depth = $depth_filter->args->[0] if ($depth_filter && $depth_filter->args->[0] =~ /^\d+$/);
+        $depth = $args{depth} if (!defined($depth) && defined($args{depth}) && $args{depth} =~ /^\d+$/);
+
+        if (defined $depth and $depth > 0) { # Not scoped to the top of the tree
+            $borgs = OpenSRF::AppSession->create( 'open-ils.cstore' )->request(
+                'open-ils.cstore.json_query.atomic',
+                { from => [ 'actor.org_unit_descendants', $site->id, $depth ] }
+            )->gather(1);
+        } else {
+            $borgs = OpenSRF::AppSession->create( 'open-ils.cstore' )->request(
+                'open-ils.cstore.json_query.atomic',
+                { from => [ 'actor.org_unit_descendants', $site->id ] }
+            )->gather(1);
+        }
 
-            if (ref $borgs && @$borgs) {
-                $borgs = join(',', map { $_->{'actor.org_unit_descendants'} } @$borgs);
-            } else {
-                $borgs = undef;
-            }
+        if (ref $borgs && @$borgs) {
+            $borgs = join(',', map { $_->{'actor.org_unit_descendants'} } @$borgs);
+        } else {
+            $borgs = undef;
         }
     }