Cosmetic fixes to state after stemweb request and query. #29
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Stemweb.pm
CommitLineData
532cc23b 1package stemmaweb::Controller::Stemweb;
2use Moose;
3use namespace::autoclean;
ed0ce314 4use Encode qw/ decode_utf8 /;
c2b80bba 5use JSON;
70744367 6use LWP::UserAgent;
532cc23b 7use Safe::Isa;
8use TryCatch;
70744367 9use URI;
532cc23b 10
11BEGIN { extends 'Catalyst::Controller' }
12
70744367 13## TODO Move the /algorithms/available function to the Stemweb module
14my $STEMWEB_BASE_URL = 'http://slinkola.users.cs.helsinki.fi';
15
532cc23b 16=head1 NAME
17
18stemmaweb::Controller::Stemweb - Client listener for Stemweb results
19
20=head1 DESCRIPTION
21
22This is a client listener for the Stemweb API as implemented by the protocol defined at
23L<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 { job_id: <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
41Used by the Stemweb server to notify us that one or more stemma graphs
42has been calculated in response to an earlier request.
43
44=cut
45
46sub 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 open( POSTDATA, $c->request->body ) or die "Failed to open post data file";
54 binmode( POSTDATA, ':utf8' );
55 # JSON should be all one line
56 my $pdata = <POSTDATA>;
57 chomp $pdata;
58 close POSTDATA;
c2b80bba 59 try {
60 $answer = from_json( $pdata );
61 } catch {
62 return _json_error( $c, 400,
63 "Could not parse POST request '' $pdata '' as JSON: $@" );
64 }
532cc23b 65 } else {
66 $answer = from_json( $c->request->body );
67 }
c2b80bba 68 return _process_stemweb_result( $c, $answer );
69 } else {
70 return _json_error( $c, 403, 'Please use POST!' );
71 }
72}
73
74=head2 query
75
76 GET stemweb/query/<jobid>
77
78A backup method to query the stemweb server to check a particular job status.
79Returns a result as in /stemweb/result above, but status can also be -1 to
80indicate that the job is still running.
81
82=cut
83
84sub query :Local :Args(1) {
85 my( $self, $c, $jobid ) = @_;
86 my $ua = LWP::UserAgent->new();
2c514a6f 87 my $resp = $ua->get( $STEMWEB_BASE_URL . "/algorithms/jobstatus/$jobid" );
c2b80bba 88 if( $resp->is_success ) {
89 # Process it
90 my $response = decode_utf8( $resp->content );
91 $c->log->debug( "Got a response from the server: $response" );
92 my $answer;
93 try {
94 $answer = from_json( $response );
95 } catch {
532cc23b 96 return _json_error( $c, 500,
c2b80bba 97 "Could not parse stemweb response '' $response '' as JSON: $@" );
98 }
99 return _process_stemweb_result( $c, $answer );
100 } elsif( $resp->code == 500 && $resp->header('Client-Warning')
101 && $resp->header('Client-Warning') eq 'Internal response' ) {
102 # The server was unavailable.
103 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
104 } else {
105 return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / "
106 . $resp->content );
107 }
108}
109
110
111## Helper function for parsing Stemweb result data either by push or by pull
112sub _process_stemweb_result {
113 my( $c, $answer ) = @_;
114 # Find a tradition with the defined Stemweb job ID.
115 # TODO: Maybe get Stemweb to pass back the tradition ID...
116 my $m = $c->model('Directory');
117 my @traditions;
2c514a6f 118 ## STUPID HACK: unless we load the possible tradition owners
119 ## within scope of the scan, they will not exist when the affected
120 ## tradition is saved.
121 my @users;
c2b80bba 122 $m->scan( sub{ push( @traditions, $_[0] )
123 if $_[0]->$_isa('Text::Tradition')
124 && $_[0]->has_stemweb_jobid
125 && $_[0]->stemweb_jobid eq $answer->{job_id};
2c514a6f 126 push( @users, $_[0] ) if $_[0]->$_isa('Text::Tradition::User');
c2b80bba 127 } );
128 if( @traditions == 1 ) {
129 my $tradition = shift @traditions;
130 if( $answer->{status} == 0 ) {
131 my $stemmata;
132 try {
133 $stemmata = $tradition->record_stemweb_result( $answer );
134 $m->save( $tradition );
135 } catch( Text::Tradition::Error $e ) {
136 return _json_error( $c, 500, $e->message );
137 } catch {
138 return _json_error( $c, 500, $@ );
139 }
140 # If we got here, success!
141 my @steminfo = map { {
142 name => $_->identifier,
143 directed => _json_bool( !$_->is_undirected ),
144 svg => $_->as_svg() } }
2c514a6f 145 @$stemmata;
c2b80bba 146 $c->stash->{'result'} = {
147 'status' => 'success',
148 'stemmata' => \@steminfo };
149 } elsif( $answer->{status} < 1 ) {
150 $c->stash->{'result'} = { 'status' => 'running' };
151 } else {
152 return _json_error( $c, 500,
153 "Stemweb failure not handled: " . $answer->{result} );
154 }
155 } elsif( @traditions ) {
156 return _json_error( $c, 500,
157 "Multiple traditions with Stemweb job ID " . $answer->{job_id} . "!" );
158 } else {
159 # Possible that the tradition got updated in the meantime...
160 if( $answer->{status} == 0 ) {
161 $c->stash->{'result'} = { 'status' => 'notfound' };
532cc23b 162 } else {
163 return _json_error( $c, 400,
164 "No tradition found with Stemweb job ID " . $answer->{job_id} );
165 }
532cc23b 166 }
c2b80bba 167 $c->forward('View::JSON');
532cc23b 168}
169
70744367 170=head2 request
171
172 GET stemweb/request/?
173 tradition=<tradition ID> &
174 algorithm=<algorithm ID> &
175 [<algorithm parameters>]
176
177Send a request for the given tradition with the given parameters to Stemweb.
178Processes and returns the JSON response given by the Stemweb server.
179
180=cut
181
182sub request :Local :Args(0) {
183 my( $self, $c ) = @_;
184 # Look up the relevant tradition and check permissions.
185 my $reqparams = $c->req->params;
186 my $tid = delete $reqparams->{tradition};
187 my $t = $c->model('Directory')->tradition( $tid );
188 my $ok = _check_permission( $c, $t );
189 return unless $ok;
190 return( _json_error( $c, 403,
191 'You do not have permission to update stemmata for this tradition' ) )
192 unless $ok eq 'full';
193
194 # Form the request for Stemweb.
195 my $algorithm = delete $reqparams->{algorithm};
196 my $return_uri = URI->new( $c->uri_for( '/stemweb/result' ) );
197 my $stemweb_request = {
198 return_path => $return_uri->path,
199 return_host => $return_uri->host_port,
200 data => $t->collation->as_tsv,
c2b80bba 201 userid => $c->user->get_object->email,
70744367 202 parameters => $reqparams };
203
204 # Call to the appropriate URL with the request parameters.
205 my $ua = LWP::UserAgent->new();
c2b80bba 206 $c->log->debug( 'Sending request to Stemweb: ' . to_json( $stemweb_request ) );
70744367 207 my $resp = $ua->post( $STEMWEB_BASE_URL . "/algorithms/process/$algorithm/",
208 'Content-Type' => 'application/json; charset=utf-8',
209 'Content' => encode_json( $stemweb_request ) );
210 if( $resp->is_success ) {
211 # Process it
ed0ce314 212 $c->log->debug( 'Got a response from the server: '
c2b80bba 213 . decode_utf8( $resp->content ) );
70744367 214 my $stemweb_response = decode_json( $resp->content );
215 try {
216 $t->set_stemweb_jobid( $stemweb_response->{jobid} );
217 } catch( Text::Tradition::Error $e ) {
218 return _json_error( $c, 429, $e->message );
219 }
220 $c->model('Directory')->save( $t );
221 $c->stash->{'result'} = $stemweb_response;
222 $c->forward('View::JSON');
223 } elsif( $resp->code == 500 && $resp->header('Client-Warning')
224 && $resp->header('Client-Warning') eq 'Internal response' ) {
225 # The server was unavailable.
226 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
227 } else {
ed0ce314 228 return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / "
70744367 229 . $resp->content );
230 }
231}
232
233# Helper to check what permission, if any, the active user has for
234# the given tradition
235sub _check_permission {
236 my( $c, $tradition ) = @_;
237 my $user = $c->user_exists ? $c->user->get_object : undef;
238 if( $user ) {
239 return 'full' if ( $user->is_admin ||
240 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
241 }
242 # Text doesn't belong to us, so maybe it's public?
243 return 'readonly' if $tradition->public;
244
245 # ...nope. Forbidden!
246 return _json_error( $c, 403, 'You do not have permission to view this tradition.' );
247}
248
532cc23b 249# Helper to throw a JSON exception
250sub _json_error {
251 my( $c, $code, $errmsg ) = @_;
252 $c->response->status( $code );
253 $c->stash->{'result'} = { 'error' => $errmsg };
254 $c->forward('View::JSON');
255 return 0;
256}
257
c2b80bba 258sub _json_bool {
259 return $_[0] ? JSON::true : JSON::false;
260}
261
262
ed0ce314 2631;