action/trigger aggregator script notify-csv
authorBill Erickson <berick@esilibrary.com>
Fri, 7 Dec 2012 20:05:31 +0000 (15:05 -0500)
committerBill Erickson <berick@esilibrary.com>
Fri, 7 Dec 2012 20:05:31 +0000 (15:05 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/support-scripts/action_trigger_aggregator.pl [new file with mode: 0755]
Open-ILS/src/support-scripts/csv_notify_pusher.pl [deleted file]

diff --git a/Open-ILS/src/support-scripts/action_trigger_aggregator.pl b/Open-ILS/src/support-scripts/action_trigger_aggregator.pl
new file mode 100755 (executable)
index 0000000..07f2316
--- /dev/null
@@ -0,0 +1,177 @@
+#!/usr/bin/perl
+# ---------------------------------------------------------------
+# Copyright (C) 2012 Equinox Software, Inc
+# Author: Bill Erickson <berick@esilibrary.com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# ---------------------------------------------------------------
+use strict; 
+use warnings;
+use DateTime;
+use Getopt::Long;
+use OpenSRF::System;
+use OpenSRF::AppSession;
+use OpenILS::Utils::CStoreEditor;
+use OpenILS::Utils::RemoteAccount;
+use OpenILS::Utils::Fieldmapper;
+
+my $osrf_config = '/openils/conf/opensrf_core.xml';
+my $start_date  = DateTime->now->strftime('%F');
+my $end_date    = '';
+my $event_defs  = '';
+my $output_file = '';
+my $local_dir   = '/tmp'; # where to keep local copies of generated files
+my $remote_acct = '';
+my $cleanup     = 0; # cleanup generated files
+my $verbose     = 0;
+my $help        = 0;
+
+GetOptions(
+    'osrf-config=s'     => \$osrf_config,
+    'start-date=s'      => \$start_date,
+    'end-date=s'        => \$end_date,
+    'event-defs=s'      => \$event_defs,
+    'output-file=s'     => \$output_file,
+    'remote-acct=s'     => \$remote_acct,
+    'local-dir=s'       => \$local_dir,
+    'cleanup'           => \$cleanup,
+    'verbose'           => \$verbose,
+    'help'              => \$help
+);
+
+sub help {
+    print <<HELP;
+
+Action/Trigger Aggregator Script
+
+Collect template output from one or more event-definitions and stitch the 
+results together in a single file.  The file may be optionally stored locally
+and/or delivered to a remote ftp/scp account.
+
+This script is useful for generating a single mass notification (e.g. overdue)
+file and sending the file to a 3rd party for processing and/or for local 
+processing.  The anticipated use case would be to stitch together lines of CSV 
+or chunks of XML.
+
+Example
+
+# Collect a week of notifications for 3 event definitions
+
+$0 \
+    --event-defs 104,105,106 \
+    --start-date 2012-01-01 \
+    --end-date 2012-01-07 \
+    --output-file 2012-01-07.notify.csv \
+    --local-dir /var/run/evergreen/csv \
+    --remote-account 6
+
+Options
+
+    --event-defs 
+        action_trigger.event_definition IDs
+    
+    --start-date 
+        Only collect output for events whose run_time is on or after this ISO date
+
+    --end-date 
+        Only collect output for events whose run_time occurred before this IDO date
+
+    --output-file [default STDOUT]
+        Output goes to this file.  
+
+    --local-dir [default /tmp]
+        Local directory where the output-file is placed.  If --cleanup is 
+        set, this is used as the tmp directory for file generation
+
+    --remote-account
+        Evergreen config.remote_account ID
+        If set, the output-file will be sent via sFTP/SCP to this server.
+
+HELP
+    exit;
+}
+
+help() if $help;
+
+my @event_defs = split(/,/, $event_defs);
+die "--event-defs required\n" unless @event_defs;
+
+my $local_file = $output_file ? "$local_dir/$output_file" : '&STDOUT';
+
+open(OUTFILE, ">$local_file") or 
+    die "unable to open out-file '$local_file' for writing: $!\n";
+binmode(OUTFILE, ":utf8");
+
+OpenSRF::System->bootstrap_client(config_file => $osrf_config);
+Fieldmapper->import(IDL => 
+    OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
+OpenILS::Utils::CStoreEditor::init();
+my $editor = OpenILS::Utils::CStoreEditor->new;
+
+my %date_filter;
+$date_filter{run_time} = {'>=' => $start_date} if $start_date;
+$date_filter{run_time} = {'<' => $end_date} if $end_date;
+
+# collect the event tempate output data
+# use a real session here so we can stream results directly to the output file
+my $ses = OpenSRF::AppSession->create('open-ils.cstore');
+my $req = $ses->request(
+    'open-ils.cstore.json_query', {
+        select => {ateo => ['data']},
+        from => {ateo => { atev => {
+            filter => {state => 'complete', %date_filter},
+            join => {atevdef => {filter => {
+                id => \@event_defs,
+                active => 't'
+            }}}
+        }}}
+    }
+);
+
+# use a large timeout since this is likely to be a hefty query
+while (my $resp = $req->recv(timeout => 3600)) {
+    die $req->failed . "\n" if $req->failed;
+    my $content = $resp->content or next;
+    print OUTFILE $content->{data};
+}
+
+if ($remote_acct) {
+    # send the file to the remote account
+
+    my $racct = $editor->retrieve_config_remote_account($remote_acct);
+    die "No such remote account $remote_acct" unless $racct;
+
+    my $type;
+    my $host = $racct->host;
+    ($host =~ s/^(S?FTP)://i and $type = uc($1)) or                   
+    ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
+
+    my $acct = OpenILS::Utils::RemoteAccount->new(
+        type            => $type,
+        account_object  => $racct,
+        local_file      => $local_file,
+        remote_file     => $output_file
+    );
+
+    my $res = $acct->put;
+
+    die "Unable to push to remote server [$remote_acct] : " . 
+        $acct->error . "\n" unless $res;
+
+    print "Pushed file to $res\n" if $verbose;
+}
+
+if ($cleanup) {
+    unlink($local_file) or 
+        die "Unable to clean up file '$local_file' : $!\n";
+}
+
+
diff --git a/Open-ILS/src/support-scripts/csv_notify_pusher.pl b/Open-ILS/src/support-scripts/csv_notify_pusher.pl
deleted file mode 100755 (executable)
index 91741e4..0000000
+++ /dev/null
@@ -1,145 +0,0 @@
-#!/usr/bin/perl
-use strict; 
-use warnings;
-use DateTime;
-use OpenSRF::AppSession;
-use OpenILS::Utils::CStoreEditor;
-use OpenILS::Utils::Cronscript;
-use OpenILS::Utils::RemoteAccount;
-
-my %defaults = (
-    'osrf-config=s'     => '/openils/conf/opensrf_core.xml',
-    'start-date=s'      => DateTime->now->strftime('%F'),
-    'end-date=s'        => '',
-    'event-defs=s'      => '',
-    'out-file=s'        => '',
-    'remote-account=s'  => 0,
-    'nolockfile'        => 1,
-    'verbose'           => 0,
-    'cleanup-file'      => 0 # remove output file when done
-);
-
-sub help {
-    print <<HELP;
-
-Collect CSV notification output and deliver to 3rd party
-
-# Collect a week of notifications
-
-$0 \
-    --event-def 104,105,106 \
-    --start-date 2012-01-01 \
-    --end-date 2012-01-07 \
-    --out-file /var/run/evergreen/csv/2012-01-07.notify.csv
-    --remote-account 6
-
-HELP
-    exit;
-}
-
-my $o = OpenILS::Utils::Cronscript->new(\%defaults);
-my $opts = $o->MyGetOptions();
-
-# TODO: skip cronscript's help
-help() if $opts->{help};
-
-my $verbose = $opts->{'verbose'};
-my $start_date = $opts->{'start-date'} || '';
-my $end_date = $opts->{'end-date'} || '';
-my $out_file = $opts->{'out-file'} || '&STDOUT';
-my $remote_account = $opts->{'remote-account'};
-
-my @event_defs = split(/,/, $opts->{'event-defs'});
-
-die "--event-defs required\n" unless @event_defs;
-
-if ($verbose) {
-    print "processing event_defs = @event_defs\n";
-    print "start_date = $start_date\n";
-    print "end_date = $end_date\n";
-    print "out_file = $out_file\n";
-}
-
-open(OUTFILE, ">$out_file") or 
-    die "unable to open out-file '$out_file' for writing: $!\n";
-
-binmode(OUTFILE, ":utf8");
-
-my %date_filter;
-$date_filter{run_time} = {'>=' => $start_date} if $start_date;
-$date_filter{run_time} = {'<' => $end_date} if $end_date;
-
-$o->bootstrap;
-
-my $ses = OpenSRF::AppSession->create('open-ils.cstore');
-my $req = $ses->request(
-    'open-ils.cstore.json_query', {
-        select => {ateo => ['data']},
-        from => {
-            ateo => {
-                atev => {
-                    filter => {
-                        state => 'complete', 
-                        %date_filter
-                    },
-                    join => {
-                        atevdef => {
-                            filter => {id => \@event_defs}
-                        }
-                    }
-                }
-            }
-        }
-    }
-);
-
-# header
-print OUTFILE '"Media Type","Language","Notice Type","Notification Level",';
-print OUTFILE '"Patron Number","Patron Title","Patron First Names","Patron Surname",';
-print OUTFILE '"Telephone Number","Email Address","Library Code","Site Code",';
-print OUTFILE '"Site Name","Item Barcode","Due Date","Item Title","Transaction ID"';
-print OUTFILE "\n";
-
-# dump the CSV chunk from each event output
-while (my $resp = $req->recv(timeout => 12000)) {
-    if ($req->failed) {
-        warn $req->failed;
-        last;
-    };
-    my $content = $resp->content or next;
-    print OUTFILE $content->{data};
-}
-
-# no output file?  nothing left to do
-exit unless $opts->{'out-file'};
-
-if ($remote_account) {
-
-    my $racct = $o->editor->retrieve_config_remote_account($remote_account);
-    die "No such remote account $remote_account" unless $racct;
-
-    my $type;
-    my $host = $racct->host;
-    ($host =~ s/^(S?FTP)://i and $type = uc($1)) or                   
-    ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
-
-    my $acct = OpenILS::Utils::RemoteAccount->new(
-        type            => $type,
-        account_object  => $racct,
-        local_file      => $out_file,
-        remote_file     => $out_file
-    );
-
-    my $res = $acct->put;
-
-    die "Unable to push to remote server [$remote_account] : " . 
-        $acct->error . "\n" unless $res;
-
-    print "Pushed file to $res\n" if $verbose;
-}
-
-if ($opts->{'cleanup-file'}) {
-    unlink($out_file) or die "Unable to clean up file '$out_file' : $!\n";
-}
-
-