]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/action_trigger_aggregator.pl
Action/Trigger template output aggregator
[Evergreen.git] / Open-ILS / src / support-scripts / action_trigger_aggregator.pl
1 #!/usr/bin/perl
2 # ---------------------------------------------------------------
3 # Copyright (C) 2012 Equinox Software, Inc
4 # Author: Bill Erickson <berick@esilibrary.com>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------
16 use strict; 
17 use warnings;
18 use DateTime;
19 use Getopt::Long;
20 use OpenSRF::System;
21 use OpenSRF::AppSession;
22 use OpenILS::Utils::CStoreEditor;
23 use OpenILS::Utils::RemoteAccount;
24 use OpenILS::Utils::Fieldmapper;
25
26 my $osrf_config = '/openils/conf/opensrf_core.xml';
27 my $start_date  = DateTime->now->strftime('%F');
28 my $end_date    = '';
29 my $event_defs  = '';
30 my $output_file = '';
31 my $local_dir   = '/tmp'; # where to keep local copies of generated files
32 my $remote_acct = '';
33 my $cleanup     = 0; # cleanup generated files
34 my $verbose     = 0;
35 my $help        = 0;
36
37 GetOptions(
38     'osrf-config=s'     => \$osrf_config,
39     'start-date=s'      => \$start_date,
40     'end-date=s'        => \$end_date,
41     'event-defs=s'      => \$event_defs,
42     'output-file=s'     => \$output_file,
43     'remote-acct=s'     => \$remote_acct,
44     'local-dir=s'       => \$local_dir,
45     'cleanup'           => \$cleanup,
46     'verbose'           => \$verbose,
47     'help'              => \$help
48 );
49
50 sub help {
51     print <<HELP;
52
53 Action/Trigger Aggregator Script
54
55 Collect template output from one or more event-definitions and stitch the 
56 results together in a single file.  The file may be optionally stored locally
57 and/or delivered to a remote ftp/scp account.
58
59 This script is useful for generating a single mass notification (e.g. overdue)
60 file and sending the file to a 3rd party for processing and/or for local 
61 processing.  The anticipated use case would be to stitch together lines of CSV 
62 or chunks of XML.
63
64 Example
65
66 # Collect a week of notifications for 3 event definitions
67
68 $0 \
69     --event-defs 104,105,106 \
70     --start-date 2012-01-01 \
71     --end-date 2012-01-07 \
72     --output-file 2012-01-07.notify.csv \
73     --local-dir /var/run/evergreen/csv \
74     --remote-account 6
75
76 Options
77
78     --event-defs 
79         action_trigger.event_definition IDs
80     
81     --start-date 
82         Only collect output for events whose run_time is on or after this ISO date
83
84     --end-date 
85         Only collect output for events whose run_time occurred before this IDO date
86
87     --output-file [default STDOUT]
88         Output goes to this file.  
89
90     --local-dir [default /tmp]
91         Local directory where the output-file is placed.  If --cleanup is 
92         set, this is used as the tmp directory for file generation
93
94     --remote-account
95         Evergreen config.remote_account ID
96         If set, the output-file will be sent via sFTP/SCP to this server.
97
98 HELP
99     exit;
100 }
101
102 help() if $help;
103
104 my @event_defs = split(/,/, $event_defs);
105 die "--event-defs required\n" unless @event_defs;
106
107 my $local_file = $output_file ? "$local_dir/$output_file" : '&STDOUT';
108
109 open(OUTFILE, ">$local_file") or 
110     die "unable to open out-file '$local_file' for writing: $!\n";
111 binmode(OUTFILE, ":utf8");
112
113 OpenSRF::System->bootstrap_client(config_file => $osrf_config);
114 Fieldmapper->import(IDL => 
115     OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
116 OpenILS::Utils::CStoreEditor::init();
117 my $editor = OpenILS::Utils::CStoreEditor->new;
118
119 my %date_filter;
120 $date_filter{run_time} = {'>=' => $start_date} if $start_date;
121 $date_filter{run_time} = {'<' => $end_date} if $end_date;
122
123 # collect the event tempate output data
124 # use a real session here so we can stream results directly to the output file
125 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
126 my $req = $ses->request(
127     'open-ils.cstore.json_query', {
128         select => {ateo => ['data']},
129         from => {ateo => { atev => {
130             filter => {state => 'complete', %date_filter},
131             join => {atevdef => {filter => {
132                 id => \@event_defs,
133                 active => 't'
134             }}}
135         }}}
136     }
137 );
138
139 # use a large timeout since this is likely to be a hefty query
140 while (my $resp = $req->recv(timeout => 3600)) {
141     die $req->failed . "\n" if $req->failed;
142     my $content = $resp->content or next;
143     print OUTFILE $content->{data};
144 }
145
146 if ($remote_acct) {
147     # send the file to the remote account
148
149     my $racct = $editor->retrieve_config_remote_account($remote_acct);
150     die "No such remote account $remote_acct" unless $racct;
151
152     my $type;
153     my $host = $racct->host;
154     ($host =~ s/^(S?FTP)://i and $type = uc($1)) or                   
155     ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
156     $host =~ s#//##;
157
158     my $acct = OpenILS::Utils::RemoteAccount->new(
159         type            => $type,
160         remote_host     => $host,
161         account_object  => $racct,
162         local_file      => $local_file,
163         remote_file     => $output_file
164     );
165
166     my $res = $acct->put;
167
168     die "Unable to push to remote server [$remote_acct] : " . 
169         $acct->error . "\n" unless $res;
170
171     print "Pushed file to $res\n" if $verbose;
172 }
173
174 if ($cleanup) {
175     unlink($local_file) or 
176         die "Unable to clean up file '$local_file' : $!\n";
177 }
178
179