X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fstemmaweb%2FController%2FStemweb.pm;h=8f7aa4e894f27f4bd1eb0c06ad27011ec675e822;hb=6aabefa3b8ad3b5746b5e8d857e3992b9abdf3da;hp=cab3b2e71cff07cf4e68cc7b8b0f005ab99eed3b;hpb=2c514a6fa0b3eed98e8f978a4b1fa24eb7a7457f;p=scpubgit%2Fstemmaweb.git diff --git a/lib/stemmaweb/Controller/Stemweb.pm b/lib/stemmaweb/Controller/Stemweb.pm index cab3b2e..8f7aa4e 100644 --- a/lib/stemmaweb/Controller/Stemweb.pm +++ b/lib/stemmaweb/Controller/Stemweb.pm @@ -29,7 +29,7 @@ L + { jobid: status: 0 format: result: } @@ -50,7 +50,9 @@ sub result :Local :Args(0) { my $answer; if( ref( $c->request->body ) eq 'File::Temp' ) { # Read in the file and parse that. - open( POSTDATA, $c->request->body ) or die "Failed to open post data file"; + $c->log->debug( "Request body is in a temp file" ); + open( POSTDATA, $c->request->body ) + or return _json_error( $c, 500, "Failed to open post data file" ); binmode( POSTDATA, ':utf8' ); # JSON should be all one line my $pdata = ; @@ -65,12 +67,35 @@ sub result :Local :Args(0) { } else { $answer = from_json( $c->request->body ); } + $c->log->debug( "Received push notification from Stemweb: " + . to_json( $answer ) ); return _process_stemweb_result( $c, $answer ); } else { return _json_error( $c, 403, 'Please use POST!' ); } } +=head2 available + + GET algorithms/available + +Queries the Stemweb server for available stemma generation algorithms and their +parameters. Returns the JSON answer as obtained from Stemweb. + +=cut + +sub available :Local :Args(0) { + my( $self, $c ) = @_; + my $ua = LWP::UserAgent->new(); + my $resp = $ua->get( $STEMWEB_BASE_URL . '/algorithms/available' ); + if( $resp->is_success ) { + $c->stash->{'result'} = decode_json( $resp->content ); + } else { + $c->stash->{'result'} = {}; + } + $c->forward('View::JSON'); +} + =head2 query GET stemweb/query/ @@ -111,24 +136,17 @@ sub query :Local :Args(1) { ## Helper function for parsing Stemweb result data either by push or by pull sub _process_stemweb_result { my( $c, $answer ) = @_; - # Find a tradition with the defined Stemweb job ID. - # TODO: Maybe get Stemweb to pass back the tradition ID... + # Find the specified tradition and check its job ID. my $m = $c->model('Directory'); - my @traditions; - ## STUPID HACK: unless we load the possible tradition owners - ## within scope of the scan, they will not exist when the affected - ## tradition is saved. - my @users; - $m->scan( sub{ push( @traditions, $_[0] ) - if $_[0]->$_isa('Text::Tradition') - && $_[0]->has_stemweb_jobid - && $_[0]->stemweb_jobid eq $answer->{job_id}; - push( @users, $_[0] ) if $_[0]->$_isa('Text::Tradition::User'); - } ); - if( @traditions == 1 ) { - my $tradition = shift @traditions; - if( $answer->{status} == 0 ) { - my $stemmata; + my $tradition = $m->tradition( $answer->{textid} ); + unless( $tradition ) { + return _json_error( $c, 400, "No tradition found with ID " + . $answer->{textid} ); + } + if( $answer->{status} == 0 ) { + my $stemmata; + if( $tradition->has_stemweb_jobid + && $tradition->stemweb_jobid eq $answer->{jobid} ) { try { $stemmata = $tradition->record_stemweb_result( $answer ); $m->save( $tradition ); @@ -137,6 +155,12 @@ sub _process_stemweb_result { } catch { return _json_error( $c, 500, $@ ); } + } else { + # It may be that we already received a callback meanwhile. + # Check all stemmata for the given jobid and return them. + @$stemmata = grep { $_->came_from_jobid && $_->from_jobid eq $answer->{jobid} } $tradition->stemmata; + } + if( @$stemmata ) { # If we got here, success! my @steminfo = map { { name => $_->identifier, @@ -146,23 +170,20 @@ sub _process_stemweb_result { $c->stash->{'result'} = { 'status' => 'success', 'stemmata' => \@steminfo }; - } elsif( $answer->{status} < 1 ) { - $c->stash->{'result'} = { 'status' => 'running' }; } else { - return _json_error( $c, 500, - "Stemweb failure not handled: " . $answer->{result} ); + # Hm, no stemmata found on this tradition with this jobid. + # Clear the tradition jobid so that the user can try again. + if( $tradition->has_stemweb_jobid ) { + $tradition->_clear_stemweb_jobid; + $m->save( $tradition ); + } + $c->stash->{'result'} = { status => 'notfound' }; } - } elsif( @traditions ) { - return _json_error( $c, 500, - "Multiple traditions with Stemweb job ID " . $answer->{job_id} . "!" ); + } elsif( $answer->{status} < 1 ) { + $c->stash->{'result'} = { 'status' => 'running' }; } else { - # Possible that the tradition got updated in the meantime... - if( $answer->{status} == 0 ) { - $c->stash->{'result'} = { 'status' => 'notfound' }; - } else { - return _json_error( $c, 400, - "No tradition found with Stemweb job ID " . $answer->{job_id} ); - } + return _json_error( $c, 500, + "Stemweb failure not handled: " . $answer->{result} ); } $c->forward('View::JSON'); } @@ -197,8 +218,9 @@ sub request :Local :Args(0) { my $stemweb_request = { return_path => $return_uri->path, return_host => $return_uri->host_port, - data => $t->collation->as_tsv, + data => $t->collation->as_tsv({noac => 1}), userid => $c->user->get_object->email, + textid => $tid, parameters => $reqparams }; # Call to the appropriate URL with the request parameters.