From: Bill Erickson Date: Thu, 9 Jun 2016 16:55:10 +0000 (-0400) Subject: hold targeter reify experiment X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=8df2b1f19595789e9c4f821155cac2c79dc742ba;p=working%2FEvergreen.git hold targeter reify experiment Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Utils/HoldTargeter.pm b/Open-ILS/src/perlmods/lib/OpenILS/Utils/HoldTargeter.pm index 018d510e94..dab21d9381 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Utils/HoldTargeter.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Utils/HoldTargeter.pm @@ -6,6 +6,7 @@ use OpenSRF::AppSession; use OpenSRF::Utils::Logger qw(:logger); use OpenILS::Application::AppUtils; use OpenILS::Utils::CStoreEditor qw/:funcs/; +use OpenSRF::Utils qw/:datetime/; # WIP notes: # avoid 'duplicate key value violates unique constraint "copy_once_per_hold"' @@ -23,6 +24,11 @@ sub new { return bless($self, $class); } +sub editor { + my $self = shift; + return $self->{editor}; +} + sub init { my $self = shift; @@ -59,6 +65,50 @@ sub target_hold { return $targeter->result; } +# Targets all holds whose prev_check_time is older than the provide interval. +# Also targets all holds that have never been targeted. +sub target_all { + my $self = shift; + my @responses; + push(@responses, $self->target_hold($_)) for $self->collect_hold_ids; + return \@responses; +} + +sub collect_hold_ids { + my $self = shift; + + $self->{retarget_interval} ||= '12h'; + + my $date = DateTime->now->subtract( + seconds => interval_to_seconds($self->{retarget_interval})); + + my $rtime_sort = $self->{newest_first} ? 'DESC' : 'ASC'; + + my $query = { + select => {ahr => ['id']}, + from => 'ahr', + where => { + capture_time => undef, + fulfillment_time => undef, + frozen => 'f', + cancel_time => undef, + '-or' => [ + {prev_check_time => undef}, + {prev_check_time => {'<=' => $date->strftime('%F %T%z')}} + ] + }, + order_by => [ + {class => 'ahr', field => 'selection_depth', direction => 'DESC'}, + {class => 'ahr', field => 'request_time', direction => $rtime_sort}, + {class => 'ahr', field => 'prev_check_time'} + ] + }; + + my $holds = $self->editor->json_query($query); + + return map {$_->{id}} @$holds; +} + # ----------------------------------------------------------------------- # Knows how to target a single hold. @@ -824,10 +874,13 @@ sub target { my $hold = $e->retrieve_action_hold_request($hold_id) or return $self->exit_targeter("No hold found", 1); - $self->hold($hold); - return $self->exit_targeter("Hold is not eligible for targeting") - if $hold->capture_time || $hold->cancel_time; + if $hold->capture_time || + $hold->cancel_time || + $hold->fulfillment_time || + $U->is_true($hold->frozen); + + $self->hold($hold); return unless $self->handle_expired_hold; return unless $self->get_hold_copies; diff --git a/Open-ILS/src/support-scripts/test-scripts/hold_targeter.pl b/Open-ILS/src/support-scripts/test-scripts/hold_targeter.pl index 87367d92ea..afb3516dd5 100755 --- a/Open-ILS/src/support-scripts/test-scripts/hold_targeter.pl +++ b/Open-ILS/src/support-scripts/test-scripts/hold_targeter.pl @@ -13,11 +13,9 @@ use Data::Dumper; my $config = shift; # path to opensrf_core.xml osrf_connect($config); # connect to jabber -my $hold_id = shift; - my $targeter = OpenILS::Utils::HoldTargeter->new; -$targeter->init; -my $resp = $targeter->target_hold($hold_id); - -print Dumper($resp); +$targeter->{retarget_interval} = '6h'; +my $responses = $targeter->target_all; +#print Dumper($responses); +print "Processed " . scalar(@$responses) . " holds\n";