6 Copyright (C) 2006 Eric Bollengier
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 use POSIX qw/strftime/;
36 my $conf = new Bweb::Config(config_file => '/etc/bweb/config');
39 my $bweb = new Bweb(info => $conf);
41 my $dbh = $bweb->{dbh};
42 my $debug = $bweb->{debug};
44 my $graph = CGI::param('graph') || 'job_size';
45 my $legend = CGI::param('legend') || 'on' ;
46 $legend = ($legend eq 'on')?1:0;
48 my $arg = $bweb->get_form(qw/width height limit offset age
49 jfilesets level status jjobnames jclients/);
51 my ($limitq, $label) = $bweb->get_limit(age => $arg->{age},
52 limit => $arg->{limit},
53 offset=> $arg->{offset},
54 order => 'Job.StartTime ASC',
58 if ($arg->{status} and $arg->{status} ne 'Any') {
59 $statusq = " AND Job.JobStatus = '$arg->{status}' ";
63 if ($arg->{level} and $arg->{level} ne 'Any') {
64 $levelq = " AND Job.Level = '$arg->{level}' ";
68 if ($arg->{jfilesets}) {
69 $filesetq = " AND FileSet.FileSet IN ($arg->{qfilesets}) ";
73 if ($arg->{jjobnames}) {
74 $jobnameq = " AND Job.Name IN ($arg->{jjobnames}) ";
76 $arg->{jjobnames} = 'all'; # skip warning
80 if ($arg->{jclients}) {
81 $clientq = " AND Client.Name IN ($arg->{jclients}) ";
83 $arg->{jclients} = 'all'; # skip warning
86 my $gtype = CGI::param('gtype') || 'bars';
88 print CGI::header('image/png');
94 if ($gtype eq 'lines') {
96 $graph = GD::Graph::lines->new ( $arg->{width}, $arg->{height} );
98 } elsif ($gtype eq 'bars') {
100 $graph = GD::Graph::bars->new ( $arg->{width}, $arg->{height} );
102 } elsif ($gtype eq 'linespoints') {
103 use GD::Graph::linespoints;
104 $graph = GD::Graph::linespoints->new ( $arg->{width}, $arg->{height} );
106 } elsif ($gtype eq 'bars3d') {
107 use GD::Graph::bars3d;
108 $graph = GD::Graph::bars3d->new ( $arg->{width}, $arg->{height} );
114 $graph->set('x_label' => 'Time',
115 'x_number_format' => sub { strftime('%D', localtime($_[0])) },
116 'x_tick_number' => 1,
132 foreach my $row (@$all_row) {
133 my $label = $row->[1] . "/" . $row->[2] ; # client/backup name
135 $ret->{date}->[$i] = $row->[0];
136 $ret->{$label}->[$i] = $row->[3];
138 $last_date = $row->[0];
141 # insert a fake element
142 foreach my $elt ( keys %{$ret}) {
143 $ret->{$elt}->[$i] = undef;
146 $ret->{date}->[$i] = $last_date + 1;
148 my $date = $ret->{date} ;
151 return ($date, $ret);
163 foreach my $row (@$all_row) {
164 $ret->{date}->[$i] = $row->[0];
165 $ret->{nb}->[$i] = $row->[1];
172 if ($graph eq 'job_size') {
176 UNIX_TIMESTAMP(Job.StartTime) AS starttime,
177 Client.Name AS clientname,
179 Job.JobBytes AS jobbytes
180 FROM Job, Client, FileSet
181 WHERE Job.ClientId = Client.ClientId
182 AND Job.FileSetId = FileSet.FileSetId
192 print STDERR $query if ($debug);
194 my $obj = get_graph('title' => "Job Size : $arg->{jclients}/$arg->{jjobnames}",
197 'y_number_format' => \&Bweb::human_size,
200 my $all = $dbh->selectall_arrayref($query) ;
202 my ($d, $ret) = make_tab($all);
204 $obj->set_legend(keys %$ret);
206 print $obj->plot([$d, values %$ret])->png;
209 if ($graph eq 'job_file') {
213 UNIX_TIMESTAMP(Job.StartTime) AS starttime,
214 Client.Name AS clientname,
216 Job.JobFiles AS jobfiles
217 FROM Job, Client, FileSet
218 WHERE Job.ClientId = Client.ClientId
219 AND Job.FileSetId = FileSet.FileSetId
229 print STDERR $query if ($debug);
231 my $obj = get_graph('title' => "Job Files : $arg->{jclients}/$arg->{jjobnames}",
232 'y_label' => 'Number Files',
236 my $all = $dbh->selectall_arrayref($query) ;
238 my ($d, $ret) = make_tab($all);
240 $obj->set_legend(keys %$ret);
242 print $obj->plot([$d, values %$ret])->png;
245 elsif ($graph eq 'job_rate') {
249 UNIX_TIMESTAMP(Job.StartTime) AS starttime,
250 Client.Name AS clientname,
253 ($bweb->{sql}->{SEC_TO_INT}(
254 $bweb->{sql}->{UNIX_TIMESTAMP}(EndTime)
255 - $bweb->{sql}->{UNIX_TIMESTAMP}(StartTime)) + 0.01)
258 FROM Job, Client, FileSet
259 WHERE Job.ClientId = Client.ClientId
260 AND Job.FileSetId = FileSet.FileSetId
270 print STDERR $query if ($debug);
272 my $obj = get_graph('title' => "Job Rate : $arg->{jclients}/$arg->{jjobnames}",
273 'y_label' => 'Rate b/s',
275 'y_number_format' => \&Bweb::human_size,
278 my $all = $dbh->selectall_arrayref($query) ;
280 my ($d, $ret) = make_tab($all);
282 $obj->set_legend(keys %$ret);
284 print $obj->plot([$d, values %$ret])->png;
289 elsif ($graph eq 'job_duration') {
293 UNIX_TIMESTAMP(Job.StartTime) AS starttime,
294 Client.Name AS clientname,
296 $bweb->{sql}->{SEC_TO_INT}( $bweb->{sql}->{UNIX_TIMESTAMP}(EndTime)
297 - $bweb->{sql}->{UNIX_TIMESTAMP}(StartTime))
299 FROM Job, Client, FileSet
300 WHERE Job.ClientId = Client.ClientId
301 AND Job.FileSetId = FileSet.FileSetId
311 print STDERR $query if ($debug);
313 my $obj = get_graph('title' => "Job Duration : $arg->{jclients}/$arg->{jjobnames}",
314 'y_label' => 'Duration',
316 'y_number_format' => \&Bweb::human_sec,
318 my $all = $dbh->selectall_arrayref($query) ;
320 my ($d, $ret) = make_tab($all);
322 $obj->set_legend(keys %$ret);
324 print $obj->plot([$d, values %$ret])->png;
327 # number of job per day/hour
328 } elsif ($graph =~ /^job_(count|sum|avg)_((p?)(day|hour|month))$/) {
332 my ($limit, $label) = $bweb->get_limit(age => $arg->{age},
333 limit => $arg->{limit},
334 offset=> $arg->{offset},
337 my @arg; # arg for plotting
339 if (!$per_t) { # much better aspect
342 push @arg, ("x_number_format" => undef,
347 if ($t eq 'sum' or $t eq 'avg') {
348 push @arg, ('y_number_format' => \&Bweb::human_size);
353 " . ($per_t?"":"UNIX_TIMESTAMP") . "(" . $bweb->{sql}->{"STARTTIME_$d"} . ") AS A,
355 FROM Job, Client, FileSet
356 WHERE Job.ClientId = Client.ClientId
357 AND Job.FileSetId = FileSet.FileSetId
367 print STDERR $query if ($debug);
369 my $obj = get_graph('title' => "Job $t : $arg->{jclients}/$arg->{jjobnames}",
375 my $all = $dbh->selectall_arrayref($query) ;
376 my ($ret) = make_tab_sum($all);
378 print $obj->plot([$ret->{date}, $ret->{nb}])->png;