configure timeout and display error about it
[APKPM.git] / web_ui.pl
index 742252f..21553c2 100755 (executable)
--- a/web_ui.pl
+++ b/web_ui.pl
@@ -81,11 +81,18 @@ get '/gnuplot' => sub {
        my $sql = $self->param('sql');
        return $self->render('gnuplot', img => '', gnuplot => '') unless $sql;
 
+       # re-format SQL
+       $sql =~ s/\s+(from|where|order|limit|join)/\n$1/gs;
+       $self->param( sql => $sql );
+
+       $gearman->timeout( $self->param('timeout') );
        my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
        die "no result for $sql" unless $ret;
 
        $ret = Mojo::JSON->new->decode( $ret );
 
+       $ret->{error} = 'query run longer than ' . $gearman->timeout . ' s timeout' unless ref $ret->{columns} eq 'ARRAY';
+
        return $self->render('gnuplot', sql => $sql, img => '', gnuplot => $ret->{error} )
                if exists $ret->{error};
 
@@ -136,6 +143,8 @@ set format x "%H:%M\\n%d"
        print $fd $gnuplot;
        close($fd);
 
+       $gnuplot = '' unless $self->param('include_gnuplot');
+
        $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name", gnuplot => $gnuplot);
 };