clear before fetch to remove selection
[APKPM.git] / web_ui.pl
1 #!/usr/bin/env perl
2
3 use Mojolicious::Lite;
4 use Redis;
5
6 use lib '/srv/MojoX-Gearman/lib';
7 use MojoX::Gearman;
8
9 use Data::Dump qw(dump);
10
11 plugin 'tag_helpers';
12 # Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
13 plugin 'pod_renderer';
14
15 app->secret('apkpm');
16
17 get '/' => sub {
18         my $self = shift;
19         $self->render('index');
20 };
21
22 get '/ping_http' => sub {
23         my $self = shift;
24         my $gearman = $self->client;
25         my $pong = $gearman->post( "http://localhost:4780/ping" => { Connection => 'close' } => "some data" )->res->body;
26         warn "ping = $pong";
27         $self->render( 'ping', pong => $pong );
28 };
29
30 my $gearman = MojoX::Gearman->new; #( ioloop => Mojo::IOLoop->singleton );
31 $gearman->server( $ENV{GEARMAN} || 'localhost:4730' );
32
33 get '/ping_g' => sub {
34         my $self = shift;
35         my $pong = $gearman->req( 'SUBMIT_JOB', 'ping', '', "some data 2" );
36         warn "ping = $pong";
37         $self->render( 'ping', pong => $pong );
38 };
39
40 sub _render_jsonp {
41         my ( $self, $json ) = @_;
42         #my $data = $self->render( json => $json, partial => 1 );
43         my $data = $json;
44         if ( my $callback = $self->param('callback') ) {
45                 $data = "$callback($data)";
46         }
47         $self->render( data => $data, format => 'js' );
48 }
49
50
51 get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
52         my $self = shift;
53         my $ret = $gearman->req( 'SUBMIT_JOB', $self->param('call'), '', $self->param('args') );
54         warn $self->param('call'), " = ", dump($ret), "\n";
55         die "no result for ", $self->param('call'), ' args: ', $self->param('args') unless defined $ret;
56         _render_jsonp( $self, $ret );
57 };
58
59 get '/_g/status' => sub {
60         my $self = shift;
61
62         my $ret = $gearman->req( 'TEXT' => 'status' );
63         warn "# status:\n$ret";
64
65         my @c = qw(function total running available);
66
67         my $status;
68         foreach my $l ( split(/\n/,$ret) ) {
69                 my @v = split(/\t/, $l);
70                 my $h;
71                 $h->{$c[$_]} = $v[$_] foreach 0 .. $#v;
72                 push @$status, $h;
73         }
74         warn "## ", dump $status;
75         _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
76 };
77
78 get '/gnuplot' => sub {
79         my $self = shift;
80
81         my $sql = $self->param('sql');
82
83         if ( my $timeout = $self->param('timeout') ) {
84                 warn "set timout to $timeout";
85                 $gearman->timeout( $timeout );
86         } else {
87                 $self->param( timeout => $gearman->timeout );
88         }
89
90         return $self->render('gnuplot', img => '', gnuplot => '') unless $sql;
91
92         # re-format SQL
93         $sql =~ s/\s+(from|where|order|limit|join)/\n$1/gs;
94         $self->param( sql => $sql );
95
96         my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
97         die "no result for $sql" unless $ret;
98
99         $ret = Mojo::JSON->new->decode( $ret );
100         
101         $ret->{error} = 'query run longer than ' . $gearman->timeout . ' s timeout'
102                 if ! exists $ret->{error} && ref $ret->{columns} ne 'ARRAY';
103
104         return $self->render('gnuplot', sql => $sql, img => '', gnuplot => $ret->{error} )
105                 if exists $ret->{error};
106
107         my $dir = $self->app->home->rel_dir('public');
108
109         my $name = $sql;
110         $name =~ s/\W+//gs;
111         $name .= '.png';
112
113 warn "# $sql -> $name";
114
115         mkdir "$dir/gnuplot" unless -e "$dir/gnuplot";
116         my $png = "$dir/gnuplot/$name"; # FIXME
117
118         my @c = @{ $ret->{columns} };
119
120         my $gnuplot = qq|
121 set terminal png
122 set output '$png'
123 |;
124
125         my $del; # delimiter between date and time
126
127         if ( $ret->{rows}->[0]->[0] =~ m/(\d{4}-\d{2}-\d{2})(.)?(\d{2}:\d{2}:\d{2})?/ ) {
128
129                 my ( $date,$time );
130                 ( $date, $del, $time ) = ( $1,$2,$3 );
131
132                 my $fmt = '%Y-%m-%d';
133                 $fmt   .= 'T' if $del;
134                 $fmt   .= '%H:%M:%S' if $time;
135
136                 my $format_x = $time ? '%d %H:%M' : '%Y-%m-%d';
137                 $gnuplot .= qq|
138
139 set xdata time
140 set timefmt "$fmt"
141
142 set format x "$format_x"
143 set xtics nomirror rotate by -90
144
145 |;
146
147         } else { 
148                 warn "first column not timestamp";
149         }
150
151         my $plot = 'plot ';
152         my $with = $self->param('with') || 'dots';
153         foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
154                 $gnuplot .= $plot; $plot = ',';
155                 $gnuplot .= qq| '-' using 1:2 with $with title "$c[$i]" |;
156         }
157         $gnuplot .= "\n";
158
159         foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
160
161                 foreach my $row ( @{ $ret->{rows} } ) {
162                         my $date = $row->[0];
163                         $date =~ s/\Q$del\E/T/g if $del;
164                         $date =~ s/\.\d+//;
165                         $gnuplot .= join(" ", $date, $row->[$i])."\n";
166                 }
167                 $gnuplot .= "e\n";
168         }
169
170         open(my $fd, '|-', 'gnuplot') || die "gnuplot: $!";
171         print $fd $gnuplot;
172         close($fd);
173
174         $gnuplot = '' unless $self->param('include_gnuplot');
175
176         $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name", gnuplot => $gnuplot);
177 };
178
179 get '/_redis' => sub {
180         my $self = shift;
181
182         my $redis = Redis->new;
183
184         my $status;
185         foreach my $k ( $redis->keys('poll.*') ) {
186                 $status->{$k} = eval { $redis->scard($k) } || $redis->get($k);
187         }
188
189         warn "## ", dump $status;
190         _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
191 };
192
193 get '/user' => sub {
194         my $self = shift;
195
196         $self->render('user');
197 };
198
199
200 app->start;