Merge branch 'devel' of h1dev:/srv/APKPM/
[APKPM.git] / web_ui.pl
index 84a983f..19f435f 100755 (executable)
--- a/web_ui.pl
+++ b/web_ui.pl
@@ -1,15 +1,24 @@
 #!/usr/bin/env perl
 
 use Mojolicious::Lite;
+use Redis;
 
 use lib '/srv/MojoX-Gearman/lib';
 use MojoX::Gearman;
 
+use lib 'lib';
+use APKPM::Model;
+use Redis;
+use Encode;
+
 use Data::Dump qw(dump);
 
+plugin 'tag_helpers';
 # Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
 plugin 'pod_renderer';
 
+app->secret('apkpm');
+
 get '/' => sub {
        my $self = shift;
        $self->render('index');
@@ -25,6 +34,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;
@@ -46,9 +56,61 @@ sub _render_jsonp {
 
 get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
        my $self = shift;
-       my $ret = $gearman->req( 'SUBMIT_JOB', $self->param('call'), '', $self->param('args') );
-       warn $self->param('call'), " = ", dump($ret), "\n";
-       die "no result for ", $self->param('call'), ' args: ', $self->param('args') unless defined $ret;
+
+       my $call = $self->param('call');
+       my $args = $self->param('args');
+
+       if ( $call =~ m/^(LDAP|CRM)_search/ ) {
+               my $k = $1 . '.*' . $args . '*';
+               my $redis = Redis->new;
+               if ( my @k = $redis->keys( $k ) ) {
+                       my $ret = join(',', map { $redis->get($_) } @k );
+                       return _render_jsonp( $self, "[$ret]" );
+               }
+       }
+
+       my $ret = $gearman->req( 'SUBMIT_JOB', $call, '', $args );
+       warn "$call = ", dump($ret), "\n";
+       die "no result for $call args: $args" unless defined $ret;
+       _render_jsonp( $self, $ret );
+};
+
+get '/table/:table' => sub {
+       my $self = shift;
+
+       my $table = $self->param('table');
+       my $username = $self->param('username');
+       my $limit = $self->param('limit') || 1;
+
+       warn "/table/$table $username $limit";
+
+       my $sql = "select * from $table";
+       $sql .= " where username = '$username'" if $username;
+       $sql .= " limit $limit";
+
+       my $ret;
+       if ( $limit == 1 ) {
+               my $redis = Redis->new;
+               my $json = Mojo::JSON->new->decode( $redis->get( "table.$table.$username" ) );
+               warn "redis hit table.$table.$username";
+
+               # generate result as Store_sql does
+
+               my @c = split(/\s+/, $redis->get("columns.$table"));
+               my ($hash_col) = grep { $c[$_] eq 'h' } 0 .. $#c;
+
+               $ret = {
+                       columns => \@c,
+                       hash_col => $hash_col,
+                       rows => [[ map { $json->{$_} } @c ]],
+               };
+               $ret = Mojo::JSON->new->encode($ret);
+       }
+       if ( ! $ret ) {
+               $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
+               warn "gearman $sql";
+       }
+       warn ">>> $ret";
        _render_jsonp( $self, $ret );
 };
 
@@ -71,20 +133,68 @@ 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 $json;
+
+       if ( $json = $redis->get($key) ) {
+               warn "redis hit $key\n";
+       } else {
+               $gearman->timeout($timeout);
+               $json = $gearman->req( 'SUBMIT_JOB', $job, '', $params );
+               die "no result for $params" unless $json;
+       }
+
+       my $ret = Mojo::JSON->new->decode( $json );
+
+       if ( ! exists $ret->{error} && ref $ret->{columns} ne 'ARRAY' ) {
+               $ret->{error} = 'query run longer than ' . $gearman->timeout . ' s timeout';
+       }
+
+       $redis->set($key => $json);
+       $redis->expire($key => 60); # refresh redis key timeout
+
+       return $ret;
+}
+
 get '/gnuplot' => sub {
        my $self = shift;
 
-       my $sql = $self->param('sql') || die "sql required";
+       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};
 
        my $dir = $self->app->home->rel_dir('public');
 
