]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/action_trigger_aggregator.pl
9da5f6fd9c5bdab8b07dc4a9fc1f847e459b8db8
[working/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 $granularity = '';
31 my $output_file = '';
32 my $local_dir   = '/tmp'; # where to keep local copies of generated files
33 my $remote_acct = '';
34 my $cleanup     = 0; # cleanup generated files
35 my $verbose     = 0;
36 my $help        = 0;
37
38 GetOptions(
39     'osrf-config=s'     => \$osrf_config,
40     'start-date=s'      => \$start_date,
41     'end-date=s'        => \$end_date,
42     'event-defs=s'      => \$event_defs,
43     'granularity=s'     => \$granularity,
44     'output-file=s'     => \$output_file,
45     'remote-acct=s'     => \$remote_acct,
46     'local-dir=s'       => \$local_dir,
47     'cleanup'           => \$cleanup,
48     'verbose'           => \$verbose,
49     'help'              => \$help
50 );
51
52 sub help {
53     print <<HELP;
54
55 Action/Trigger Aggregator Script
56
57 Collect template output from one or more event-definitions and stitch the 
58 results together in a single file.  The file may be optionally stored locally
59 and/or delivered to a remote ftp/scp account.
60
61 This script is useful for generating a single mass notification (e.g. overdue)
62 file and sending the file to a 3rd party for processing and/or for local 
63 processing.  The anticipated use case would be to stitch together lines of CSV 
64 or chunks of XML.
65
66 Example
67
68 # Collect a week of notifications for 3 event definitions
69
70 $0 \
71     --event-defs 104,105,106 \
72     --start-date 2012-01-01 \
73     --end-date 2012-01-07 \
74     --output-file 2012-01-07.notify.csv \
75     --local-dir /var/run/evergreen/csv \
76     --remote-account 6
77
78 Options
79
80     --cleanup
81         Remove the local file after script is done.
82
83     --event-defs 
84         action_trigger.event_definition IDs to include
85
86     --granularity
87         Process all event definitions that match this granularity.  If used in
88         conjunction with --event-defs, the union of the two sets is used.
89     
90     --start-date 
91         Only collect output for events whose run_time is on or after this ISO date
92
93     --end-date 
94         Only collect output for events whose run_time occurred before this ISO date
95
96     --osrf-config
97         To set the OpenSRF config to something other than the default of
98         '/openils/conf/opensrf_core.xml'
99
100     --output-file [default STDOUT]
101         Output goes to this file.  
102
103     --local-dir [default /tmp]
104         Local directory where the output-file is placed.  If --cleanup is 
105         set, this is used as the tmp directory for file generation
106
107     --remote-account
108         Evergreen config.remote_account ID
109         If set, the output-file will be sent via sFTP/SCP to this server.
110
111     --verbose
112         Show more information about what the script is doing.
113
114     --help
115         Show command usage information.
116
117 HELP
118     exit;
119 }
120
121 help() if $help;
122
123 my @event_defs = split(/,/, $event_defs);
124 die "--event-defs or --granularity required\n" 
125     unless @event_defs or $granularity;
126
127 my $local_file = $output_file ? "$local_dir/$output_file" : '&STDOUT';
128
129 open(OUTFILE, ">$local_file") or 
130     die "unable to open out-file '$local_file' for writing: $!\n";
131 binmode(OUTFILE, ":utf8");
132
133 print "Output will be written to $local_file\n" if $verbose;
134
135 OpenSRF::System->bootstrap_client(config_file => $osrf_config);
136 Fieldmapper->import(IDL => 
137     OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
138 OpenILS::Utils::CStoreEditor::init();
139 my $editor = OpenILS::Utils::CStoreEditor->new;
140
141 # if granularity is set, append all event defs with the 
142 # selected granularity to the set of event-defs to process.
143 if ($granularity) {
144     my $defs = $editor->search_action_trigger_event_definition(
145         {granularity => $granularity},
146         {idlist => 1}
147     );
148
149     for my $id (@$defs) {
150         push(@event_defs, $id) 
151             unless grep { $_ eq $id} @event_defs;
152     }
153 }
154
155 print "Processing event-defs @event_defs\n" if $verbose;
156
157 my %date_filter;
158 $date_filter{run_time} = {'>=' => $start_date} if $start_date;
159
160 if ($end_date) {
161     if ($date_filter{run_time}) {
162         # both filters requested, -and them together
163         $date_filter{'-and'} = [
164             {run_time => delete $date_filter{run_time}},
165             {run_time => {'<' => $end_date}}
166         ];
167     } else {
168         $date_filter{run_time} = {'<' => $end_date};
169     }
170 }
171                 
172
173 # collect the event tempate output data
174 # use a real session here so we can stream results directly to the output file
175 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
176 my $req = $ses->request(
177     'open-ils.cstore.json_query', {
178         select => {ateo => [
179             # ateo's may be linked to multiple atev's; select distinct.
180             {column => 'id', transform => 'distinct'}, 'data']},
181         from => {ateo => { atev => {
182             filter => {state => 'complete', %date_filter},
183             join => {atevdef => {filter => {
184                 id => \@event_defs,
185                 active => 't'
186             }}}
187         }}}
188     }
189 );
190
191 # use a large timeout since this is likely to be a hefty query
192 while (my $resp = $req->recv(timeout => 3600)) {
193     die $req->failed . "\n" if $req->failed;
194     my $content = $resp->content or next;
195     print OUTFILE $content->{data};
196 }
197
198 if ($remote_acct) {
199     # send the file to the remote account
200
201     my $racct = $editor->retrieve_config_remote_account($remote_acct);
202     die "No such remote account $remote_acct" unless $racct;
203
204     my $type;
205     my $host = $racct->host;
206     ($host =~ s/^(S?FTP)://i and $type = uc($1)) or                   
207     ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
208     $host =~ s#//##;
209
210     my $acct = OpenILS::Utils::RemoteAccount->new(
211         type            => $type,
212         remote_host     => $host,
213         account_object  => $racct,
214         local_file      => $local_file,
215         remote_file     => $output_file
216     );
217
218     my $res = $acct->put;
219
220     die "Unable to push to remote server [$remote_acct] : " . 
221         $acct->error . "\n" unless $res;
222
223     print "Pushed file to $res\n" if $verbose;
224 }
225
226 if ($cleanup) {
227     unlink($local_file) or 
228         die "Unable to clean up file '$local_file' : $!\n";
229 }
230
231