]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/bgraph.inc.php
bacula-web: Removed paths.php from git
[bacula/bacula] / gui / bacula-web / bgraph.inc.php
1 <?php
2
3 class BGraph{
4         private $title;
5         
6         private $data;
7         private $data_type;
8         private $type;
9         
10         private $colors;
11         private $shading;
12         
13         private $width;
14         private $height;
15         private $output_file;
16         private $plot;
17         
18         function __construct( $filename = "graph.png" )
19         {
20                 $this->output_file = 'templates_c/' . $filename;
21         }
22         
23         public function SetData( $data_in, $type, $data_type, $shading = 5 )
24         {
25                 $this->data             = $data_in;
26                 $this->type             = $type;
27                 $this->data_type        = $data_type;
28                 $this->shadding         = $shading;
29         }
30         
31         public function SetGraphSize( $width, $height )
32         {
33                 $this->width  = $width;
34                 $this->height = $height;
35         }
36         
37         public function SetTitle( $title )
38         {
39                 if( !empty($title) )
40                         $this->title = $title;
41                 else
42                         die( "Please provide a non empty title for the graph" );
43         }
44         
45         public function SetColors( $colors )
46         {
47                 if( is_array( $colors ) )
48                         $this->colors = $colors;
49                 else
50                         die( "Please provide a array in BGraph->SetColors()" );
51         }
52         
53         public function Get_Image_file()
54         {
55                 return $this->output_file;
56         }
57         
58         public function Render()
59         {
60                 // Setting the size
61                 $this->plot = new PHPlot( $this->width, $this->height );
62                 
63                 // Render to file instead of screen
64                 $this->plot->SetOutputFile( $this->output_file );
65                 $this->plot->SetFileFormat("png");
66                 $this->plot->SetIsInline( true );
67                 
68                 
69                 $this->plot->SetImageBorderType('plain');
70
71                 // Data, type and data type
72                 $this->plot->SetPlotType( $this->type );
73                 $this->plot->SetDataType( $this->data_type );
74                 $this->plot->SetDataValues( $this->data );
75                 
76                 // Plot colors
77                 $this->plot->SetDataColors( $this->colors );
78                 
79                 // Plot shading
80                 $this->plot->SetShading( $this->shading );
81                 
82                 // Image border
83                 $this->plot->SetImageBorderType( 'none' );
84
85                 // Plot area (calculated regarding the width and height of the graph)
86                 if( $this->type == 'pie' )
87                         $this->plot->SetPlotAreaPixels( 10, 10, ($this->width / 2), $this->height-10 );
88                 
89                 // Legend position (calculated regarding the width and height of the graph)
90                 $this->plot->SetLegendPixels( ($this->width / 2) + 10, 25 );
91
92                 // Labels scale position
93                 if( $this->type == 'pie' )
94                         $this->plot->SetLabelScalePosition( 0.2 );
95                 
96                 // Graph title
97                 $this->plot->SetTitle( $this->title );
98
99                 // Setting up legends
100                 if( $this->type != 'bars' ) {
101                         $legends = array();
102                         foreach( $this->data as $key => $legend ) {
103                                 $this->plot->SetLegend( implode(': ',$legend) );
104                         }
105                 }
106
107                 # Turn off X tick labels and ticks because they don't apply here:
108                 $this->plot->SetXTickLabelPos('none');
109                 $this->plot->SetXTickPos('none');
110                 $this->plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);
111
112
113                 $this->plot->DrawGraph();
114         } // end function Render()
115 } // end BGraph classe
116
117 /*
118 class BGraph {
119
120         var $type;
121         var $sizex;
122         var $sizey;
123         var $MarginBottom;
124         var $MarginLeft;
125         var $Leg;
126
127
128         
129         function BShowGraph($datos,$title,$xlabel,$ylabel,$leyenda,$tipo="lines") {
130         
131                 global $type;
132         
133                 require_once ("external_packages/phplot/phplot.php");
134
135                 if ( empty($this->sizex) || empty($this->sizey) ) {    //Default size
136                         $this->sizex = "600";
137                         $this->sizey = "400";
138                 }
139                 if ( empty($this->MarginBottom) ) {
140                         $this->MarginBottom = 120;
141                 }
142                 
143                 $legend = $leyenda;
144 //              $bgcolor = array(222,206,215);      // Background color of graph
145                 $bgcolor = array(207,231,231);
146                 $fgcolor = array(110,41,57);
147                 
148
149                 
150                 $graph = new PHPlot($this->sizex,$this->sizey,"","");
151
152                 if ( !empty($type) )
153                         $graph->setDataType($type);
154
155                 $graph->SetDataValues($datos);
156                 $graph->SetPlotType($tipo);
157 //              $graph->SetUseTTF(1);
158                 $graph->SetBackgroundColor($bgcolor);
159
160                 $graph->SetLegendPixels(1,20);
161                 $graph->SetDataColors(array('SkyBlue','purple','PeachPuff','aquamarine1','#2CB04B','beige','#9F865F','#135568','orchid','navy','red', 'black', 'blue', 'green', 'brown', 'yellow','cyan','orange','#B9F5A7','#AFAFAF'));
162                 $graph->SetTitle($title);
163                 $graph->SetXLabel($xlabel);
164                 $graph->SetYLabel($ylabel);
165                 $graph->SetPlotAreaWorld("","","","");
166                 
167                 if ( count($datos) > 5 )
168                         $graph->SetXDataLabelAngle(90);
169                 else
170                         $graph->SetXDataLabelAngle(0);
171                 $graph->SetNumXTicks(1);
172 //              $graph->SetXDataLabelPos('none');
173 //              $graph->SetXTickLabelPos('plotdown');
174                 
175 //              $graph->SetXGridLabelType("time");
176 //              $graph->SetXTimeFormat("%b ") ;
177
178                 if ( $this->Leg == 1 ) {
179                         $this->MarginLeftWithLegend($legend);
180                         $graph->SetMarginsPixels($this->MarginLeft,10,35,$this->MarginBottom);
181                         $graph->SetLegend($legend);
182                                 }
183                 else
184                         $graph->SetMarginsPixels(90,35,35,$this->MarginBottom);
185 //              $graph->SetDataColors(array($fgcolor),array( "black"));
186                 $graph->SetFileFormat( "png");
187 //              $graph->DoScaleData(1,1);
188 //              $graph->DoMovingAverage(1,1,1);
189
190 //              FIX ME -- to round y axis.
191                 $vtick = strlen (round ($graph->max_y));
192                 $res = 1;
193                 for ($i=1;$i < $vtick; $i++)
194                         $res = $res*10;
195                 if (strlen($graph->max_y-$res) != $vtick )
196                         $res = $res/10;
197                 $graph->SetVertTickIncrement($res);
198                 $graph->DrawGraph();
199
200         }//end Crear
201
202
203 //Estupidez que tengo que cambiar. !!!!!!!!!!!
204         function SetDataType($typ) {
205                 
206                 global $type;
207                 $type = $typ;
208         }
209
210         function MarginLeftWithLegend($clients) {
211                 
212                 $maxlen = 0;
213                 
214                 while (next($clients)) {
215                         $tmp = strlen(current($clients));
216                         if ( $tmp > $maxlen )
217                                 $maxlen = $tmp;
218                 }
219                 $this->MarginLeft = $maxlen * 9;
220         }       
221
222 }//end class
223
224
225
226
227
228 class BCreateGraph extends BGraph {
229
230         var $BD_bacula;
231         var $izquierda;
232         var $derecha;
233         var $StartDate;
234         var $EndDate;
235         var $elapsed;                        // Default elapsed time to show complex graphs
236         
237         
238         
239         function BCreateGraph() {
240         
241                 $this->StartDate = "1900-01-01";
242                 $this->EndDate = "4000-01-01";
243                 $this->elapsed = LAST_DAY;                   // 24 hours in seconds.
244                 
245          }              
246          
247          
248          
249         function BCreate($server,$tipo_dato,$title,$tipo="bars",$xlabel="",$ylabel="") {
250         
251                 global $DB_bacula;
252                 global $izquierda;
253                 global $derecha;
254                 global $clientes;
255         
256                 $this->clientes=array();
257                 $DB_bacula = new Bweb();
258                 $datos = $this->SQLPrepareData($server,$tipo_dato);
259         
260                 if ( empty($datos) ) {                       //No data = No stats = Empty graph
261                         header("Content-type: image/png");
262                         $img= @ImageCreate(200,100) or die ("Cannot intialize GD stream");
263                         $bgc= ImageColorAllocate($img, 0, 255,255);
264                         $txc= ImageColorAllocate($img, 0,0,0);
265                         ImageString($img, 5, 4, 4, "No data to process", $txc);
266                         ImagePng($img);
267                         ImageDestroy($img);
268                         return; 
269                 }
270         
271                 if ( empty ($xlabel) ) {                       // If no label, table names like leyends
272                         $xlabel=$derecha; $ylabel=$izquierda; 
273                 } 
274                         
275                 $this->SetDataType("text-data");
276                 $this->BShowGraph($datos,$title,$xlabel,$ylabel,$this->clientes,$tipo);
277                 
278         }
279
280
281  
282         function SQLPrepareData($servidor,$tipo_dato=0) {         // Prepare bytes data from database.
283
284                 global $DB_bacula;
285                 global $izquierda;
286                 global $derecha;
287         
288                 if ( $tipo_dato<30 ) {               // Simple graph. Only 2 data 
289         
290                 switch ($tipo_dato)
291                                 {
292                                 case BACULA_TYPE_BYTES_FILES:
293                                         $izquierda="jobbytes";
294                                         $derecha="jobfiles";
295                                         break;
296                                 case BACULA_TYPE_FILES_JOBID:
297                                         $izquierda="jobfiles";
298                                         $derecha="jobid";
299                                         break;
300                                 default:
301                                         $izquierda="jobbytes";
302                                         $derecha="endtime";
303                                         break;
304                                 }
305                         $result = $DB_bacula->db_link->query("select $derecha,$izquierda from Job where Name='$servidor' and EndTime < '$this->EndDate' and EndTime > '$this->StartDate' order by SchedTime asc")
306                                 or die ("classes.inc: Error at query: 5");
307                 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
308                         $whole_result[] = $this->array_merge_php4($row["$derecha"],$row[$izquierda]);
309                 }
310                 $result->free();
311         } else {                                                // Complex graph. 3 or more data.
312                 
313                         switch ( $tipo_dato )
314                                 {
315                                 case '30':                      // Unused, at this time.
316                                         $result = $DB_bacula->db_link->query("select JobBytes,JobFiles,Jobid from Job where Name='$servidor' order by EndTime asc")
317                                                 or die ("classes.inc: Error at query: 6");
318                                         while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) )
319                                                 $whole_result[] = array_merge($row["Jobid"],$row["JobFiles"],$row["JobBytes"]);
320                                         $result->free();
321                                         break;
322                                 case BACULA_TYPE_BYTES_ENDTIME_ALLJOBS:  // Special: Generic graph from all clientes.
323                                         $i = -1;                         // Counter of number of jobs of one client. SP: Contador del nmero de jobs totales de un cliente.
324                                         $i2 = 0;                         // Counter of number of keys of array. SP: Contador del nmero de valores del array.
325                                         
326                                         if ($DB_bacula->driver == "mysql") {
327                                         $res = $DB_bacula->db_link->query("select Name from Job where UNIX_TIMESTAMP(EndTime) > UNIX_TIMESTAMP(NOW())-$this->elapsed  group by Name order by Name desc")
328                                                 or die ("classes.inc: Error at query: 7");
329                                                 $resdata = $DB_bacula->db_link->query("select date_format(EndTime,\"%Y-%m-%d\") from Job where UNIX_TIMESTAMP(EndTime) > UNIX_TIMESTAMP(NOW())-$this->elapsed  group by date_format(EndTime, \"%Y-%m-%d\") order by EndTime")
330                                                         or die ("classes.inc: Error at query: 8");
331                                         }
332                                         else if ($DB_bacula->driver == "pgsql") {
333                                                 $res = $DB_bacula->db_link->query("select Name from Job where EndTime > now() - 1*interval'$this->elapsed s'  group by Name order by Name desc")
334                                                         or die ("classes.inc: Error at query: 8");
335                                                 $resdata = $DB_bacula->db_link->query("select to_char(EndTime,'YY-MM-DD') from Job where EndTime > NOW() - 1*interval'$this->elapsed s'  group by EndTime order by EndTime")
336                                                         or die ("classes.inc: Error at query: 9");
337                                         }
338                                         
339                                         if (PEAR::isError($resdata))
340                                                 die("classes.inc: Error at query: 9.1<br>".$resdata->getMessage());
341                                         while ( $tmpdata = $res->fetchRow() )
342                                                 array_push($this->clientes,$tmpdata[0]);
343                                                 
344 //                                      echo "<pre>";
345 //                                      print_r ($this->clientes);
346 //                                      echo "</pre>";
347                                         
348                                         
349                                         $spr                    = array();                        // Temporal array
350                                         $spr2                   = array();                       // Temporal array
351                                         $whole_result   = array();
352                                         $count                  = 0;
353                                                                                 
354                                         while ( $tmpdata = $resdata->fetchRow() ) {
355                                                 $count++;
356                                                 array_push($spr,$tmpdata[0]);
357                                                 if ($DB_bacula->driver == "mysql")
358                                                         $result = $DB_bacula->db_link->query("select date_format(EndTime,\"%Y-%m-%d\"),SUM(JobBytes) as sum,Name as name,count(Name) as Nname from Job WHERE EndTime like '$tmpdata[0]%' group by Name order by Name desc")
359                                                                 or die ("classes.inc: Error at query: 10");
360                                                 else if ($DB_bacula->driver == "pgsql") {
361                                                         $query = "select to_char(EndTime,'YY-MM-DD'),SUM(JobBytes) as sum,Name,count(Name) as Nname from Job WHERE EndTime like '%$tmpdata[0]%' group by EndTime,Name order by Name desc";
362                                                         $result = $DB_bacula->db_link->query($query)
363                                                                 or die ("classes.inc: Error at query: 11");
364                                                 }
365                                                 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
366                                                         $spr2 = array_merge($spr2,array($row["name"]=>$row["sum"]));
367                                                         $i = $result->numRows();
368                                                 }
369
370                                         
371 //                                              echo "<pre>";
372 //                                              print_r ($spr2);
373 //                                              echo "</pre>";
374                                                 
375                                                 reset ($this->clientes);        
376                                                 do {
377                                                         if ( $spr2[current($this->clientes)] != NULL)
378                                                                 array_push($spr,$spr2[current($this->clientes)]);
379                                                         else
380                                                                 array_push($spr,0);
381                                                 } while ( next($this->clientes) );
382                                                 
383                                                 if ( $i2 < $i )
384                                                         $i2 = $i;
385                                                 
386                                                 if ( $tmpdata[0] != $row["EndTime"] )   
387                                                         array_push($whole_result,$spr);
388                                                 
389                                                 $spr = array();
390                                                 $spr2 = array();
391                                         }
392
393                                         for ( $i = 0; $i < count($whole_result); $i++ ) {  // To equal the arrays so that the graph is not unsquared. SP:Igualamos las matrices para que la gr?ica no se descuadre
394                                                 $tmp = count($whole_result[$i]);
395                                                 if ( $i2 < $tmp )                // Estupidez?. Check this code later...
396                                                         continue;
397                                                 $tmp = $i2 - $tmp;
398                                                 for ( $a = 0; $a <= $tmp; $a++ )
399                                                         array_push($whole_result[$i],"0");                                      // Fill the array
400                                         }
401                                         $resdata->free();       
402 //                                      echo "DEBUG:<br>";
403 //                                      echo "<pre>";
404 //                                      print_r ($whole_result);
405 //                                      echo "</pre>";  
406                                         break;
407                                 
408                                 default:
409                                         break;
410                         }
411                 }
412 //      $result->free();
413           return $whole_result;
414         }//end function
415
416
417
418         //Convert date from mysql to smarty.           THE SAME FUNCTION AT 2 CLASSES. THIS WAY IS BUGGY. TO SOLVE LATER.
419         function PrepareDate($StartDateMonth,$StartDateDay,$StartDateYear,$EndDateMonth,$EndDateDay,$EndDateYear){
420         
421                 $this->StartDate = $StartDateYear."-".$StartDateMonth."-".$StartDateDay." 00:00:00";
422                 $this->EndDate = $EndDateYear."-".$EndDateMonth."-".$EndDateDay." 23:59:00";
423                 
424         }//end function
425
426
427         function array_merge_php4($array1,$array2) {
428             $return=array();
429
430             foreach(func_get_args() as $arg) {
431                 if(!is_array($arg)){
432                 $arg=array($arg);
433                 }
434                     foreach($arg as $key=>$val){
435                             if(!is_int($key)){
436                                 $return[$key]=$val;
437                             }else{
438                                 $return[]=$val;
439                             }
440                     }
441             }
442         return $return;
443         }
444
445 }//end class
446 */
447 ?>