2 # ---------------------------------------------------------------
3 # Copyright (C) 2012 Equinox Software, Inc
4 # Author: Bill Erickson <berick@esilibrary.com>
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.
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 # ---------------------------------------------------------------
21 use OpenSRF::AppSession;
22 use OpenILS::Utils::CStoreEditor;
23 use OpenILS::Utils::RemoteAccount;
24 use OpenILS::Utils::Fieldmapper;
26 my $osrf_config = '@sysconfdir@/opensrf_core.xml';
27 my $default_start_date = DateTime->now->strftime('%F');
34 my $local_dir = '/tmp'; # where to keep local copies of generated files
36 my $cleanup = 0; # cleanup generated files
41 'osrf-config=s' => \$osrf_config,
43 'start-date=s' => \$start_date,
44 'end-date=s' => \$end_date,
45 'event-defs=s' => \$event_defs,
46 'granularity=s' => \$granularity,
47 'output-file=s' => \$output_file,
48 'remote-acct=s' => \$remote_acct,
49 'local-dir=s' => \$local_dir,
50 'cleanup' => \$cleanup,
51 'verbose' => \$verbose,
58 Action/Trigger Aggregator Script
60 Collect template output from one or more event-definitions and stitch the
61 results together in a single file. The file may be optionally stored locally
62 and/or delivered to a remote ftp/scp account.
64 This script is useful for generating a single mass notification (e.g. overdue)
65 file and sending the file to a 3rd party for processing and/or for local
66 processing. The anticipated use case would be to stitch together lines of CSV
71 # Collect a week of notifications for 3 event definitions
74 --event-defs 104,105,106 \
75 --start-date 2012-01-01 \
76 --end-date 2012-01-07 \
77 --output-file 2012-01-07.notify.csv \
78 --local-dir /var/run/evergreen/csv \
84 Remove the local file after script is done.
87 action_trigger.event_definition IDs to include
90 Process all event definitions that match this granularity. If used in
91 conjunction with --event-defs, the union of the two sets is used.
94 Only collect output for events whose run_time is on or after this ISO date
97 Only collect output for events whose run_time occurred before this ISO date
100 Only collect output for events whose run_time is on this ISO date.
101 Cannot be used with either --start-date or --end-date
104 To set the OpenSRF config to something other than the default of
105 '@sysconfdir@/opensrf_core.xml'
107 --output-file [default STDOUT]
108 Output goes to this file.
110 --local-dir [default /tmp]
111 Local directory where the output-file is placed. If --cleanup is
112 set, this is used as the tmp directory for file generation
115 Evergreen config.remote_account ID
116 If set, the output-file will be sent via sFTP/SCP to this server.
119 Show more information about what the script is doing.
122 Show command usage information.
130 my @event_defs = split(/,/, $event_defs);
131 die "--event-defs or --granularity required\n"
132 unless @event_defs or $granularity;
134 my $local_file = $output_file ? "$local_dir/$output_file" : '&STDOUT';
136 open(OUTFILE, ">$local_file") or
137 die "unable to open out-file '$local_file' for writing: $!\n";
138 binmode(OUTFILE, ":utf8");
140 print "Output will be written to $local_file\n" if $verbose;
142 OpenSRF::System->bootstrap_client(config_file => $osrf_config);
143 Fieldmapper->import(IDL =>
144 OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
145 OpenILS::Utils::CStoreEditor::init();
146 my $editor = OpenILS::Utils::CStoreEditor->new;
148 # if granularity is set, append all event defs with the
149 # selected granularity to the set of event-defs to process.
151 my $defs = $editor->search_action_trigger_event_definition(
152 {granularity => $granularity},
156 for my $id (@$defs) {
157 push(@event_defs, $id)
158 unless grep { $_ eq $id} @event_defs;
162 print "Processing event-defs @event_defs\n" if $verbose;
164 die "Can't use --date with --start-date or --end-date!" if ($date && ($start_date || $end_date) );
166 $start_date = $default_start_date unless $start_date; # This is basically what happened pre- --date
169 $date_filter{run_time} = {'>=' => $start_date} if $start_date;
172 if ($date_filter{run_time}) {
173 # both filters requested, -and them together
174 $date_filter{'-and'} = [
175 {run_time => delete $date_filter{run_time}},
176 {run_time => {'<' => $end_date}}
179 $date_filter{run_time} = {'<' => $end_date};
184 delete $date_filter{run_time}; # will always exist because of defaulting $start_date
185 $date_filter{run_time} = { "=" => {"transform" => "date", "value" => $date}};
188 # collect the event tempate output data
189 # use a real session here so we can stream results directly to the output file
190 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
191 my $req = $ses->request(
192 'open-ils.cstore.json_query', {
194 # ateo's may be linked to multiple atev's; select distinct.
195 {column => 'id', transform => 'distinct'}, 'data']},
196 from => {ateo => { atev => {
197 filter => {state => 'complete', %date_filter},
198 join => {atevdef => {filter => {
206 # use a large timeout since this is likely to be a hefty query
207 while (my $resp = $req->recv(timeout => 3600)) {
208 die $req->failed . "\n" if $req->failed;
209 my $content = $resp->content or next;
210 print OUTFILE $content->{data};
214 # send the file to the remote account
216 my $racct = $editor->retrieve_config_remote_account($remote_acct);
217 die "No such remote account $remote_acct" unless $racct;
220 my $host = $racct->host;
221 ($host =~ s/^(S?FTP)://i and $type = uc($1)) or
222 ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
225 my $acct = OpenILS::Utils::RemoteAccount->new(
227 remote_host => $host,
228 account_object => $racct,
229 local_file => $local_file,
230 remote_file => $output_file
233 my $res = $acct->put;
235 die "Unable to push to remote server [$remote_acct] : " .
236 $acct->error . "\n" unless $res;
238 print "Pushed file to $res\n" if $verbose;
242 unlink($local_file) or
243 die "Unable to clean up file '$local_file' : $!\n";