Isolate Stemweb calls into own controller; guard against server being down. #29
tla [Tue, 12 Nov 2013 09:52:51 +0000 (10:52 +0100)]
lib/stemmaweb/Controller/Root.pm
lib/stemmaweb/Controller/Stemweb.pm
root/js/componentload.js
root/src/index.tt

index 82fde7d..0315867 100644 (file)
@@ -2,7 +2,6 @@ package stemmaweb::Controller::Root;
 use Moose;
 use namespace::autoclean;
 use JSON qw ();
-use LWP::UserAgent;
 use TryCatch;
 use XML::LibXML;
 use XML::LibXML::XPathContext;
@@ -16,8 +15,6 @@ BEGIN { extends 'Catalyst::Controller' }
 #
 __PACKAGE__->config(namespace => '');
 
-my $STEMWEB_BASE_URL = 'http://slinkola.users.cs.helsinki.fi';
-
 =head1 NAME
 
 stemmaweb::Controller::Root - Root Controller for stemmaweb
@@ -42,14 +39,6 @@ sub index :Path :Args(0) {
        if( $c->req->param('withtradition') ) {
                $c->stash->{'withtradition'} = $c->req->param('withtradition');
        }
-       # Get the current list of Stemweb algorithms
-       my $ua = LWP::UserAgent->new();
-       my $resp = $ua->get( $STEMWEB_BASE_URL . '/algorithms/available' );
-       if( $resp->is_success ) {
-               $c->stash->{'stemweb_algorithms'} = $resp->content;
-       } else {
-               $c->stash->{'stemweb_algorithms'} = '{}';
-       }
     $c->stash->{template} = 'index.tt';
 }
 
index 33c717f..2a31903 100644 (file)
@@ -75,6 +75,27 @@ sub result :Local :Args(0) {
        }
 }
 
+=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'} = $resp->content;
+       } else {
+               $c->stash->{'result'} = '{}';
+       }
+       $c->forward('View::JSON');
+}
+
 =head2 query
 
  GET stemweb/query/<jobid>
index 7551a86..06a8706 100644 (file)
@@ -551,52 +551,58 @@ $(document).ready( function() {
                        // populate the form.
                        var algorithmTypes = {};
                        var algorithmArgs = {};
-                       $.each( stemwebAlgorithms, function( i, o ) {
-                               if( o.model === 'algorithms.algorithm' ) {
-                                       // it's an algorithm.
-                                       algorithmTypes[ o.pk ] = o.fields;
-                               } else if( o.model == 'algorithms.algorithmarg' && o.fields.external ) {
-                                       // it's an option for an algorithm that we should display.
-                                       algorithmArgs[ o.pk ] = o.fields;
-                               }
-                       });
-                       $.each( algorithmTypes, function( pk, fields ) {
-                               var algopt = $('<option>').attr( 'value', pk ).append( fields.name );
-                               $('#stemweb_algorithm').append( algopt );
-                       });
-                       // Set up the relevant options for whichever algorithm is chosen.
-                       // "key" -> form name, option ID "stemweb_$key_opt"
-                       // "name" -> form label
-                       $('#stemweb_algorithm').change( function() {
-                               var pk = $(this).val();
-                               $('#stemweb_runtime_options').empty();
-                               $.each( algorithmTypes[pk].args, function( i, apk ) {
-                                       var argInfo = algorithmArgs[apk];
-                                       if( argInfo ) {
-                                               // Make the element ID
-                                               var optId = 'stemweb_' + argInfo.key + '_opt';
-                                               // Make the label
-                                               var optLabel = $('<label>').attr( 'for', optId )
-                                                       .append( argInfo.name + ": " );
-                                               var optCtrl;
-                                               var argType = argInfo.value;
-                                               if( argType === 'positive_integer' ) {
-                                                       // Make it an input field of smallish size.
-                                                       optCtrl = $('<input>').attr( 'size', 4 );
-                                               } else if ( argType === 'boolean' ) {
-                                                       // Make it a checkbox.
-                                                       optCtrl = $('<checkbox>');
-                                               }
-                                               // Add the name and element ID
-                                               optCtrl.attr( 'name', argInfo.key ).attr( 'id', optId );
-                                               // Append the label and the option itself to the form.
-                                               $('#stemweb_runtime_options').append( optLabel )
-                                                       .append( optCtrl ).append( $('<br>') );
+                       var requrl = _get_url([ "stemweb", "available" ]);
+                       $.getJSON( requrl, function( data ) {
+                               $.each( data, function( i, o ) {
+                                       if( o.model === 'algorithms.algorithm' ) {
+                                               // it's an algorithm.
+                                               algorithmTypes[ o.pk ] = o.fields;
+                                       } else if( o.model == 'algorithms.algorithmarg' && o.fields.external ) {
+                                               // it's an option for an algorithm that we should display.
+                                               algorithmArgs[ o.pk ] = o.fields;
                                        }
                                });
+                               // TODO if it is an empty object, disable Stemweb entirely.
+                               if( !jQuery.isEmptyObject( algorithmTypes ) ) {
+                                       $.each( algorithmTypes, function( pk, fields ) {
+                                               var algopt = $('<option>').attr( 'value', pk ).append( fields.name );
+                                               $('#stemweb_algorithm').append( algopt );
+                                       });
+                                       // Set up the relevant options for whichever algorithm is chosen.
+                                       // "key" -> form name, option ID "stemweb_$key_opt"
+                                       // "name" -> form label
+                                       $('#stemweb_algorithm').change( function() {
+                                               var pk = $(this).val();
+                                               $('#stemweb_runtime_options').empty();
+                                               $.each( algorithmTypes[pk].args, function( i, apk ) {
+                                                       var argInfo = algorithmArgs[apk];
+                                                       if( argInfo ) {
+                                                               // Make the element ID
+                                                               var optId = 'stemweb_' + argInfo.key + '_opt';
+                                                               // Make the label
+                                                               var optLabel = $('<label>').attr( 'for', optId )
+                                                                       .append( argInfo.name + ": " );
+                                                               var optCtrl;
+                                                               var argType = argInfo.value;
+                                                               if( argType === 'positive_integer' ) {
+                                                                       // Make it an input field of smallish size.
+                                                                       optCtrl = $('<input>').attr( 'size', 4 );
+                                                               } else if ( argType === 'boolean' ) {
+                                                                       // Make it a checkbox.
+                                                                       optCtrl = $('<checkbox>');
+                                                               }
+                                                               // Add the name and element ID
+                                                               optCtrl.attr( 'name', argInfo.key ).attr( 'id', optId );
+                                                               // Append the label and the option itself to the form.
+                                                               $('#stemweb_runtime_options').append( optLabel )
+                                                                       .append( optCtrl ).append( $('<br>') );
+                                                       }
+                                               });
+                                       });
+                                       $('#stemweb_algorithm').change();
+                               }
                        });
                        // Prime the initial options
-                       $('#stemweb_algorithm').change();
                },
                open: function(evt) {
                        $('#stemweb_run_status').empty();
index aab3963..1aaaddc 100644 (file)
@@ -6,7 +6,6 @@
 // Set global variables that must be passed by the server
 var basepath = "[% c.uri_for( '/' ) %]";
 var textOnLoad = "[% withtradition %]";
-var stemwebAlgorithms = [% stemweb_algorithms %];
     </script>
 
 [% END %]