modify schema for new CPE stats
[APKPM.git] / web_ui.pl
index f82f67a..52ad371 100755 (executable)
--- a/web_ui.pl
+++ b/web_ui.pl
@@ -6,6 +6,9 @@ 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';
@@ -29,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;
@@ -79,12 +83,31 @@ get '/gnuplot' => sub {
        my $self = shift;
 
        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');
 
@@ -98,22 +121,43 @@ warn "# $sql -> $name";
        my $png = "$dir/gnuplot/$name"; # FIXME
 
        my @c = @{ $ret->{columns} };
-       warn "first column not timestamp" unless $c[0] eq 'timestamp';
 
        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-%dT%H:%M:%S"
-set format x "%H:%M\\n%d"
+set timefmt "$fmt"
+
+set format x "$format_x"
+set xtics nomirror rotate by -90
 
 |;
 
+       } else { 
+               warn "first column not timestamp";
+       }
+
        my $plot = 'plot ';
+       my $with = $self->param('with') || 'dots';
        foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
                $gnuplot .= $plot; $plot = ',';
-               $gnuplot .= qq| '-' using 1:2 title "$c[$i]" |;
+               $gnuplot .= qq| '-' using 1:2 with $with title "$c[$i]" |;
        }
        $gnuplot .= "\n";
 
@@ -121,7 +165,7 @@ set format x "%H:%M\\n%d"
 
                foreach my $row ( @{ $ret->{rows} } ) {
                        my $date = $row->[0];
-                       $date =~ s/ /T/g;
+                       $date =~ s/\Q$del\E/T/g if $del;
                        $date =~ s/\.\d+//;
                        $gnuplot .= join(" ", $date, $row->[$i])."\n";
                }
@@ -132,65 +176,25 @@ set format x "%H:%M\\n%d"
        print $fd $gnuplot;
        close($fd);
 
+       $gnuplot = '' unless $self->param('include_gnuplot');
+
        $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name", gnuplot => $gnuplot);
 };
 
 get '/_redis' => sub {
        my $self = shift;
 
-       my $redis = Redis->new;
+       _render_jsonp( $self, Mojo::JSON->new->encode({ status => APKPM::Model->redis_status }) );
+};
 
-       my $status;
-       foreach my $k ( $redis->keys('poll.*') ) {
-               $status->{$k} = eval { $redis->scard($k) } || $redis->get($k);
-       }
+get '/user' => sub {
+       my $self = shift;
 
-       warn "## ", dump $status;
-       _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
+       $self->render('user');
 };
 
+open(my $pid, '>', '/tmp/apkpm.web_ui.pid');
+print $pid "$$\n";
+close $pid;
+
 app->start;
-__DATA__
-
-@@ index.html.ep
-% layout 'default';
-% title 'Gearman demo';
-
-<ul>
-<li><a href="/gearman.html#ping/127.0.0.1">Gearman</a> web interface
-<li><%= link_to 'CRM' => 'CRM.html' %> search with tabular output
-</ul>
-
-Gnuplot graphs:
-
-<ul>
-<li><a href="/gnuplot?sql=select start,ping_error,adsl_ok from poll">poll stats</a>
-<li><a href="/gnuplot?sql=select timestamp,snrtx,attntx,pwrtx,pwrrx,attnrx,snrrx from adsl where username='test36zg@h1snc'">adsl stat for single user</a>
-<li><a href="/gnuplot?sql=select timestamp,rtt from ping where ip << inet '10.17/16' order by timestamp desc limit 1000">ttl from 10.17 network</a>
-</ul>
-
-Low-level API tests:
-
-<%= link_to 'HTTP ping' => 'ping_http' %>
-<%= link_to 'Gearman ping' => 'ping_g' %>
-
-@@ ping.html.ep
-% layout 'default';
-pong: <tt><%= $pong %>
-
-@@ gnuplot.html.ep
-% layout 'default';
-<%= form_for gnuplot => begin %>
- <%= text_area 'sql', cols => 80 %>
- <%= submit_button 'execute' %>
-<% end %>
-% if ( $img ) {
-<img src="<%= $img %>">
-% }
-<pre><%= $gnuplot %></pre>
-
-@@ layouts/default.html.ep
-<!doctype html><html>
-  <head><title><%= title %></title></head>
-  <body><%= content %></body>
-</html>