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