a038fd0c93d67eac2d98c7ad74fdb030bbb608bd
[APKPM.git] / web_ui.pl
1 #!/usr/bin/env perl
2
3 use Mojolicious::Lite;
4
5 use lib '/srv/MojoX-Gearman/lib';
6 use MojoX::Gearman;
7
8 use Data::Dump qw(dump);
9
10 plugin 'tag_helpers';
11 # Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
12 plugin 'pod_renderer';
13
14 app->secret('apkpm');
15
16 get '/' => sub {
17         my $self = shift;
18         $self->render('index');
19 };
20
21 get '/ping_http' => sub {
22         my $self = shift;
23         my $gearman = $self->client;
24         my $pong = $gearman->post( "http://localhost:4780/ping" => { Connection => 'close' } => "some data" )->res->body;
25         warn "ping = $pong";
26         $self->render( 'ping', pong => $pong );
27 };
28
29 my $gearman = MojoX::Gearman->new; #( ioloop => Mojo::IOLoop->singleton );
30 $gearman->server( $ENV{GEARMAN} || 'localhost:4730' );
31
32 get '/ping_g' => sub {
33         my $self = shift;
34         my $pong = $gearman->req( 'SUBMIT_JOB', 'ping', '', "some data 2" );
35         warn "ping = $pong";
36         $self->render( 'ping', pong => $pong );
37 };
38
39 sub _render_jsonp {
40         my ( $self, $json ) = @_;
41         #my $data = $self->render( json => $json, partial => 1 );
42         my $data = $json;
43         if ( my $callback = $self->param('callback') ) {
44                 $data = "$callback($data)";
45         }
46         $self->render( data => $data, format => 'js' );
47 }
48
49
50 get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
51         my $self = shift;
52         my $ret = $gearman->req( 'SUBMIT_JOB', $self->param('call'), '', $self->param('args') );
53         warn $self->param('call'), " = ", dump($ret), "\n";
54         die "no result for ", $self->param('call'), ' args: ', $self->param('args') unless defined $ret;
55         _render_jsonp( $self, $ret );
56 };
57
58 get '/_g/status' => sub {
59         my $self = shift;
60
61         my $ret = $gearman->req( 'TEXT' => 'status' );
62         warn "# status:\n$ret";
63
64         my @c = qw(function total running available);
65
66         my $status;
67         foreach my $l ( split(/\n/,$ret) ) {
68                 my @v = split(/\t/, $l);
69                 my $h;
70                 $h->{$c[$_]} = $v[$_] foreach 0 .. $#v;
71                 push @$status, $h;
72         }
73         warn "## ", dump $status;
74         _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
75 };
76
77 get '/gnuplot' => sub {
78         my $self = shift;
79
80         my $sql = $self->param('sql') || die "sql required";
81
82         my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
83         die "no result for $sql" unless $ret;
84
85         $ret = Mojo::JSON->new->decode( $ret );
86
87         my $dir = $self->app->home->rel_dir('public');
88
89         my $name = $sql;
90         $name =~ s/\W+//gs;
91         $name .= '.png';
92
93 warn "# $sql -> $name";
94
95         mkdir "$dir/gnuplot" unless -e "$dir/gnuplot";
96         my $png = "$dir/gnuplot/$name"; # FIXME
97
98         open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot: $!";
99         open($gnuplot, '>', '/tmp/gnuplot') if $self->param('debug');
100
101         my @c = @{ $ret->{columns} };
102         warn "first column not timestamp" unless shift @c eq 'timestamp';
103
104         print $gnuplot qq|
105 set terminal png
106 set output '$png'
107
108 set xdata time
109 set timefmt "%Y-%m-%d %H:%M:%S"
110 set format x "%H%M%S"
111
112 plot '-' using 1:3 title "$c[0]"
113 |;
114         foreach my $row ( @{ $ret->{rows} } ) {
115                 print $gnuplot join("\t", @$row),"\n";
116         }
117         print $gnuplot "e\n";
118         close($gnuplot);
119
120         $self->render('gnuplot', sql => $sql, img => "/gnuplot/$name");
121 };
122
123 app->start;
124 __DATA__
125
126 @@ index.html.ep
127 % layout 'default';
128 % title 'Gearman demo';
129
130 <ul>
131 <li><a href="/gearman.html#ping/127.0.0.1">Gearman</a> web interface
132 <li><%= link_to 'CRM' => 'CRM.html' %> search with tabular output
133 </ul>
134
135 Gnuplot graphs:
136
137 <ul>
138 <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>
139 </ul>
140
141 Low-level API tests:
142
143 <%= link_to 'HTTP ping' => 'ping_http' %>
144 <%= link_to 'Gearman ping' => 'ping_g' %>
145
146 @@ ping.html.ep
147 % layout 'default';
148 pong: <tt><%= $pong %>
149
150 @@ gnuplot.html.ep
151 % layout 'default';
152 <%= form_for gnuplot => begin %>
153  <%= text_area 'sql', cols => 80 %>
154  <%= submit_button %>
155 <% end %>
156 <img src="<%= $img %>">
157
158 @@ layouts/default.html.ep
159 <!doctype html><html>
160   <head><title><%= title %></title></head>
161   <body><%= content %></body>
162 </html>