pass gearman timeout option correctly
[APKPM.git] / web_ui.pl
index 930fa47..0331d68 100755 (executable)
--- a/web_ui.pl
+++ b/web_ui.pl
@@ -6,6 +6,9 @@ use Redis;
 use lib '/srv/MojoX-Gearman/lib';
 use MojoX::Gearman;
 
+use lib 'lib';
+use APKPM::Model;
+
 use Data::Dump qw(dump);
 
 plugin 'tag_helpers';
@@ -29,6 +32,7 @@ get '/ping_http' => sub {
 
 my $gearman = MojoX::Gearman->new; #( ioloop => Mojo::IOLoop->singleton );
 $gearman->server( $ENV{GEARMAN} || 'localhost:4730' );
+$gearman->timeout( 15 );
 
 get '/ping_g' => sub {
        my $self = shift;
@@ -75,16 +79,58 @@ get '/_g/status' => sub {
        _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
 };
 
+sub _gearman_redis {
+       my ( $job, $params, $timeout ) = @_;
+
+       my $key = $params;
+       $key =~ s/\W+/_/gs;
+       $key =~ s/\s+/_/gs;
+       $key = "sql.$key";
+       warn "# _gearman_redis $key [$timeout s]";
+
+       my $redis = Redis->new;
+       my $ret;
+
+       if ( $ret = $redis->get($key) ) {
+               warn "redis hit $key\n";
+       } else {
+               $gearman->timeout($timeout);
+               $ret = $gearman->req( 'SUBMIT_JOB', $job, '', $params );
+               die "no result for $params" unless $ret;
+
+               $redis->set($key => $ret);
+       }
+
+       $redis->expire($key => 60); # refresh redis key timeout
+
+       $ret = Mojo::JSON->new->decode( $ret );
+
+       if ( ! exists $ret->{error} && ref $ret->{columns} ne 'ARRAY' ) {
+               $ret->{error} = 'query run longer than ' . $gearman->timeout . ' s timeout';
+       }
+
+       return $ret;
+}
+
 get '/gnuplot' => sub {
        my $self = shift;
 
        my $sql = $self->param('sql');
+
+       if ( my $timeout = $self->param('timeout') ) {
+               warn "set timout to $timeout";
+               $gearman->timeout( $timeout );
+       } else {
+               $self->param( timeout => $gearman->timeout );
+       }
+
        return $self->render('gnuplot', img => '', gnuplot => '') unless $sql;
 
-       my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
-       die "no result for $sql" unless $ret;
+       # re-format SQL
+       $sql =~ s/\s+(from|where|order|limit|join)/\n$1/gs;
+       $self->param( sql => $sql );
 
-       $ret = Mojo::JSON->new->decode( $ret );
+       my $ret = _gearman_redis( 'Store_sql' => $sql, $self->param('timeout') );
 
        return $self->render('gnuplot', sql => $sql, img => '', gnuplot => $ret->{error} )
                if exists $ret->{error};
@@ -101,18 +147,38 @@ warn "# $sql -> $name";
        my $png = "$dir/gnuplot/$name"; # FIXME
 
        my @c = @{ $ret->{columns} };
-       warn "first column not timestamp" unless $c[0] eq 'timestamp';
 
        my $gnuplot = qq|
 set terminal png
 set output '$png'
+|;
+
+       my $del; # delimiter between date and time
+
+       if ( $ret->{rows}->[0]->[0] =~ m/(\d{4}-\d{2}-\d{2})(.)?(\d{2}:\d{2}:\d{2})?/ ) {
+
+               my ( $date,$time );
+               ( $date, $del, $time ) = ( $1,$2,$3 );
+
+               my $fmt = '%Y-%m-%d';
+               $fmt   .= 'T' if $del;
+               $fmt   .= '%H:%M:%S' if $time;
+
+               my $format_x = $time ? '%d %H:%M' : '%Y-%m-%d';
+               $gnuplot .= qq|
 
 set xdata time
-set timefmt "%Y-%m-%dT%H:%M:%S"
-set format x "%H:%M\\n%d"
+set timefmt "$fmt"
+
+set format x "$format_x"
+set xtics nomirror rotate by -90
 
 |;
 
+       } else { 
+               warn "first column not timestamp";
+       }
+
        my $plot = 'plot ';
        my $with = $self->param('with') || 'dots';
        foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
@@ -125,7 +191,7 @@ set format x "%H:%M\\n%d"
 
                foreach my $row ( @{ $ret->{rows} } ) {
                        my $date = $row->[0];
-                       $date =~ s/ /T/g;
+                       $date =~ s/\Q$del\E/T/g if $del;
                        $date =~ s/\.\d+//;
                        $gnuplot .= join(" ", $date, $row->[$i])."\n";
                }
@@ -144,15 +210,27 @@ set format x "%H:%M\\n%d"
 get '/_redis' => sub {
        my $self = shift;
 
-       my $redis = Redis->new;
+       _render_jsonp( $self, Mojo::JSON->new->encode({ status => APKPM::Model->redis_status }) );
+};
 
-       my $status;
-       foreach my $k ( $redis->keys('poll.*') ) {
-               $status->{$k} = eval { $redis->scard($k) } || $redis->get($k);
-       }
+get '/user' => sub {
+       my $self = shift;
 
-       warn "## ", dump $status;
-       _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
+       $self->render('user');
 };
 
+get '/psql/:command' => sub {
+       my $self = shift;
+       my $command = $self->param('command');
+       $command =~ s/^_/\\/ && warn "internal psql command $command";
+       $command = sprintf "psql -c '%s' apkpm", $command;
+       my $output = `$command`; # FIXME unsecure
+       $self->render_text( "\$ <b><tt>$command</tt></b>\n\n<pre>$output</pre>" );
+};
+
+# create pid file
+open(my $pid, '>', '/tmp/apkpm.web_ui.pid');
+print $pid "$$\n";
+close $pid;
+
 app->start;