6 use lib '/srv/MojoX-Gearman/lib';
14 use Data::Dump qw(dump);
17 # Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
18 plugin 'pod_renderer';
24 $self->render('index');
27 get '/ping_http' => sub {
29 my $gearman = $self->client;
30 my $pong = $gearman->post( "http://localhost:4780/ping" => { Connection => 'close' } => "some data" )->res->body;
32 $self->render( 'ping', pong => $pong );
35 my $gearman = MojoX::Gearman->new; #( ioloop => Mojo::IOLoop->singleton );
36 $gearman->server( $ENV{GEARMAN} || 'localhost:4730' );
37 $gearman->timeout( 15 );
39 get '/ping_g' => sub {
41 my $pong = $gearman->req( 'SUBMIT_JOB', 'ping', '', "some data 2" );
43 $self->render( 'ping', pong => $pong );
47 my ( $self, $json ) = @_;
48 #my $data = $self->render( json => $json, partial => 1 );
50 if ( my $callback = $self->param('callback') ) {
51 $data = "$callback($data)";
53 $self->render( data => $data, format => 'js' );
57 get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
60 my $call = $self->param('call');
61 my $args = $self->param('args');
63 if ( $call =~ m/^(LDAP|CRM)_search/ ) {
64 my $k = $1 . '.*' . $args . '*';
65 my $redis = Redis->new;
66 if ( my @k = $redis->keys( $k ) ) {
67 my $ret = join(',', map { $redis->get($_) } @k );
68 return _render_jsonp( $self, "[$ret]" );
72 my $ret = $gearman->req( 'SUBMIT_JOB', $call, '', $args );
73 warn "$call = ", dump($ret), "\n";
74 die "no result for $call args: $args" unless defined $ret;
75 _render_jsonp( $self, $ret );
78 get '/table/:table' => sub {
81 my $table = $self->param('table');
82 my $username = $self->param('username');
83 my $limit = $self->param('limit') || 1;
85 warn "/table/$table $username $limit";
87 my $sql = "select * from $table";
88 $sql .= " where username = '$username'" if $username;
89 $sql .= " limit $limit";
93 my $redis = Redis->new;
94 my $json = Mojo::JSON->new->decode( $redis->get( "table.$table.$username" ) );
95 warn "redis hit table.$table.$username";
97 # generate result as Store_sql does
99 my @c = split(/\s+/, $redis->get("pg.$table"));
100 my ($hash_col) = grep { $c[$_] eq 'h' } 0 .. $#c;
104 hash_col => $hash_col,
105 rows => [[ map { $json->{$_} } @c ]],
107 $ret = Mojo::JSON->new->encode($ret);
110 $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
114 _render_jsonp( $self, $ret );
117 get '/_g/status' => sub {
120 my $ret = $gearman->req( 'TEXT' => 'status' );
121 warn "# status:\n$ret";
123 my @c = qw(function total running available);
126 foreach my $l ( split(/\n/,$ret) ) {
127 my @v = split(/\t/, $l);
129 $h->{$c[$_]} = $v[$_] foreach 0 .. $#v;
132 warn "## ", dump $status;
133 _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
137 my ( $job, $params, $timeout ) = @_;
143 warn "# _gearman_redis $key [$timeout s]";
145 my $redis = Redis->new;
148 if ( $ret = $redis->get($key) ) {
149 warn "redis hit $key\n";
151 $gearman->timeout($timeout);
152 $ret = $gearman->req( 'SUBMIT_JOB', $job, '', $params );
153 die "no result for $params" unless $ret;
155 $redis->set($key => $ret);
158 $redis->expire($key => 60); # refresh redis key timeout
160 $ret = Mojo::JSON->new->decode( $ret );
162 if ( ! exists $ret->{error} && ref $ret->{columns} ne 'ARRAY' ) {
163 $ret->{error} = 'query run longer than ' . $gearman->timeout . ' s timeout';
169 get '/gnuplot' => sub {
172 my $sql = $self->param('sql');
174 if ( my $timeout = $self->param('timeout') ) {
175 warn "set timout to $timeout";
176 $gearman->timeout( $timeout );
178 $self->param( timeout => $gearman->timeout );
181 return $self->render('gnuplot', img => '', gnuplot => '') unless $sql;
184 $sql =~ s/\s+(from|where|order|limit|join)/\n$1/gs;
185 $self->param( sql => $sql );
187 my $ret = _gearman_redis( 'Store_sql' => $sql, $self->param('timeout') );
189 return $self->render('gnuplot', sql => $sql, img => '', gnuplot => $ret->{error} )
190 if exists $ret->{error};
192 my $dir = $self->app->home->rel_dir('public');
198 warn "# $sql -> $name";
200 mkdir "$dir/gnuplot" unless -e "$dir/gnuplot";
201 my $png = "$dir/gnuplot/$name"; # FIXME
203 my @c = @{ $ret->{columns} };
210 my $del; # delimiter between date and time
212 if ( $ret->{rows}->[0]->[0] =~ m/(\d{4}-\d{2}-\d{2})(.)?(\d{2}:\d{2}:\d{2})?/ ) {
215 ( $date, $del, $time ) = ( $1,$2,$3 );
217 my $fmt = '%Y-%m-%d';
219 $fmt .= '%H:%M:%S' if $time;
221 my $format_x = $time ? '%d %H:%M' : '%Y-%m-%d';
227 set format x "$format_x"
228 set xtics nomirror rotate by -90
233 warn "first column not timestamp";
237 my $with = $self->param('with') || 'dots';
238 foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
239 $gnuplot .= $plot; $plot = ',';
240 $gnuplot .= qq| '-' using 1:2 with $with title "$c[$i]" |;
244 foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
246 foreach my $row ( @{ $ret->{rows} } ) {
247 my $date = $row->[0];
248 $date =~ s/\Q$del\E/T/g if $del;
250 $gnuplot .= join(" ", $date, $row->[$i])."\n";
255 open(my $fd, '|-', 'gnuplot') || die "gnuplot: $!";
259 $gnuplot = '' unless $self->param('include_gnuplot');
261 $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name", gnuplot => $gnuplot);
264 get '/_redis' => sub {
267 _render_jsonp( $self, Mojo::JSON->new->encode({ status => APKPM::Model->redis_status }) );
273 $self->render('user');
276 get '/psql/:command' => sub {
278 my $command = $self->param('command');
279 $command =~ s/^_/\\/ && warn "internal psql command $command";
280 $command = sprintf "psql -c '%s' apkpm", $command;
281 my $output = `$command`; # FIXME unsecure
282 $self->render_text( "\$ <b><tt>$command</tt></b>\n\n<pre>$output</pre>" );
286 open(my $pid, '>', '/tmp/apkpm.web_ui.pid');