+       my $with = $self->param('with') || 'dots';
+
        my $name = $sql;
        $name =~ s/\W+//gs;
+       $name .= $with;
        $name .= '.png';
 
 warn "# $sql -> $name";
@@ -92,64 +202,90 @@ warn "# $sql -> $name";
        mkdir "$dir/gnuplot" unless -e "$dir/gnuplot";
        my $png = "$dir/gnuplot/$name"; # FIXME
 
-       open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot: $!";
-       open($gnuplot, '>', '/tmp/gnuplot') if $self->param('debug');
-
        my @c = @{ $ret->{columns} };
-       warn "first column not timestamp" unless shift @c eq 'timestamp';
 
-       print $gnuplot qq|
+       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-%d %H:%M:%S"
-set format x "%H%M%S"
+set timefmt "$fmt"
+
+set format x "$format_x"
+set xtics nomirror rotate by -90
 
-plot '-' using 1:3 title "$c[0]"
 |;
-       foreach my $row ( @{ $ret->{rows} } ) {
-               print $gnuplot join("\t", @$row),"\n";
+
+       } else { 
+               warn "first column not timestamp";
        }
-       print $gnuplot "e\n";
-       close($gnuplot);
 
-       $self->redirect_to("/gnuplot/$name");
-};
+       my $plot = 'plot ';
+       foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
+               $gnuplot .= $plot; $plot = ',';
+               $gnuplot .= qq| '-' using 1:2 with $with title "$c[$i]" |;
+       }
+       $gnuplot .= "\n";
 
-app->start;
-__DATA__
+       foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
 
-@@ index.html.ep
-% layout 'default';
-% title 'Gearman demo';
+               foreach my $row ( @{ $ret->{rows} } ) {
+                       my $date = $row->[0];
+                       $date =~ s/\Q$del\E/T/g if $del;
+                       $date =~ s/\.\d+//;
+                       $gnuplot .= join(" ", $date, $row->[$i])."\n";
+               }
+               $gnuplot .= "e\n";
+       }
 
-<ul>
-<li><a href="/gearman.html#ping/127.0.0.1">Gearman</a> web interface
-<li><%= link_to 'CRM' => 'CRM.html' %> search with tabular output
-</ul>
+       open(my $fd, '|-', 'gnuplot') || die "gnuplot: $!";
+       print $fd $gnuplot;
+       close($fd);
+
+       $gnuplot = '' unless $self->param('include_gnuplot');
+
+       $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name", gnuplot => $gnuplot);
+};
 
-Gnuplot graphs:
+get '/_redis' => sub {
+       my $self = shift;
 
-<ul>
-<li><a href="/gnuplot?sql=select timestamp,rtt from ping where ip << inet '10.17/16' order by timestamp desc limit 1000">ttl from 10.17 network</a>
-</ul>
+       _render_jsonp( $self, Mojo::JSON->new->encode({ status => APKPM::Model->redis_status }) );
+};
 
-Low-level API tests:
+get '/user' => sub {
+       my $self = shift;
 
-<%= link_to 'HTTP ping' => 'ping_http' %>
-<%= link_to 'Gearman ping' => 'ping_g' %>
+       $self->render('user');
+};
 
-@@ ping.html.ep
-% layout 'default';
-pong: <tt><%= $pong %>
+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>" );
+};
 
-@@ dump.html.ep
-% layout 'default';
-<pre><%= $dump %></pre>
+# create pid file
+open(my $pid, '>', '/tmp/apkpm.web_ui.pid');
+print $pid "$$\n";
+close $pid;
 
-@@ layouts/default.html.ep
-<!doctype html><html>
-  <head><title><%= title %></title></head>
-  <body><%= content %></body>
-</html>
+app->start;