X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=web_ui.pl;h=0331d68db4527ce54fc6591d1e23be4e21be543e;hb=a491d4bc0d4a71afd3c04d7cfbcf9fad45a63833;hp=7eb6563e66d2e3c1700f6f960d185726963c0654;hpb=88d1020a899bc37d7cea0cb2c20e2af0fff56d34;p=APKPM.git diff --git a/web_ui.pl b/web_ui.pl index 7eb6563..0331d68 100755 --- a/web_ui.pl +++ b/web_ui.pl @@ -79,6 +79,39 @@ 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; @@ -97,14 +130,7 @@ get '/gnuplot' => sub { $sql =~ s/\s+(from|where|order|limit|join)/\n$1/gs; $self->param( sql => $sql ); - $gearman->timeout(15); - 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' - if ! exists $ret->{error} && ref $ret->{columns} ne 'ARRAY'; + my $ret = _gearman_redis( 'Store_sql' => $sql, $self->param('timeout') ); return $self->render('gnuplot', sql => $sql, img => '', gnuplot => $ret->{error} ) if exists $ret->{error}; @@ -193,5 +219,18 @@ get '/user' => sub { $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( "\$ $command\n\n
$output
" ); +}; + +# create pid file +open(my $pid, '>', '/tmp/apkpm.web_ui.pid'); +print $pid "$$\n"; +close $pid; app->start;