use do_high for CRM_search
[APKPM.git] / web_ui.pl
index 4dbef87..36d00de 100755 (executable)
--- a/web_ui.pl
+++ b/web_ui.pl
@@ -77,13 +77,40 @@ get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
 
 get '/table/:table' => sub {
        my $self = shift;
-       my $sql = "select * from " . $self->param('table');
-       if ( my $username = $self->param('username') ) {
-               $sql .= " where username = '$username' ";
-       }
-       $sql .= " limit " . ( $self->param('limit') || 1 );
 
-       my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
+       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("pg.$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 );
 };
 
@@ -116,26 +143,25 @@ sub _gearman_redis {
        warn "# _gearman_redis $key [$timeout s]";
 
        my $redis = Redis->new;
-       my $ret;
+       my $json;
 
-       if ( $ret = $redis->get($key) ) {
+       if ( $json = $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);
+               $json = $gearman->req( 'SUBMIT_JOB', $job, '', $params );
+               die "no result for $params" unless $json;
        }
 
-       $redis->expire($key => 60); # refresh redis key timeout
-
-       $ret = Mojo::JSON->new->decode( $ret );
+       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;
 }
 
@@ -164,8 +190,11 @@ get '/gnuplot' => sub {
 
        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";
@@ -207,7 +236,6 @@ set xtics nomirror rotate by -90
        }
 
        my $plot = 'plot ';
-       my $with = $self->param('with') || 'dots';
        foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
                $gnuplot .= $plot; $plot = ',';
                $gnuplot .= qq| '-' using 1:2 with $with title "$c[$i]" |;