From: tla Date: Thu, 7 Nov 2013 19:53:28 +0000 (+0100) Subject: UNTESTED allow for query of outstanding Stemweb processes and return of results. #29 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c2b80bba7c5c1620828f6aa4d7ee841bf8d9a8e4;p=scpubgit%2Fstemmaweb.git UNTESTED allow for query of outstanding Stemweb processes and return of results. #29 --- diff --git a/lib/stemmaweb/Controller/Root.pm b/lib/stemmaweb/Controller/Root.pm index f33761d..82fde7d 100644 --- a/lib/stemmaweb/Controller/Root.pm +++ b/lib/stemmaweb/Controller/Root.pm @@ -1,6 +1,7 @@ package stemmaweb::Controller::Root; use Moose; use namespace::autoclean; +use JSON qw (); use LWP::UserAgent; use TryCatch; use XML::LibXML; @@ -324,7 +325,6 @@ sub textinfo :Local :Args(1) { my $textinfo = { textid => $textid, name => $tradition->name, - #language => $tradition->language, public => $tradition->public || 0, owner => $tradition->user ? $tradition->user->email : undef, witnesses => [ map { $_->sigil } $tradition->witnesses ], @@ -332,6 +332,9 @@ sub textinfo :Local :Args(1) { if( $tradition->can('language') ) { $textinfo->{'language'} = $tradition->language; } + if( $tradition->can('stemweb_jobid') ) { + $textinfo->{'stemweb_jobid'} = $tradition->stemweb_jobid || 0; + } my @stemmasvg = map { { name => $_->identifier, directed => _json_bool( !$_->is_undirected ), diff --git a/lib/stemmaweb/Controller/Stemweb.pm b/lib/stemmaweb/Controller/Stemweb.pm index 1a8a56e..990c1ee 100644 --- a/lib/stemmaweb/Controller/Stemweb.pm +++ b/lib/stemmaweb/Controller/Stemweb.pm @@ -2,7 +2,7 @@ package stemmaweb::Controller::Stemweb; use Moose; use namespace::autoclean; use Encode qw/ decode_utf8 /; -use JSON qw/ decode_json encode_json from_json /; +use JSON; use LWP::UserAgent; use Safe::Isa; use TryCatch; @@ -56,47 +56,110 @@ sub result :Local :Args(0) { my $pdata = ; chomp $pdata; close POSTDATA; - $answer = from_json( $pdata ); + try { + $answer = from_json( $pdata ); + } catch { + return _json_error( $c, 400, + "Could not parse POST request '' $pdata '' as JSON: $@" ); + } } else { $answer = from_json( $c->request->body ); } - # Find a tradition with the defined Stemweb job ID. - # TODO: Maybe get Stemweb to pass back the tradition ID... - my $m = $c->model('Directory'); - my @traditions; - $m->scan( sub{ push( @traditions, $_[0] ) - if $_[0]->$_isa('Text::Tradition') - && $_[0]->has_stemweb_jobid - && $_[0]->stemweb_jobid eq $answer->{job_id}; - } ); - if( @traditions == 1 ) { - my $tradition = shift @traditions; - if( $answer->{status} == 0 ) { - try { - $tradition->record_stemweb_result( $answer ); - $m->save( $tradition ); - } catch( Text::Tradition::Error $e ) { - return _json_error( $c, 500, $e->message ); - } catch { - return _json_error( $c, 500, $@ ); - } - # If we got here, success! - $c->stash->{'result'} = { 'status' => 'success' }; - $c->forward('View::JSON'); - } else { - return _json_error( $c, 500, - "Stemweb failure not handled: " . $answer->{result} ); - } - } elsif( @traditions ) { + return _process_stemweb_result( $c, $answer ); + } else { + return _json_error( $c, 403, 'Please use POST!' ); + } +} + +=head2 query + + GET stemweb/query/ + +A backup method to query the stemweb server to check a particular job status. +Returns a result as in /stemweb/result above, but status can also be -1 to +indicate that the job is still running. + +=cut + +sub query :Local :Args(1) { + my( $self, $c, $jobid ) = @_; + my $ua = LWP::UserAgent->new(); + my $resp = $ua->get( $STEMWEB_BASE_URL . "/jobstatus/$jobid" ); + if( $resp->is_success ) { + # Process it + my $response = decode_utf8( $resp->content ); + $c->log->debug( "Got a response from the server: $response" ); + my $answer; + try { + $answer = from_json( $response ); + } catch { return _json_error( $c, 500, - "Multiple traditions with Stemweb job ID " . $answer->{job_id} . "!" ); + "Could not parse stemweb response '' $response '' as JSON: $@" ); + } + return _process_stemweb_result( $c, $answer ); + } elsif( $resp->code == 500 && $resp->header('Client-Warning') + && $resp->header('Client-Warning') eq 'Internal response' ) { + # The server was unavailable. + return _json_error( $c, 503, "The Stemweb server is currently unreachable." ); + } else { + return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / " + . $resp->content ); + } +} + + +## 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... + my $m = $c->model('Directory'); + my @traditions; + $m->scan( sub{ push( @traditions, $_[0] ) + if $_[0]->$_isa('Text::Tradition') + && $_[0]->has_stemweb_jobid + && $_[0]->stemweb_jobid eq $answer->{job_id}; + } ); + if( @traditions == 1 ) { + my $tradition = shift @traditions; + if( $answer->{status} == 0 ) { + my $stemmata; + try { + $stemmata = $tradition->record_stemweb_result( $answer ); + $m->save( $tradition ); + } catch( Text::Tradition::Error $e ) { + return _json_error( $c, 500, $e->message ); + } catch { + return _json_error( $c, 500, $@ ); + } + # If we got here, success! + my @steminfo = map { { + name => $_->identifier, + directed => _json_bool( !$_->is_undirected ), + svg => $_->as_svg() } } + $stemmata; + $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} ); + } + } elsif( @traditions ) { + return _json_error( $c, 500, + "Multiple traditions with Stemweb job ID " . $answer->{job_id} . "!" ); + } 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} ); } - } else { - return _json_error( $c, 403, 'Please use POST!' ); } + $c->forward('View::JSON'); } =head2 request @@ -130,19 +193,19 @@ sub request :Local :Args(0) { return_path => $return_uri->path, return_host => $return_uri->host_port, data => $t->collation->as_tsv, - userid => $t->user->email, + userid => $c->user->get_object->email, parameters => $reqparams }; # Call to the appropriate URL with the request parameters. - $DB::single = 1; my $ua = LWP::UserAgent->new(); + $c->log->debug( 'Sending request to Stemweb: ' . to_json( $stemweb_request ) ); my $resp = $ua->post( $STEMWEB_BASE_URL . "/algorithms/process/$algorithm/", 'Content-Type' => 'application/json; charset=utf-8', 'Content' => encode_json( $stemweb_request ) ); if( $resp->is_success ) { # Process it $c->log->debug( 'Got a response from the server: ' - . decode_utf8( $stemweb_response ) ); + . decode_utf8( $resp->content ) ); my $stemweb_response = decode_json( $resp->content ); try { $t->set_stemweb_jobid( $stemweb_response->{jobid} ); @@ -187,4 +250,9 @@ sub _json_error { return 0; } +sub _json_bool { + return $_[0] ? JSON::true : JSON::false; +} + + 1; diff --git a/root/js/componentload.js b/root/js/componentload.js index e804204..838da46 100644 --- a/root/js/componentload.js +++ b/root/js/componentload.js @@ -45,12 +45,12 @@ function loadTradition( textid, textname, editable ) { // Hide the functionality that is irrelevant if( editable ) { $('#open_stemma_add').show(); - $('#open_stemweb_ui').show(); $('#open_textinfo_edit').show(); $('#relatebutton_label').text('View collation and edit relationships'); } else { $('#open_stemma_add').hide(); $('#open_stemweb_ui').hide(); + $('#query_stemweb_ui').hide(); $('#open_textinfo_edit').hide(); $('#relatebutton_label').text('View collation and relationships'); } @@ -133,6 +133,14 @@ function load_stemma( idx, editable ) { $('#open_stemma_edit').hide(); $('#run_stexaminer').hide(); $('#stemma_identifier').empty(); + // Add the relevant Stemweb functionality + if( editable ) { + if( selectedTextInfo.stemweb_jobid == 0 ) { + $('#open_stemweb_ui').show(); + } else { + $('#query_stemweb_ui').show(); + } + } if( idx > -1 ) { // Load the stemma and its properties var stemmadata = stemmata[idx]; @@ -151,6 +159,32 @@ function load_stemma( idx, editable ) { } } +function query_stemweb_progress() { + var requrl = _get_url([ "stemweb", "query", selectedTextInfo.stemweb_jobid ]); + $.getJSON( requrl, function (data) { + // Look for a status message, either success, running, or notfound. + if( data.status === 'success' ) { + // Add the new stemmata to the textinfo and tell the user. + if( data.stemmata.length > 0 ) { + stemmata = stemmata.concat( data.stemmata ); + if( selectedStemmaID == -1 ) { + // We have a stemma for the first time; load the first one. + load_stemma( 0 ); + } + alert( 'You have one or more new stemmata!' ); + } else { + alert( 'Stemweb run finished with no stemmata...huh?!' ); + } + } else if( data.status === 'running' ) { + // Just tell the user. + alert( 'Your Stemweb query is still running!' ); + } else if( data.status === 'notfound' ) { + // Ask the user to refresh, for now. + alert( 'Your Stemweb query probably finished and reported back. Please reload to check.' ); + } + }); +} + // Load the SVG we are given function loadSVG(svgData) { var svgElement = $('#stemma_graph'); diff --git a/root/src/index.tt b/root/src/index.tt index c0daf8e..c5f6373 100644 --- a/root/src/index.tt +++ b/root/src/index.tt @@ -73,6 +73,12 @@ var stemwebAlgorithms = [% stemweb_algorithms %]; Run a Stemweb algorithm +
+
+ Check progress of Stemweb algorithm +
+
Examine variants against this stemma