]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/action_trigger_aggregator.pl
LP#1261486 Action/trigger aggregator script repairs
[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     --event-defs 
81         action_trigger.event_definition IDs to include
82
83     --granularity
84         Process all event definitions that match this granularity.  If used in
85         conjunction with --event-defs, the union of the two sets is used.
86     
87     --start-date 
88         Only collect output for events whose run_time is on or after this ISO date
89
90     --end-date 
91         Only collect output for events whose run_time occurred before this IDO date
92
93     --output-file [default STDOUT]
94         Output goes to this file.  
95
96     --local-dir [default /tmp]
97         Local directory where the output-file is placed.  If --cleanup is 
98         set, this is used as the tmp directory for file generation
99
100     --remote-account
101         Evergreen config.remote_account ID
102         If set, the output-file will be sent via sFTP/SCP to this server.
103
104 HELP
105     exit;
106 }
107
108 help() if $help;
109
110 my @event_defs = split(/,/, $event_defs);
111 die "--event-defs or --granularity required\n" 
112     unless @event_defs or $granularity;
113
114 my $local_file = $output_file ? "$local_dir/$output_file" : '&STDOUT';
115
116 open(OUTFILE, ">$local_file") or 
117     die "unable to open out-file '$local_file' for writing: $!\n";
118 binmode(OUTFILE, ":utf8");
119
120 print "Output will be written to $local_file\n" if $verbose;
121
122 OpenSRF::System->bootstrap_client(config_file => $osrf_config);
123 Fieldmapper->import(IDL => 
124     OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
125 OpenILS::Utils::CStoreEditor::init();
126 my $editor = OpenILS::Utils::CStoreEditor->new;
127
128 # if granularity is set, append all event defs with the 
129 # selected granularity to the set of event-defs to process.
130 if ($granularity) {
131     my $defs = $editor->search_action_trigger_event_definition(
132         {granularity => $granularity},
133         {idlist => 1}
134     );
135
136     for my $id (@$defs) {
137         push(@event_defs, $id) 
138             unless grep { $_ eq $id} @event_defs;
139     }
140 }
141
142 print "Processing event-defs @event_defs\n" if $verbose;
143
144 my %date_filter;
145 $date_filter{run_time} = {'>=' => $start_date} if $start_date;
146
147 if ($end_date) {
148     if ($date_filter{run_time}) {
149         # both filters requested, -and them together
150         $date_filter{'-and'} = [
151             {run_time => delete $date_filter{run_time}},
152             {run_time => {'<' => $end_date}}
153         ];
154     } else {
155         $date_filter{run_time} = {'<' => $end_date};
156     }
157 }
158                 
159
160 # collect the event tempate output data
161 # use a real session here so we can stream results directly to the output file
162 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
163 my $req = $ses->request(
164     'open-ils.cstore.json_query', {
165         select => {ateo => [
166             # ateo's may be linked to multiple atev's; select distinct.
167             {column => 'id', transform => 'distinct'}, 'data']},
168         from => {ateo => { atev => {
169             filter => {state => 'complete', %date_filter},
170             join => {atevdef => {filter => {
171                 id => \@event_defs,
172                 active => 't'
173             }}}
174         }}}
175     }
176 );
177
178 # use a large timeout since this is likely to be a hefty query
179 while (my $resp = $req->recv(timeout => 3600)) {
180     die $req->failed . "\n" if $req->failed;
181     my $content = $resp->content or next;
182     print OUTFILE $content->{data};
183 }
184
185 if ($remote_acct) {
186     # send the file to the remote account
187
188     my $racct = $editor->retrieve_config_remote_account($remote_acct);
189     die "No such remote account $remote_acct" unless $racct;
190
191     my $type;
192     my $host = $racct->host;
193     ($host =~ s/^(S?FTP)://i and $type = uc($1)) or                   
194     ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
195     $host =~ s#//##;
196
197     my $acct = OpenILS::Utils::RemoteAccount->new(
198         type            => $type,
199         remote_host     => $host,
200         account_object  => $racct,
201         local_file      => $local_file,
202         remote_file     => $output_file
203     );
204
205     my $res = $acct->put;
206
207     die "Unable to push to remote server [$remote_acct] : " . 
208         $acct->error . "\n" unless $res;
209
210     print "Pushed file to $res\n" if $verbose;
211 }
212
213 if ($cleanup) {
214     unlink($local_file) or 
215         die "Unable to clean up file '$local_file' : $!\n";
216 }
217
218