]> git.sur5r.net Git - bacula/bacula/blob - gui/bweb/cgi/bgraph.pl
ebl hide legend with option
[bacula/bacula] / gui / bweb / cgi / bgraph.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 =head1 LICENSE
5
6     Copyright (C) 2006 Eric Bollengier
7         All rights reserved.
8
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
12     any later version.
13
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.
18
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
22
23 =head1 VERSION
24
25     $Id$
26
27 =cut
28
29 use Bweb;
30
31 use Data::Dumper;
32 use CGI;
33
34 use POSIX qw/strftime/;
35
36 my $conf = new Bweb::Config(config_file => '/etc/bweb/config');
37 $conf->load();
38
39 my $bweb = new Bweb(info => $conf);
40 $bweb->connect_db();
41 my $dbh = $bweb->{dbh};
42 my $debug = $bweb->{debug};
43
44 my $graph = CGI::param('graph') || 'begin';
45 my $legend = CGI::param('legend') || 'on' ;
46 $legend = ($legend eq 'on')?1:0;
47
48 my $arg = $bweb->get_form(qw/width height limit offset age
49                              jfilesets level status jjobnames jclients/);
50
51 my ($limitq, $label) = $bweb->get_limit(age   => $arg->{age},
52                                         limit => $arg->{limit},
53                                         offset=> $arg->{offset},
54                                         order => 'Job.StartTime ASC',
55                                         );
56
57 my $statusq='';
58 if ($arg->{status} and $arg->{status} ne 'Any') {
59     $statusq = " AND Job.JobStatus = '$arg->{status}' ";
60 }
61     
62 my $levelq='';
63 if ($arg->{level} and $arg->{level} ne 'Any') {
64     $levelq = " AND Job.Level = '$arg->{level}' ";
65
66
67 my $filesetq='';
68 if ($arg->{jfilesets}) {
69     $filesetq = " AND FileSet.FileSet IN ($arg->{qfilesets}) ";
70
71
72 my $jobnameq='';
73 if ($arg->{jjobnames}) {
74     $jobnameq = " AND Job.Name IN ($arg->{jjobnames}) ";
75 } else {
76     $arg->{jjobnames} = 'all';  # skip warning
77
78
79 my $clientq='';
80 if ($arg->{jclients}) {
81     $clientq = " AND Client.Name IN ($arg->{jclients}) ";
82 } else {
83     $arg->{jclients} = 'all';   # skip warning
84 }
85
86 my $gtype = CGI::param('gtype') || 'bars';
87
88 print CGI::header('image/png');
89
90 sub get_graph
91 {
92     my (@options) = @_;
93     my $graph;
94     if ($gtype eq 'lines') {
95         use GD::Graph::lines;
96         $graph = GD::Graph::lines->new ( $arg->{width}, $arg->{height} );
97
98     } elsif ($gtype eq 'bars') {
99         use GD::Graph::bars;
100         $graph = GD::Graph::bars->new ( $arg->{width}, $arg->{height} );
101
102     } elsif ($gtype eq 'linespoints') {
103         use GD::Graph::linespoints;
104         $graph = GD::Graph::linespoints->new ( $arg->{width}, $arg->{height} );
105
106     } elsif ($gtype eq 'bars3d') {
107         use GD::Graph::bars3d;
108         $graph = GD::Graph::bars3d->new ( $arg->{width}, $arg->{height} );
109
110     } else {
111         return undef;
112     }
113
114     $graph->set('x_label' => 'Time',
115                 'x_number_format' => sub { strftime('%D', localtime($_[0])) },
116                 'x_tick_number' => 1,
117                 @options,
118                 );
119
120     return $graph;
121 }
122
123 sub make_tab
124 {
125     my ($all_row) = @_;
126
127     my $i=0;
128     my $last_date=0;
129
130     my $ret = {};
131     
132     foreach my $row (@$all_row) {
133         my $label = $row->[1] . "/" . $row->[2] ; # client/backup name
134
135         $ret->{date}->[$i]   = $row->[0];       
136         $ret->{$label}->[$i] = $row->[3];
137         $i++;
138         $last_date = $row->[0];
139     }
140
141     # insert a fake element
142     foreach my $elt ( keys %{$ret}) {
143         $ret->{$elt}->[$i] =  undef;
144     }
145
146     $ret->{date}->[$i] = $last_date + 1;
147
148     my $date = $ret->{date} ;
149     delete $ret->{date};
150
151     return ($date, $ret);
152 }
153
154 if ($graph eq 'job_size') {
155
156     my $query = "
157 SELECT 
158        UNIX_TIMESTAMP(Job.StartTime)    AS starttime,
159        Client.Name                      AS clientname,
160        Job.Name                         AS jobname,
161        Job.JobBytes                     AS jobbytes
162 FROM Job, Client, FileSet
163 WHERE Job.ClientId = Client.ClientId
164   AND Job.FileSetId = FileSet.FileSetId
165   AND Job.Type = 'B'
166   $clientq
167   $statusq
168   $filesetq
169   $levelq
170   $jobnameq
171 $limitq
172 ";
173
174     print STDERR $query if ($debug);
175
176     my $obj = get_graph('title' => "Job Size : $arg->{jclients}/$arg->{jjobnames}",
177                         'y_label' => 'Size',
178                         'y_min_value' => 0,
179                         'y_number_format' => \&Bweb::human_size,
180                         );
181
182     my $all = $dbh->selectall_arrayref($query) ;
183
184     my ($d, $ret) = make_tab($all);
185     if ($legend) {
186         $obj->set_legend(keys %$ret);
187     }
188     print $obj->plot([$d, values %$ret])->png;
189 }
190
191 if ($graph eq 'job_file') {
192
193     my $query = "
194 SELECT 
195        UNIX_TIMESTAMP(Job.StartTime)    AS starttime,
196        Client.Name                      AS clientname,
197        Job.Name                         AS jobname,
198        Job.JobFiles                     AS jobfiles
199 FROM Job, Client, FileSet
200 WHERE Job.ClientId = Client.ClientId
201   AND Job.FileSetId = FileSet.FileSetId
202   AND Job.Type = 'B'
203   $clientq
204   $statusq
205   $filesetq
206   $levelq
207   $jobnameq
208 $limitq
209 ";
210
211     print STDERR $query if ($debug);
212
213     my $obj = get_graph('title' => "Job Files : $arg->{jclients}/$arg->{jjobnames}",
214                         'y_label' => 'Number Files',
215                         'y_min_value' => 0,
216                         );
217
218     my $all = $dbh->selectall_arrayref($query) ;
219
220     my ($d, $ret) = make_tab($all);
221     if ($legend) {
222         $obj->set_legend(keys %$ret);
223     }
224     print $obj->plot([$d, values %$ret])->png;
225 }
226
227 elsif ($graph eq 'job_rate') {
228
229     my $query = "
230 SELECT 
231        UNIX_TIMESTAMP(Job.StartTime)                          AS starttime,
232        Client.Name                      AS clientname,
233        Job.Name                         AS jobname,
234        Job.JobBytes /
235        ($bweb->{sql}->{SEC_TO_INT}(
236                           $bweb->{sql}->{UNIX_TIMESTAMP}(EndTime)  
237                         - $bweb->{sql}->{UNIX_TIMESTAMP}(StartTime)) + 0.01) 
238          AS rate
239
240 FROM Job, Client, FileSet
241 WHERE Job.ClientId = Client.ClientId
242   AND Job.FileSetId = FileSet.FileSetId
243   AND Job.Type = 'B'
244   $clientq
245   $statusq
246   $filesetq
247   $levelq
248   $jobnameq
249 $limitq
250 ";
251
252     print STDERR $query if ($debug);
253
254     my $obj = get_graph('title' => "Job Rate : $arg->{jclients}/$arg->{jjobnames}",
255                         'y_label' => 'Rate b/s',
256                         'y_min_value' => 0,
257                         'y_number_format' => \&Bweb::human_size,
258                         );
259
260     my $all = $dbh->selectall_arrayref($query) ;
261
262     my ($d, $ret) = make_tab($all);    
263     if ($legend) {
264         $obj->set_legend(keys %$ret);
265     }
266     print $obj->plot([$d, values %$ret])->png;
267 }
268
269
270
271 elsif ($graph eq 'job_duration') {
272
273     my $query = "
274 SELECT 
275        UNIX_TIMESTAMP(Job.StartTime)                           AS starttime,
276        Client.Name                                             AS clientname,
277        Job.Name                                                AS jobname,
278   $bweb->{sql}->{SEC_TO_INT}(  $bweb->{sql}->{UNIX_TIMESTAMP}(EndTime)  
279                              - $bweb->{sql}->{UNIX_TIMESTAMP}(StartTime)) 
280          AS duration
281 FROM Job, Client, FileSet
282 WHERE Job.ClientId = Client.ClientId
283   AND Job.FileSetId = FileSet.FileSetId
284   AND Job.Type = 'B'
285   $clientq
286   $statusq
287   $filesetq
288   $levelq
289   $jobnameq
290 $limitq
291 ";
292
293     print STDERR $query if ($debug);
294
295     my $obj = get_graph('title' => "Job Duration : $arg->{jclients}/$arg->{jjobnames}",
296                         'y_label' => 'Duration',
297                         'y_min_value' => 0,
298                         'y_number_format' => \&Bweb::human_sec,
299                         );
300     my $all = $dbh->selectall_arrayref($query) ;
301
302     my ($d, $ret) = make_tab($all);
303     if ($legend) {
304         $obj->set_legend(keys %$ret);
305     }
306     print $obj->plot([$d, values %$ret])->png;
307 }
308