]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/test-scripts/offline.pl
80c2dd1d18b749d5c024996b6f99eb75e5749297
[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                 $json .= OpenSRF::Utils::JSON->perl2JSON($checkout) . "\n";
85                 $json .= OpenSRF::Utils::JSON->perl2JSON($renew) . "\n" if $renew;
86                 $json .= OpenSRF::Utils::JSON->perl2JSON($checkin) . "\n";
87         }
88
89         return $json;
90 }
91
92 #-----------------------------------------------------------------------------
93 # Run the scripts
94 #-----------------------------------------------------------------------------
95 sub run_scripts { 
96         create_session();
97         upload_script(); 
98         check_sessions();
99         run_script();
100         check_script();
101 }
102
103
104 sub create_session {
105
106         my $url = "$baseurl/offline.pl$params&action=create&desc=test_d";
107         my $req = GET( $url );
108         my $res = $useragent->request($req);
109         my $response = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
110
111         oils_event_die($response);
112         $seskey = $response->{payload};
113         $params = "$params&seskey=$seskey";
114
115         printl("Created new session with key $seskey");
116 }
117
118
119 #-----------------------------------------------------------------------------
120 # Uploads the offline script to the server
121 #-----------------------------------------------------------------------------
122 sub upload_script {
123         my $script =  build_script();
124
125         my $req = POST( 
126                 "$baseurl/offline.pl",
127                 Content_Type => 'form-data',
128                 Content => [
129                         action  => 'load',
130                         seskey  => $seskey,
131                         ses             => $authtoken, 
132                         ws                      => $station, 
133                         file            => [ undef, "offline-test.script", Content_Type => "text/plain", Content => $script ] ]
134                 );
135
136         my $res = $useragent->request($req);
137
138         # Pass request to the user agent and get a response back
139         my $event = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
140         oils_event_die($event);
141         print "Upload succeeded to session $seskey...\n";
142 }
143
144
145 #-----------------------------------------------------------------------------
146 # Gets a list of all of the sessions that were either started today or 
147 # completed today
148 #-----------------------------------------------------------------------------
149 sub check_sessions {
150
151         my $url = "$baseurl/offline.pl$params&action=status&status_type=scripts";
152         my $req = GET( $url );
153         my $res = $useragent->request($req);
154         my $ses = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
155
156         my $scripts = $ses->{scripts};
157         delete $ses->{scripts};
158
159         $ses->{$_} ||= "" for keys %$ses;
160
161         print "-"x60 . "\n";
162         print "Session Details\n\n";
163         print "$_=".$ses->{$_}."\n" for keys %$ses;
164
165         print "scripts:\n";
166         for my $scr (@$scripts) {
167                 $scr->{$_} ||= "" for keys %$scr;
168                 print "\t$_=".$scr->{$_}."\n" for keys %$scr;
169         }
170
171
172
173         print "-"x60 . "\n";
174 }
175
176
177 #-----------------------------------------------------------------------------
178 # Tells the server to run the script 
179 #-----------------------------------------------------------------------------
180 sub run_script {
181
182         print "Executing script...\n";
183         my $url = "$baseurl/offline.pl$params&action=execute";
184         my $req = GET( $url );
185
186         my $res = $useragent->request($req);
187         my $event = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
188
189         oils_event_die($event);
190 }
191
192 sub check_script {
193
194         my $complete = 0;
195         my $start = time;
196
197         while(1) {
198
199                 my $url = "$baseurl/offline.pl$params&action=status&status_type=summary";
200                 my $req = GET( $url );
201                 my $res = $useragent->request($req);
202                 my $blob = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
203
204                 my $total = $blob->{total};
205                 my $count = $blob->{num_complete} || "0";
206                 $complete = ($total == $count) ? 1 : 0;
207
208                 print "Completed Transactions: $count\n";
209                 last if $complete;
210
211                 sleep 1;
212         }
213
214         my $diff = time - $start;
215
216         my $url = "$baseurl/offline.pl$params&action=status&status_type=exceptions";
217         my $req = GET( $url );
218         my $res = $useragent->request($req);
219         my $blob = OpenSRF::Utils::JSON->JSON2perl($res->{_content});
220
221         my @events;
222         push(@events, $_->{event}) for @$blob;
223
224         print "Received event ".$_->{ilsevent}.' : '.$_->{textcode}."\n" for @events;
225
226         print "-"x60 . "\n";
227         print "Execute round trip took $diff seconds\n";
228         print "-"x60 . "\n";
229 }
230
231