Isolate Stemweb calls into own controller; guard against server being down. #29
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Stemweb.pm
1 package stemmaweb::Controller::Stemweb;
2 use Moose;
3 use namespace::autoclean;
4 use Encode qw/ decode_utf8 /;
5 use JSON;
6 use LWP::UserAgent;
7 use Safe::Isa;
8 use TryCatch;
9 use URI;
10
11 BEGIN { extends 'Catalyst::Controller' }
12
13 ## TODO Move the /algorithms/available function to the Stemweb module
14 my $STEMWEB_BASE_URL = 'http://slinkola.users.cs.helsinki.fi';
15
16 =head1 NAME
17
18 stemmaweb::Controller::Stemweb - Client listener for Stemweb results
19
20 =head1 DESCRIPTION
21
22 This is a client listener for the Stemweb API as implemented by the protocol defined at
23 L<https://docs.google.com/document/d/1aNYGAo1v1WPDZi6LXZ30FJSMJwF8RQPYbOkKqHdCZEc/pub>.
24
25 =head1 METHODS
26
27 =head2 result
28
29  POST stemweb/result
30  Content-Type: application/json
31  (On success):
32  { jobid: <ID number>
33    status: 0
34    format: <format>
35    result: <data> }
36  (On failure):
37  { jobid: <ID number>
38    status: >1
39    result: <error message> }
40    
41 Used by the Stemweb server to notify us that one or more stemma graphs
42 has been calculated in response to an earlier request.
43
44 =cut
45
46 sub result :Local :Args(0) {
47         my( $self, $c ) = @_;
48         if( $c->request->method eq 'POST' ) {
49                 # TODO: Verify the sender!
50                 my $answer;
51                 if( ref( $c->request->body ) eq 'File::Temp' ) {
52                         # Read in the file and parse that.
53                         $c->log->debug( "Request body is in a temp file" );
54                         open( POSTDATA, $c->request->body ) 
55                                 or return _json_error( $c, 500, "Failed to open post data file" );
56                         binmode( POSTDATA, ':utf8' );
57                         # JSON should be all one line
58                         my $pdata = <POSTDATA>;
59                         chomp $pdata;
60                         close POSTDATA;
61                         try {
62                                 $answer = from_json( $pdata );
63                         } catch {
64                                 return _json_error( $c, 400, 
65                                         "Could not parse POST request '' $pdata '' as JSON: $@" );
66                         }
67                 } else {
68                         $answer = from_json( $c->request->body );
69                 }
70                 $c->log->debug( "Received push notification from Stemweb: "
71                         . to_json( $answer ) );
72                 return _process_stemweb_result( $c, $answer );
73         } else {
74                 return _json_error( $c, 403, 'Please use POST!' );
75         }
76 }
77
78 =head2 available
79
80  GET algorithms/available
81  
82 Queries the Stemweb server for available stemma generation algorithms and their 
83 parameters. Returns the JSON answer as obtained from Stemweb.
84
85 =cut
86
87 sub available :Local :Args(0) {
88         my( $self, $c ) = @_;
89         my $ua = LWP::UserAgent->new();
90         my $resp = $ua->get( $STEMWEB_BASE_URL . '/algorithms/available' );
91         if( $resp->is_success ) {
92                 $c->stash->{'result'} = $resp->content;
93         } else {
94                 $c->stash->{'result'} = '{}';
95         }
96         $c->forward('View::JSON');
97 }
98
99 =head2 query
100
101  GET stemweb/query/<jobid>
102
103 A backup method to query the stemweb server to check a particular job status.
104 Returns a result as in /stemweb/result above, but status can also be -1 to 
105 indicate that the job is still running.
106
107 =cut
108
109 sub query :Local :Args(1) {
110         my( $self, $c, $jobid ) = @_;
111         my $ua = LWP::UserAgent->new();
112         my $resp = $ua->get( $STEMWEB_BASE_URL . "/algorithms/jobstatus/$jobid" );
113         if( $resp->is_success ) {
114                 # Process it
115                 my $response = decode_utf8( $resp->content );
116                 $c->log->debug( "Got a response from the server: $response" );
117                 my $answer;
118                 try {
119                         $answer = from_json( $response );
120                 } catch {
121                         return _json_error( $c, 500, 
122                                 "Could not parse stemweb response '' $response '' as JSON: $@" );
123                 }
124                 return _process_stemweb_result( $c, $answer );
125         } elsif( $resp->code == 500 && $resp->header('Client-Warning')
126                 && $resp->header('Client-Warning') eq 'Internal response' ) {
127                 # The server was unavailable.
128                 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
129         } else {
130                 return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / "
131                         . $resp->content );
132         }
133 }
134
135
136 ## Helper function for parsing Stemweb result data either by push or by pull
137 sub _process_stemweb_result {
138         my( $c, $answer ) = @_;
139         # Find the specified tradition and check its job ID.
140         my $m = $c->model('Directory');
141         my $tradition = $m->tradition( $answer->{textid} );
142         unless( $tradition ) {
143                 return _json_error( $c, 400, "No tradition found with ID "
144                         . $answer->{textid} );
145         }
146         if( $answer->{status} == 0 ) {
147                 my $stemmata;
148                 if( $tradition->has_stemweb_jobid 
149                         && $tradition->stemweb_jobid eq $answer->{jobid} ) {
150                         try {
151                                 $stemmata = $tradition->record_stemweb_result( $answer );
152                                 $m->save( $tradition );
153                         } catch( Text::Tradition::Error $e ) {
154                                 return _json_error( $c, 500, $e->message );
155                         } catch {
156                                 return _json_error( $c, 500, $@ );
157                         }
158                 } else {
159                         # It may be that we already received a callback meanwhile.
160                         # Check all stemmata for the given jobid and return them.
161                         @$stemmata = grep { $_->came_from_jobid && $_->from_jobid eq $answer->{jobid} } $tradition->stemmata;
162                 }
163         $DB::single = 1;
164                 if( @$stemmata ) {
165                         # If we got here, success!
166                         my @steminfo = map { { 
167                                         name => $_->identifier, 
168                                         directed => _json_bool( !$_->is_undirected ),
169                                         svg => $_->as_svg() } } 
170                                 @$stemmata;
171                         $c->stash->{'result'} = { 
172                                 'status' => 'success',
173                                 'stemmata' => \@steminfo };
174                 } else {
175                         # Hm, no stemmata found on this tradition with this jobid.
176                         # Clear the tradition jobid so that the user can try again.
177                         if( $tradition->has_stemweb_jobid ) {
178                                 $tradition->_clear_stemweb_jobid;
179                                 $m->save( $tradition );
180                         }
181                         $c->stash->{'result'} = { status => 'notfound' };
182                 }
183         } elsif( $answer->{status} < 1 ) {
184                 $c->stash->{'result'} = { 'status' => 'running' };
185         } else {
186                 return _json_error( $c, 500,
187                         "Stemweb failure not handled: " . $answer->{result} );
188         }
189         $c->forward('View::JSON');
190 }
191
192 =head2 request
193
194  GET stemweb/request/?
195         tradition=<tradition ID> &
196         algorithm=<algorithm ID> &
197         [<algorithm parameters>]
198    
199 Send a request for the given tradition with the given parameters to Stemweb.
200 Processes and returns the JSON response given by the Stemweb server.
201
202 =cut
203
204 sub request :Local :Args(0) {
205         my( $self, $c ) = @_;
206         # Look up the relevant tradition and check permissions.
207         my $reqparams = $c->req->params;
208         my $tid = delete $reqparams->{tradition};
209         my $t = $c->model('Directory')->tradition( $tid );
210         my $ok = _check_permission( $c, $t );
211         return unless $ok;
212         return( _json_error( $c, 403, 
213                         'You do not have permission to update stemmata for this tradition' ) )
214                 unless $ok eq 'full';
215         
216         # Form the request for Stemweb.
217         my $algorithm = delete $reqparams->{algorithm};
218         my $return_uri = URI->new( $c->uri_for( '/stemweb/result' ) );
219         my $stemweb_request = {
220                 return_path => $return_uri->path,
221                 return_host => $return_uri->host_port,
222                 data => $t->collation->as_tsv({noac => 1}),
223                 userid => $c->user->get_object->email,
224                 textid => $tid,
225                 parameters => $reqparams };
226                 
227         # Call to the appropriate URL with the request parameters.
228         my $ua = LWP::UserAgent->new();
229         $c->log->debug( 'Sending request to Stemweb: ' . to_json( $stemweb_request ) ); 
230         my $resp = $ua->post( $STEMWEB_BASE_URL . "/algorithms/process/$algorithm/",
231                 'Content-Type' => 'application/json; charset=utf-8', 
232                 'Content' => encode_json( $stemweb_request ) ); 
233         if( $resp->is_success ) {
234                 # Process it
235                 $c->log->debug( 'Got a response from the server: '
236                         . decode_utf8( $resp->content ) );
237                 my $stemweb_response = decode_json( $resp->content );
238                 try {
239                         $t->set_stemweb_jobid( $stemweb_response->{jobid} );
240                 } catch( Text::Tradition::Error $e ) {
241                         return _json_error( $c, 429, $e->message );
242                 }
243                 $c->model('Directory')->save( $t );
244                 $c->stash->{'result'} = $stemweb_response;
245                 $c->forward('View::JSON');
246         } elsif( $resp->code == 500 && $resp->header('Client-Warning')
247                 && $resp->header('Client-Warning') eq 'Internal response' ) {
248                 # The server was unavailable.
249                 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
250         } else {
251                 return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / "
252                         . $resp->content );
253         }
254 }
255
256 # Helper to check what permission, if any, the active user has for
257 # the given tradition
258 sub _check_permission {
259         my( $c, $tradition ) = @_;
260     my $user = $c->user_exists ? $c->user->get_object : undef;
261     if( $user ) {
262         return 'full' if ( $user->is_admin || 
263                 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
264     }
265         # Text doesn't belong to us, so maybe it's public?
266         return 'readonly' if $tradition->public;
267
268         # ...nope. Forbidden!
269         return _json_error( $c, 403, 'You do not have permission to view this tradition.' );
270 }
271
272 # Helper to throw a JSON exception
273 sub _json_error {
274         my( $c, $code, $errmsg ) = @_;
275         $c->response->status( $code );
276         $c->stash->{'result'} = { 'error' => $errmsg };
277         $c->forward('View::JSON');
278         return 0;
279 }
280
281 sub _json_bool {
282         return $_[0] ? JSON::true : JSON::false;
283 }
284
285
286 1;