From 910eb995e8afda7c5bb299b6f631bf8145681513 Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Mon, 25 Jan 2016 10:41:46 -0500 Subject: [PATCH] LP#1549505: Assume all orgs when none supplied for sorting on popularity Signed-off-by: Mike Rylander Signed-off-by: Galen Charlton --- .../Application/Storage/Driver/Pg/QueryParser.pm | 18 ++++-- .../Application/Storage/Publisher/metabib.pm | 71 +++++++++++----------- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm index 3af02b55de..6ff8db8f6a 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm @@ -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 diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm index 13886a6070..634fd7bdef 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm @@ -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; } } -- 2.11.0