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 action_trigger.event_definition IDs to include
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.
88 Only collect output for events whose run_time is on or after this ISO date
91 Only collect output for events whose run_time occurred before this IDO date
93 --output-file [default STDOUT]
94 Output goes to this file.
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
101 Evergreen config.remote_account ID
102 If set, the output-file will be sent via sFTP/SCP to this server.
110 my @event_defs = split(/,/, $event_defs);
111 die "--event-defs or --granularity required\n"
112 unless @event_defs or $granularity;
114 my $local_file = $output_file ? "$local_dir/$output_file" : '&STDOUT';
116 open(OUTFILE, ">$local_file") or
117 die "unable to open out-file '$local_file' for writing: $!\n";
118 binmode(OUTFILE, ":utf8");
120 print "Output will be written to $local_file\n" if $verbose;
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;
128 # if granularity is set, append all event defs with the
129 # selected granularity to the set of event-defs to process.
131 my $defs = $editor->search_action_trigger_event_definition(
132 {granularity => $granularity},
136 for my $id (@$defs) {
137 push(@event_defs, $id)
138 unless grep { $_ eq $id} @event_defs;
142 print "Processing event-defs @event_defs\n" if $verbose;
145 $date_filter{run_time} = {'>=' => $start_date} if $start_date;
146 $date_filter{run_time} = {'<' => $end_date} if $end_date;
148 # collect the event tempate output data
149 # use a real session here so we can stream results directly to the output file
150 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
151 my $req = $ses->request(
152 'open-ils.cstore.json_query', {
153 select => {ateo => ['data']},
154 from => {ateo => { atev => {
155 filter => {state => 'complete', %date_filter},
156 join => {atevdef => {filter => {
164 # use a large timeout since this is likely to be a hefty query
165 while (my $resp = $req->recv(timeout => 3600)) {
166 die $req->failed . "\n" if $req->failed;
167 my $content = $resp->content or next;
168 print OUTFILE $content->{data};
172 # send the file to the remote account
174 my $racct = $editor->retrieve_config_remote_account($remote_acct);
175 die "No such remote account $remote_acct" unless $racct;
178 my $host = $racct->host;
179 ($host =~ s/^(S?FTP)://i and $type = uc($1)) or
180 ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
183 my $acct = OpenILS::Utils::RemoteAccount->new(
185 remote_host => $host,
186 account_object => $racct,
187 local_file => $local_file,
188 remote_file => $output_file
191 my $res = $acct->put;
193 die "Unable to push to remote server [$remote_acct] : " .
194 $acct->error . "\n" unless $res;
196 print "Pushed file to $res\n" if $verbose;
200 unlink($local_file) or
201 die "Unable to clean up file '$local_file' : $!\n";