my $ret;
if ( $limit == 1 ) {
my $redis = Redis->new;
- $ret = $redis->get( "table.$table.$username" );
+ 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_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;
}
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";
}
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]" |;