draw graphs using gnuplot
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 19 Mar 2011 13:19:47 +0000 (13:19 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 19 Mar 2011 13:31:40 +0000 (13:31 +0000)
bin/debian-install.sh
web_ui.pl

index dd155ed..78ea2d7 100755 (executable)
@@ -5,5 +5,6 @@ sudo apt-get install \
        libgearman-driver-perl libmojolicious-perl \
        libdata-dump-perl \
        libregexp-common-perl \
-       postgresql libdbd-pg-perl
+       postgresql libdbd-pg-perl \
+       gnuplot-nox
 
index 5f48fbd..84a983f 100755 (executable)
--- a/web_ui.pl
+++ b/web_ui.pl
@@ -71,6 +71,52 @@ get '/_g/status' => sub {
        _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
 };
 
+get '/gnuplot' => sub {
+       my $self = shift;
+
+       my $sql = $self->param('sql') || die "sql required";
+
+       my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
+       die "no result for $sql" unless $ret;
+
+       $ret = Mojo::JSON->new->decode( $ret );
+
+       my $dir = $self->app->home->rel_dir('public');
+
+       my $name = $sql;
+       $name =~ s/\W+//gs;
+       $name .= '.png';
+
+warn "# $sql -> $name";
+
+       mkdir "$dir/gnuplot" unless -e "$dir/gnuplot";
+       my $png = "$dir/gnuplot/$name"; # FIXME
+
+       open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot: $!";
+       open($gnuplot, '>', '/tmp/gnuplot') if $self->param('debug');
+
+       my @c = @{ $ret->{columns} };
+       warn "first column not timestamp" unless shift @c eq 'timestamp';
+
+       print $gnuplot qq|
+set terminal png
+set output '$png'
+
+set xdata time
+set timefmt "%Y-%m-%d %H:%M:%S"
+set format x "%H%M%S"
+
+plot '-' using 1:3 title "$c[0]"
+|;
+       foreach my $row ( @{ $ret->{rows} } ) {
+               print $gnuplot join("\t", @$row),"\n";
+       }
+       print $gnuplot "e\n";
+       close($gnuplot);
+
+       $self->redirect_to("/gnuplot/$name");
+};
+
 app->start;
 __DATA__
 
@@ -83,6 +129,12 @@ __DATA__
 <li><%= link_to 'CRM' => 'CRM.html' %> search with tabular output
 </ul>
 
+Gnuplot graphs:
+
+<ul>
+<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>
+</ul>
+
 Low-level API tests:
 
 <%= link_to 'HTTP ping' => 'ping_http' %>