draw multiple columns from sql queries
[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') || die "sql required";
82
83         my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
84         die "no result for $sql" unless $ret;
85
86         $ret = Mojo::JSON->new->decode( $ret );
87
88         my $dir = $self->app->home->rel_dir('public');
89
90         my $name = $sql;
91         $name =~ s/\W+//gs;
92         $name .= '.png';
93
94 warn "# $sql -> $name";
95
96         mkdir "$dir/gnuplot" unless -e "$dir/gnuplot";
97         my $png = "$dir/gnuplot/$name"; # FIXME
98
99         my @c = @{ $ret->{columns} };
100         warn "first column not timestamp" unless $c[0] eq 'timestamp';
101
102         my $gnuplot = qq|
103 set terminal png
104 set output '$png'
105
106 set xdata time
107 set timefmt "%Y-%m-%dT%H:%M:%S"
108 set format x "%H:%M\\n%d"
109
110 |;
111
112         my $plot = 'plot ';
113         foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
114                 $gnuplot .= $plot; $plot = ',';
115                 $gnuplot .= qq| '-' using 1:2 title "$c[$i]" |;
116         }
117         $gnuplot .= "\n";
118
119         foreach my $i ( 1 .. $#{ $ret->{rows}->[0] } ) {
120
121                 foreach my $row ( @{ $ret->{rows} } ) {
122                         my $date = $row->[0];
123                         $date =~ s/ /T/g;
124                         $date =~ s/\.\d+//;
125                         $gnuplot .= join(" ", $date, $row->[$i])."\n";
126                 }
127                 $gnuplot .= "e\n";
128         }
129
130         open(my $fd, '|-', 'gnuplot') || die "gnuplot: $!";
131         print $fd $gnuplot;
132         close($fd);
133
134         $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name", gnuplot => $gnuplot);
135 };
136
137 get '/_redis' => sub {
138         my $self = shift;
139
140         my $redis = Redis->new;
141
142         my $status;
143         $status->{$_} = $redis->scard($_) foreach $redis->keys('poll.*');
144
145         warn "## ", dump $status;
146         _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
147 };
148
149 app->start;
150 __DATA__
151
152 @@ index.html.ep
153 % layout 'default';
154 % title 'Gearman demo';
155
156 <ul>
157 <li><a href="/gearman.html#ping/127.0.0.1">Gearman</a> web interface
158 <li><%= link_to 'CRM' => 'CRM.html' %> search with tabular output
159 </ul>
160
161 Gnuplot graphs:
162
163 <ul>
164 <li><a href="/gnuplot?sql=select start,ping_error,adsl_ok from poll">poll stats</a>
165 <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>
166 </ul>
167
168 Low-level API tests:
169
170 <%= link_to 'HTTP ping' => 'ping_http' %>
171 <%= link_to 'Gearman ping' => 'ping_g' %>
172
173 @@ ping.html.ep
174 % layout 'default';
175 pong: <tt><%= $pong %>
176
177 @@ gnuplot.html.ep
178 % layout 'default';
179 <%= form_for gnuplot => begin %>
180  <%= text_area 'sql', cols => 80 %>
181  <%= submit_button %>
182 <% end %>
183 <img src="<%= $img %>">
184 <pre><%= $gnuplot %></pre>
185
186 @@ layouts/default.html.ep
187 <!doctype html><html>
188   <head><title><%= title %></title></head>
189   <body><%= content %></body>
190 </html>