X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=web_ui.pl;h=52ad371ccd98706e97e2c23d9e3cc9fd66238b0a;hb=7bff12afddfd0c986328cd8202e425967de6b133;hp=a038fd0c93d67eac2d98c7ad74fdb030bbb608bd;hpb=347ca913c366abf9947bb9d6c6ef81e8b00b8495;p=APKPM.git diff --git a/web_ui.pl b/web_ui.pl index a038fd0..52ad371 100755 --- a/web_ui.pl +++ b/web_ui.pl @@ -1,10 +1,14 @@ #!/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 Data::Dump qw(dump); plugin 'tag_helpers'; @@ -28,6 +32,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; @@ -77,12 +82,32 @@ get '/_g/status' => sub { 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; + # re-format SQL + $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'; + + return $self->render('gnuplot', sql => $sql, img => '', gnuplot => $ret->{error} ) + if exists $ret->{error}; my $dir = $self->app->home->rel_dir('public'); @@ -95,68 +120,81 @@ 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->render('gnuplot', sql => $sql, img => "/gnuplot/$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]" |; + } + $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"; + } - + open(my $fd, '|-', 'gnuplot') || die "gnuplot: $!"; + print $fd $gnuplot; + close($fd); -Gnuplot graphs: + $gnuplot = '' unless $self->param('include_gnuplot'); - + $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name", gnuplot => $gnuplot); +}; -Low-level API tests: +get '/_redis' => sub { + my $self = shift; + + _render_jsonp( $self, Mojo::JSON->new->encode({ status => APKPM::Model->redis_status }) ); +}; -<%= link_to 'HTTP ping' => 'ping_http' %> -<%= link_to 'Gearman ping' => 'ping_g' %> +get '/user' => sub { + my $self = shift; -@@ ping.html.ep -% layout 'default'; -pong: <%= $pong %> + $self->render('user'); +}; -@@ gnuplot.html.ep -% layout 'default'; -<%= form_for gnuplot => begin %> - <%= text_area 'sql', cols => 80 %> - <%= submit_button %> -<% end %> - +open(my $pid, '>', '/tmp/apkpm.web_ui.pid'); +print $pid "$$\n"; +close $pid; -@@ layouts/default.html.ep - - <%= title %> - <%= content %> - +app->start;