]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/test-scripts/offline.pl
LP#797409: Offline Transaction - Option to Skip Old Records
[working/Evergreen.git] / Open-ILS / src / support-scripts / test-scripts / offline.pl
1 #!/usr/bin/perl
2 require '../oils_header.pl';
3 use vars qw/ $user $authtoken /;
4 use strict; use warnings;
5 use Time::HiRes qw/time usleep/;
6 use LWP::UserAgent;
7 use HTTP::Request::Common;
8 use Data::Dumper;
9 use OpenSRF::Utils::JSON;
10
11 #-----------------------------------------------------------------------------
12 # Does a checkout, renew, and checkin 
13 #-----------------------------------------------------------------------------
14
15 err("usage: $0 <config> <username> <password> <base_url> ".
16         "<workstation_name> <patron_barcode> <item_barcode> <iterations>") unless $ARGV[7];
17
18 my $config              = shift; # - bootstrap config
19 my $username    = shift; # - oils login username
20 my $password    = shift; # - oils login password
21 my $baseurl             = shift; # - base offline script url
22 my $station             = shift; # - workstation name
23 my $patronbc    = shift; # - patron barcode
24 my $barcode             = shift; # - item barcode
25 my $iterations  = shift || 1; # - number of iterations
26
27 my $useragent = LWP::UserAgent->new; # - HTTP request handler
28 my $seskey;
29 my $params; # - CGI params
30
31
32 sub go {
33         osrf_connect($config);
34         oils_login($username, $password);
35         $params = "?ses=$authtoken&ws=$station";
36         run_scripts();
37         oils_logout();
38 }
39 go();
40
41
42
43 #-----------------------------------------------------------------------------
44 # Builds the script lines
45 #-----------------------------------------------------------------------------
46 sub build_script {
47         
48         my $json = "";
49         my $time = CORE::time;
50
51         for(1..$iterations) {
52
53                 my($s,$m,$h,$d,$mon,$y) = localtime(++$time);
54                 $mon++; $y += 1900;
55                 my $t1 = "$y-$mon-$d";
56                 my $t2 = "$t1 $h:$m:$s";
57         
58                 my $checkout = {
59                         timestamp               => ++$time,
60                         type                            => "checkout",
61                         barcode                 => $barcode,
62                         patron_barcode => $patronbc,
63                         checkout_time   => $t2, 
64                         due_date                        => $t1
65                 };
66         
67 #               my $renew = undef;
68                 my $renew = {
69                         timestamp               => ++$time,
70                         type                            => "renew",
71                         barcode                 => $barcode,
72                         patron_barcode => $patronbc,
73                         checkout_time   => $t2, 
74                         due_date                        => $t1
75                 };
76         
77                 my $checkin = {
78                         timestamp               => ++$time,
79                         type                            => "checkin",
80                         barcode                 => $barcode,
81                         backdate                        => $t1
82                 };
83         
84         # Here we need to create an option to test the skip_late feature, ask community
85
86                 $json .= OpenSRF::Utils::JSON->perl2JSON($checkout) . "\n";
87                 $json .= OpenSRF::Utils::JSON->perl2JSON($renew) . "\n" if $renew;
88                 $json .= OpenSRF::Utils::JSON->perl2JSON($checkin) . "\n";
89         }
90
91         return $json;
92 }
93
94 #-----------------------------------------------------------------------------
95 # Run the scripts
96 #-----------------------------------------------------------------------------
97 sub run_scripts { 
98         create_session();
99         upload_script(); 
100         check_sessions();
101         run_script();
102         check_script();
103 }
104
105
106 sub create_session {
107
108         my $url = "$baseurl/offline.pl$params&action=create&desc=test_d";
109         my $req = GET( $url );
110         my $res = $useragent->request($req);
111         my $response = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
112
113         oils_event_die($response);
114         $seskey = $response->{payload};
115         $params = "$params&seskey=$seskey";
116
117         printl("Created new session with key $seskey");
118 }
119
120
121 #-----------------------------------------------------------------------------
122 # Uploads the offline script to the server
123 #-----------------------------------------------------------------------------
124 sub upload_script {
125         my $script =  build_script();
126
127         my $req = POST( 
128                 "$baseurl/offline.pl",
129                 Content_Type => 'form-data',
130                 Content => [
131                         action  => 'load',
132                         seskey  => $seskey,
133                         ses             => $authtoken, 
134                         ws                      => $station, 
135                         file            => [ undef, "offline-test.script", Content_Type => "text/plain", Content => $script ] ]
136                 );
137
138         my $res = $useragent->request($req);
139
140         # Pass request to the user agent and get a response back
141         my $event = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
142         oils_event_die($event);
143         print "Upload succeeded to session $seskey...\n";
144 }
145
146
147 #-----------------------------------------------------------------------------
148 # Gets a list of all of the sessions that were either started today or 
149 # completed today
150 #-----------------------------------------------------------------------------
151 sub check_sessions {
152
153         my $url = "$baseurl/offline.pl$params&action=status&status_type=scripts";
154         my $req = GET( $url );
155         my $res = $useragent->request($req);
156         my $ses = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
157
158         my $scripts = $ses->{scripts};
159         delete $ses->{scripts};
160
161         $ses->{$_} ||= "" for keys %$ses;
162
163         print "-"x60 . "\n";
164         print "Session Details\n\n";
165         print "$_=".$ses->{$_}."\n" for keys %$ses;
166
167         print "scripts:\n";
168         for my $scr (@$scripts) {
169                 $scr->{$_} ||= "" for keys %$scr;
170                 print "\t$_=".$scr->{$_}."\n" for keys %$scr;
171         }
172
173
174
175         print "-"x60 . "\n";
176 }
177
178
179 #-----------------------------------------------------------------------------
180 # Tells the server to run the script 
181 #-----------------------------------------------------------------------------
182 sub run_script {
183
184         print "Executing script...\n";
185         my $url = "$baseurl/offline.pl$params&action=execute";
186         my $req = GET( $url );
187
188         my $res = $useragent->request($req);
189         my $event = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
190
191         oils_event_die($event);
192 }
193
194 sub check_script {
195
196         my $complete = 0;
197         my $start = time;
198
199         while(1) {
200
201                 my $url = "$baseurl/offline.pl$params&action=status&status_type=summary";
202                 my $req = GET( $url );
203                 my $res = $useragent->request($req);
204                 my $blob = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
205
206                 my $total = $blob->{total};
207                 my $count = $blob->{num_complete} || "0";
208                 $complete = ($total == $count) ? 1 : 0;
209
210                 print "Completed Transactions: $count\n";
211                 last if $complete;
212
213                 sleep 1;
214         }
215
216         my $diff = time - $start;
217
218         my $url = "$baseurl/offline.pl$params&action=status&status_type=exceptions";
219         my $req = GET( $url );
220         my $res = $useragent->request($req);
221         my $blob = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
222
223         my @events;
224         push(@events, $_->{event}) for @$blob;
225
226         print "Received event ".$_->{ilsevent}.' : '.$_->{textcode}."\n" for @events;
227
228         print "-"x60 . "\n";
229         print "Execute round trip took $diff seconds\n";
230         print "-"x60 . "\n";
231 }
232
233