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