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