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 = '/openils/conf/opensrf_core.xml';
27 my $start_date = DateTime->now->strftime('%F');
32 my $local_dir = '/tmp'; # where to keep local copies of generated files
34 my $cleanup = 0; # cleanup generated files
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,
55 Action/Trigger Aggregator Script
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.
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
68 # Collect a week of notifications for 3 event definitions
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 \
81 Remove the local file after script is done.
84 action_trigger.event_definition IDs to include
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.
91 Only collect output for events whose run_time is on or after this ISO date
94 Only collect output for events whose run_time occurred before this ISO date
97 To set the OpenSRF config to something other than the default of
98 '/openils/conf/opensrf_core.xml'
100 --output-file [default STDOUT]
101 Output goes to this file.
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
108 Evergreen config.remote_account ID
109 If set, the output-file will be sent via sFTP/SCP to this server.
112 Show more information about what the script is doing.
115 Show command usage information.
123 my @event_defs = split(/,/, $event_defs);
124 die "--event-defs or --granularity required\n"
125 unless @event_defs or $granularity;
127 my $local_file = $output_file ? "$local_dir/$output_file" : '&STDOUT';
129 open(OUTFILE, ">$local_file") or
130 die "unable to open out-file '$local_file' for writing: $!\n";
131 binmode(OUTFILE, ":utf8");
133 print "Output will be written to $local_file\n" if $verbose;
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;
141 # if granularity is set, append all event defs with the
142 # selected granularity to the set of event-defs to process.
144 my $defs = $editor->search_action_trigger_event_definition(
145 {granularity => $granularity},
149 for my $id (@$defs) {
150 push(@event_defs, $id)
151 unless grep { $_ eq $id} @event_defs;
155 print "Processing event-defs @event_defs\n" if $verbose;
158 $date_filter{run_time} = {'>=' => $start_date} if $start_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}}
168 $date_filter{run_time} = {'<' => $end_date};
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', {
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 => {
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};
199 # send the file to the remote account
201 my $racct = $editor->retrieve_config_remote_account($remote_acct);
202 die "No such remote account $remote_acct" unless $racct;
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');
210 my $acct = OpenILS::Utils::RemoteAccount->new(
212 remote_host => $host,
213 account_object => $racct,
214 local_file => $local_file,
215 remote_file => $output_file
218 my $res = $acct->put;
220 die "Unable to push to remote server [$remote_acct] : " .
221 $acct->error . "\n" unless $res;
223 print "Pushed file to $res\n" if $verbose;
227 unlink($local_file) or
228 die "Unable to clean up file '$local_file' : $!\n";