hold targeter reify experiment
authorBill Erickson <berickxx@gmail.com>
Thu, 9 Jun 2016 16:55:10 +0000 (12:55 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 9 Jun 2016 16:55:10 +0000 (12:55 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Utils/HoldTargeter.pm
Open-ILS/src/support-scripts/test-scripts/hold_targeter.pl

index 018d510..dab21d9 100644 (file)
@@ -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;
index 87367d9..afb3516 100755 (executable)
@@ -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";