]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/bweb.inc.php
bacula-web: Added Job Level stats on main dashboard
[bacula/bacula] / gui / bacula-web / bweb.inc.php
1 <?php
2 /* 
3 +-------------------------------------------------------------------------+
4 | Copyright (C) 2004-2005 Juan Luis Frances Jiminez                       |
5 |                                                                         |
6 | This program is free software; you can redistribute it and/or           |
7 | modify it under the terms of the GNU General Public License             |
8 | as published by the Free Software Foundation; either version 2          |
9 | of the License, or (at your option) any later version.                  |
10 |                                                                         |
11 | This program is distributed in the hope that it will be useful,         |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
14 | GNU General Public License for more details.                            |
15 +-------------------------------------------------------------------------+ 
16 */
17 require_once "paths.php";
18 require_once "DB.php";                                                                                                                  // Pear DB
19 require_once "config.inc.php";
20 require_once "bgraph.inc.php";
21 require_once($smarty_path."Config_File.class.php");
22
23 if (!function_exists('array_fill')) {                                                                                   // For PHP < 4.2.0 users 
24     require_once('array_fill.func.php');
25 }
26
27 class Bweb extends DB {
28
29     var $StartDate;
30     var $EndDate;
31     var $driver;
32         var $dbs;
33         var $dbs_name;
34         
35         public $db_link;                                                // Database link
36         private $db_dsn;                                                // Data Source Name
37         
38         private $config_file;                                   // Config filename
39         private $config;                                                // Loaded config from bacula.conf
40         private $catalogs = array();                    // Catalog array
41
42     function __construct()
43         {             
44                 $this->catalogs = array();
45                 
46                 // Loading configuration
47                 $this->config_file = getcwd() . '/configs/bacula.conf';
48                 if( !$this->load_config() )
49                         die( "Unable to load configuration");
50                         
51                 //echo "Number of catalog defined " . count($this->catalogs) . "<br />";
52                 
53                 /*
54                 $conf = new Config_File (CONFIG_DIR);
55                 $this->dbs = array();
56
57                 $i = 2;
58                 $sections = $conf->get(CONFIG_FILE,"DATABASE","host");
59                 array_push($this->dbs, "DATABASE");
60
61                 while ( !empty($sections) ) {                
62                         $sections = $conf->get(CONFIG_FILE,"DATABASE".$i,"host");
63                         if ( !empty($sections) )
64                                 array_push($this->dbs,"DATABASE".$i);
65                         $i++;
66                 }
67
68                 if ( $i < 4)
69                         $sec = "DATABASE";
70                 else {
71                         if ( !empty($_POST['sel_database']) ) {
72                                 $_SESSION['DATABASE'] = $_POST['sel_database'];
73                                 $sec = $_POST['sel_database'];
74                         } else {
75                                 if (isset($_SESSION['DATABASE']) )
76                                         $sec = $_SESSION['DATABASE'];
77                                 else
78                                         $sec = "DATABASE";
79                         }
80                 }
81
82         $this->dsn['hostspec'] = $conf->get(CONFIG_FILE,$sec,"host");
83         $this->dsn['username'] = $conf->get(CONFIG_FILE,$sec,"login");
84         $this->dsn['password'] = $conf->get(CONFIG_FILE,$sec,"pass");
85         $this->dsn['database'] = $conf->get(CONFIG_FILE,$sec,"db_name");
86         $this->dsn['phptype']  = $conf->get(CONFIG_FILE,$sec,"db_type");   // mysql, pgsql
87         
88                 if (  $conf->get(CONFIG_FILE,$sec,"db_port") )
89                         $this->dsn[port] = $conf->get(CONFIG_FILE,$sec,"db_port");
90                 */
91                 
92                 // Construct a valid dsn
93         $this->db_dsn['hostspec'] = $this->catalogs[0]["host"];
94         $this->db_dsn['username'] = $this->catalogs[0]["login"];
95                 $this->db_dsn['password'] = $this->catalogs[0]["pass"];
96                 $this->db_dsn['database'] = $this->catalogs[0]["db_name"];
97                 $this->db_dsn['phptype']  = $this->catalogs[0]["db_type"];
98                 
99                                         
100         $this->db_link = $this->connect($this->db_dsn);
101         
102                 if (DB::isError($this->db_link)) {
103                         die($this->db_link->getMessage());
104                 }else {
105                         $this->driver = $this->db_dsn['phptype'];                            
106             register_shutdown_function(array(&$this,'close'));
107                         $this->dbs_name = $this->db_dsn['database'];
108                 }
109         }
110                 
111     function load_config()
112         {
113                 $this->config = parse_ini_file( $this->config_file, true );
114                 
115                 if( !$this->config == false ) {
116                         // Loading database connection information
117                         foreach( $this->config as $parameter => $value )
118                         {
119                                 //echo "Param $parameter = $value <br />";
120                                 if( is_array($value) ){         // Parsing database section
121                                         array_push( $this->catalogs, $value );
122                                 }
123                         }
124                         return true;
125                 }else
126                         return false;
127         }
128         
129         public function get_config_param( $param )
130         {
131                 if( isset( $this->config[$param] ) )
132                         return $this->config[$param];
133                 else
134                         return false;
135         }
136         
137         public function Get_Nb_Catalogs() 
138         {
139                 return count( $this->catalogs );
140         }
141         
142         
143         function close() 
144         {
145                 $this->db_link->disconnect();
146     }      
147
148         
149          
150         function CalculateBytesPeriod($server,$StartDate,$EndPeriod) {   // Bytes transferred in a period.
151
152                 $result =& $this->db_link->query("select SUM(JobBytes) from Job WHERE EndTime < '$EndPeriod' and EndTime > '$StartDate' and Name='$server'")
153                         or die("classes.inc: Error query: 1");
154                 $return =& $result->fetchRow(); 
155                 return $return[0];
156         }//end function
157
158         
159          
160         function CalculateFilesPeriod($server,$StartDate,$EndPeriod) {    // Number of files transferred in a period.
161
162                 $result =& $this->db_link->query("select SUM(JobFiles) from Job WHERE EndTime < '$EndPeriod' and EndTime > '$StartDate' and Name='$server'")
163                         or die("classes.inc: Error query: 2");
164                 $return =& $result->fetchRow();
165                 return $return[0];
166         }//end function 
167
168                  
169
170         function PrepareDate($StartDateMonth,$StartDateDay,$StartDateYear,$EndDateMonth,$EndDateDay,$EndDateYear) {  // Convert date for Smarty. Check if only works with Mysql.
171         
172                 $this->StartDate=$StartDateYear."-".$StartDateMonth."-".$StartDateDay." 00:00:00";
173                 $this->EndDate=$EndDateYear."-".$EndDateMonth."-".$EndDateDay." 23:59:59";  // last day full
174                 
175         }//end function
176                 
177                 // Return humanized size with default unit of GB
178                 // if auto provide for unit argument, automaticaly decide which unit
179                 function human_file_size( $size, $decimal = 2, $unit = 'auto' )
180                 {
181                         $unit_id = 0;
182                         $lisible = false;
183                         $units = array('B','KB','MB','GB','TB');
184                         $hsize = $size;
185
186                         switch( $unit )
187                         {
188                                 case 'auto';
189                                         while( !$lisible ) {
190                                                 if ( $hsize >= 1024 ) {
191                                                         $hsize    = $hsize / 1024;
192                                                         $unit_id += 1;
193                                                 }        
194                                                 else {
195                                                         $lisible = true;
196                                                 } 
197                                         }
198                                 break;
199                                 
200                                 default:
201                                         $p = array_search( $unit, $units);
202                                         $hsize = $hsize / pow(1024,$p);
203                                 break;
204                         } // end switch
205                         
206                         $hsize = sprintf("%." . $decimal . "f", $hsize);
207                         $hsize = $hsize . ' ' . $units[$unit_id];
208                         return $hsize;
209                 } // end function
210
211                 
212                 function GetDbSize() 
213                 {
214                         $database_size  = 0;
215                         $query                  = "";
216                         
217                         switch( $this->driver )
218                         {
219                                 case 'mysql':
220                                         $query  = "SELECT table_schema AS 'database', sum( data_length + index_length) AS 'dbsize' ";
221                                         $query .= "FROM information_schema.TABLES ";
222                                         $query .= "WHERE table_schema = 'bacula' ";
223                                         $query .= "GROUP BY table_schema";
224                                 break;
225                                 case 'pgsql':
226                                         $query  = "SELECT pg_database_size('bacula') AS dbsize";
227                                 break;
228                                 case 'sqlite':
229                                         // Not yet implemented
230                                         return "0 MB";
231                                 break;
232                         }
233                         
234                         $result = $this->db_link->query( $query );
235                         
236                         if(! PEAR::isError( $result ) )
237                         {
238                                 $db = $result->fetchRow( DB_FETCHMODE_ASSOC );
239                                 $database_size =+ $db['dbsize'];
240                         }else
241                                 die( "Unable to get database size<br />" . $jobs->getMessage() );
242                         
243                         return $this->human_file_size( $database_size );  
244                         
245                         /*
246                         if ( $this->driver == "mysql") {
247                                 $dbsize = $this->db_link->query("show table status") or die ("classes.inc: Error query: 3");
248                                 
249                                 if ( $dbsize->numRows() ) {
250                                         while ( $res = $dbsize->fetchRow(DB_FETCHMODE_ASSOC) )
251                                                 $database_size += $res["Data_length"];
252                 } else {
253                                         return 0;
254                                 } // end if else
255             } // end if
256             else if ( $this->driver == "pgsql") {
257                                 $dbsize = $this->db_link->query("select pg_database_size('$this->dbs_name')") or die ("classes.inc: Error query: 4");
258                                 
259                                 if (PEAR::isError($dbsize))
260                                         die($dbsize->getMessage());
261                     
262                                 if ( $dbsize->numRows() ) {
263                                         while ( $res = $dbsize->fetchRow() )
264                                                 $database_size += $res[0];
265                 } else {
266                                         return 0;
267                                 }
268             } // end if       
269                                 
270                         $dbsize->free();
271                         */
272         } // end function GetDbSize()
273                 
274                 public function Get_Nb_Clients()
275                 {
276                         $clients = $this->db_link->query("SELECT COUNT(*) AS nb_client FROM Client");
277                         if( PEAR::isError($clients) )
278                                 die( "Unable to get client number" );
279                         else
280                                 return $clients->fetchRow( DB_FETCHMODE_ASSOC );
281                 }
282
283                 // Return an array of volumes ordered by poolid and volume name
284                 function GetVolumeList() {
285
286                 $volumes   = array();
287                 $query     = "";
288                                 $debug     = false;
289                                 
290                                 // Get the list of pools id
291                                 $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
292                                 
293                                 //$this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
294                                 $pools = $this->db_link->query( $query );
295                                 
296                                 if( PEAR::isError( $pools ) )
297                                         die("Error: Failed to get pool list <br />SQL Query: $query<br />" . $pools->getMessage() );
298                                 
299                                 while( $pool = $pools->fetchRow( DB_FETCHMODE_ASSOC ) ) {
300                                         switch( $this->driver )
301                                         {
302                                                 case 'mysql':
303 /*
304                                                         $query  = "SELECT Media.VolumeName, Media.VolBytes, Media.VolStatus, Pool.Name, Media.MediaType,Media.LastWritten, FROM_UNIXTIME(UNIX_TIMESTAMP(Media.LastWritten)+Media.VolRetention ) AS expire 
305                                                                            FROM Pool LEFT JOIN Media ON Media.PoolId=Pool.PoolId WHERE poolid='$pool[0]' 
306                                                                            ORDER BY Media.VolumeName";
307 */
308                                                         $query  = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
309                                                                            FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
310                                                                        WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
311                                                 break;
312                                                 case 'pgsql':
313                                                         $query  = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
314                                                                            FROM media LEFT JOIN pool ON media.poolid = pool.poolid
315                                                                        WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
316                                                         /*
317                                                         $query  = "SELECT Media.VolumeName, Media.VolBytes,Media.VolStatus,Pool.Name,Media.MediaType,Media.LastWritten, Media.LastWritten + Media.VolRetention * interval '1 second' AS expire 
318                                                                            FROM Pool LEFT JOIN Media ON media.poolid=pool.poolid WHERE poolid='$pool[0]' 
319                                                                            ORDER BY Media.VolumeName";
320                                                         */
321                                                 break;
322                                                 case 'sqlite':
323                                                         $query  = "";           // not yet implemented
324                                                 break;
325                                                 default:
326                                                 break;
327                                         } // end switch
328                                         
329                                         //$this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
330                                         $medias = $this->db_link->query( $query );
331
332                                         if( PEAR::isError( $medias ) ) {
333                                                 die( "Failed to get media list for pool $volume[0] <br /> " . $medias->getMessage() );
334                                         }else {
335                                                 if( $debug ) echo "Found " . $medias->numRows() . " medias for pool " . $pool['name'] . " <br />";
336                                         
337                                                 // Create array key for each pool
338                                                 if( !array_key_exists( $pool['name'], $volumes) )
339                                                 {
340                                                         $volumes[ $pool['name'] ] = array();
341                                                 }
342                                                 while( $media = $medias->fetchRow( DB_FETCHMODE_ASSOC ) ) {
343                                                         if( $debug ) {
344                                                                 var_dump( $media );
345                                                         }
346                                                         // If the pool is empty (no volumes in this pool)
347                                                         if( $medias->numRows() == 0 ) {
348                                                                 if( $debug ) echo "No media in pool " . $pool['name'] . "<br />";
349                                                         } else {
350                                                                         if( $media['lastwritten'] != "0000-00-00 00:00:00" ) {
351                                                                                 // Calculate expiration date if the volume is Full
352                                                                                 if( $media['volstatus'] == 'Full' ) {
353                                                                                         $expire_date     = strtotime($media['lastwritten']) + $media['volretention'];
354                                                                                         $media['expire'] = strftime("%Y-%m-%d", $expire_date);
355                                                                                 }else {
356                                                                                         $media['expire'] = 'N/A';
357                                                                                 }
358                                                                                 // Media used bytes in a human format
359                                                                                 $media['volbytes'] = $this->human_file_size( $media['volbytes'] );
360                                                                         } else {
361                                                                                 $media['lastwritten'] = "N/A";
362                                                                                 $media['expire']      = "N/A";
363                                                                                 $media['volbytes']        = "0 KB";
364                                                                         }                                                               
365                                                                 
366                                                                 // Odd or even row
367                                                                 if( count(  $volumes[ $pool['name'] ] ) % 2)
368                                                                         $media['class'] = 'odd';
369
370                                                                 // Add the media in pool array
371                                                                 array_push( $volumes[ $pool['name']], $media);
372                                                         }
373                                                 } // end while
374                                         } // end if else
375                                 } // end while
376                                 return $volumes;
377         } // end function GetVolumeList()
378                 
379                 public function CountJobsbyLevel( $delay = LAST_DAY, $level = 'F' )
380                 {
381                         $end_date    = mktime();
382                         $start_date  = $end_date - $delay;
383                         
384                         $start_date  = date( "Y-m-d H:i:s", $start_date );
385                         $end_date    = date( "Y-m-d H:i:s", $end_date );
386                         
387                         $query   = "SELECT COUNT(JobId) as jobs FROM Job ";
388                         $query  .= "WHERE (EndTime BETWEEN '$start_date' AND '$end_date') AND ";
389                         $query  .= "Level = '$level' ";
390                         
391                         $result  = $this->db_link->query( $query );
392                         
393                         if (PEAR::isError( $result ) ) {
394                                 die( "Unable to get number of jobs with $level status from catalog <br />" . $result->getMessage() );
395                         }else {
396                                 $jobs = $result->fetchRow( DB_FETCHMODE_ASSOC ); 
397                                 return $jobs['jobs'];
398                         }
399                         
400                 }
401                 
402                 public function CountJobs( $delay = LAST_DAY, $status = 'any' )
403                 {
404                         $query                  = "SELECT COUNT(JobId) AS job_nb FROM Job ";
405                         $where_delay    = "";
406                         $where_status   = "";
407                         
408                         // Interval condition for SQL query
409                         if( $delay != ALL ) {
410                                 $end_date    = mktime();
411                                 $start_date  = $end_date - $delay;
412                         
413                                 $start_date  = date( "Y-m-d H:i:s", $start_date );
414                                 $end_date    = date( "Y-m-d H:i:s", $end_date );
415                         
416                                 $where_delay = "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
417                         }
418                         
419                         if( $status != 'any' ) {
420                                 switch( $status )
421                                 {
422                                         case 'completed':
423                                                 $where_status = "JobStatus = 'T' ";
424                                         break;
425                                         case 'failed':
426                                                 $where_status = "JobStatus IN ('f','E') ";
427                                         break;
428                                         case 'canceled':
429                                                 $where_status = "JobStatus = 'A' ";
430                                         break;
431                                         case 'waiting':
432                                                 $where_status = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
433                                         break;
434                                 } // end switch
435                         }
436                         
437                         if( !empty($where_delay) )
438                                 $query = $query . $where_delay . 'AND ' . $where_status;
439                         else {
440                                 if( !empty($where_status) )
441                                         $query = $query . 'WHERE ' . $where_status;
442                         }
443                                 
444                         $jobs = $this->db_link->query( $query );
445                 
446                         if (PEAR::isError( $jobs ) ) {
447                                 die( "Unable to get last $status jobs number from catalog <br />" . $jobs->getMessage() );
448                         }else {
449                                 $jobs = $jobs->fetchRow( DB_FETCHMODE_ASSOC ); 
450                                 return $jobs['job_nb'];
451                         }
452                 }
453                 
454                 // Return the list of Pools in a array
455                 public function Get_Pools_List()
456                 {
457                         $pool_list = array();
458                         $result    = "";
459                         
460                         $query = "SELECT Name, PoolId FROM Pool";
461                         
462                         $result = $this->db_link->query ( $query );
463         
464                         if( PEAR::isError( $result ) ) {
465                                 die( "Unable to get the pool list from catalog" );                              
466                         }else {
467                                 while( $pool = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
468                                         array_push( $pool_list, array( $pool['Name'] => $pool['PoolId'] ) );
469                                 }
470                                 return $pool_list;
471                         }
472                 }
473                 
474                 public function Get_BackupJob_Names()
475                 {
476                         $query          = "SELECT Name FROM Job GROUP BY Name";
477                         $backupjobs = array();
478                         
479                         $result = $this->db_link->query( $query );
480                         
481                         if (PEAR::isError( $result ) ) {
482                                 die("Unable to get BackupJobs list from catalog" );
483                         }else{
484                                 while( $backupjob = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
485                                         array_push( $backupjobs, $backupjob["Name"] );
486                                 }
487                                 return $backupjobs;
488                         }
489                 }
490                 
491                 // Return elasped time string for a job
492                 function Get_ElapsedTime( $start_time, $end_time ) 
493                 { 
494                         $diff = $end_time - $start_time;
495                         
496                         $daysDiff = sprintf("%02d", floor($diff/60/60/24) );
497                         $diff -= $daysDiff*60*60*24;
498                         
499                         $hrsDiff = sprintf("%02d", floor($diff/60/60) );
500                         $diff -= $hrsDiff*60*60;
501                         
502                         $minsDiff = sprintf("%02d", floor($diff/60) );
503                         $diff -= $minsDiff*60;
504                         $secsDiff = sprintf("%02d", $diff );
505                         
506                         if( $daysDiff > 0 )
507                                 return $daysDiff . 'day(s) ' . $hrsDiff.':' . $minsDiff . ':' . $secsDiff;
508                         else
509                                 return $hrsDiff . ':' . $minsDiff . ':' . $secsDiff;
510                 }
511                 
512                 public function Get_ElapsedTime_Job( $delay = LAST_DAY )
513                 {
514                         $query                  = "";
515                         $total_elapsed  = 0;
516                         
517                         // Interval calculation
518                         $end_date   = mktime();
519                         $start_date = $end_date - $delay;
520                         
521                         $start_date = date( "Y-m-d H:i:s", $start_date );
522                         $end_date   = date( "Y-m-d H:i:s", $end_date );
523                         
524                         switch( $this->driver )
525                         {
526                                 case 'mysql':
527                                         $query  = "SELECT UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime) AS elapsed from Job ";
528                                         $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
529                                 break;
530                         }
531                         $result = $this->db_link->query( $query );
532                         
533                         if( PEAR::isError($result) ){
534                                 die( "Unable to get elapsed time for jobs from catalog<br />query = $query <br />" . $result->getMessage() );
535                         }else {
536                                 while( $time = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
537                                         //echo 'elapsed = ' . $time['elapsed'] . '<br />';
538                                         $total_elapsed += $time['elapsed'];
539                                 }
540                                 // Verify if elapsed time is more than 1 day
541                                 if ( $total_elapsed > LAST_DAY ) {
542                                         return date("%d days H:i:s", $total_elapsed );
543                                 }else {
544                                         return date("H:i:s", $total_elapsed );
545                                 }
546                         }
547                 }
548                 
549                 // Return Jobs statistics for a specific interval such as
550                 // - Completed jobs number
551                 // - Failed jobs number
552                 // - Waiting jobs number
553                 // The returned values will be used by a Bgraph classe
554                 public function GetJobsStatistics( $type = 'completed', $delay = LAST_DAY )
555                 {
556                         $query  = "";
557                         $where  = "";
558                         $jobs   = "";
559                         $label  = "";
560                         $res    = "";
561                         
562                         // Interval calculation
563                         $end_date   = mktime();
564                         $start_date = $end_date - $delay;
565                         
566                         $start_date = date( "Y-m-d H:i:s", $start_date );
567                         $end_date   = date( "Y-m-d H:i:s", $end_date );
568                         
569                         $interval_where = "(EndTime BETWEEN '$start_date' AND '$end_date') AND ";
570                         
571                         // Job status
572                         switch( $type )
573                         {
574                                 case 'completed':
575                                         $where = $interval_where . "JobStatus = 'T' ";
576                                         $label = "Completed";
577                                 break;
578                                 case 'terminated_errors':
579                                         $where = $interval_where . "JobStatus = 'E' ";
580                                         $label = "Terminated with errors";
581                                 break;
582                                 case 'failed':
583                                         $where = $interval_where . "JobStatus = 'f' ";
584                                         $label = "Failed";
585                                 break;
586                                 case 'waiting':
587                                         $where = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
588                                         $label = "Waiting";
589                                 break;
590                                 case 'created':
591                                         $where = "JobStatus = 'C' ";
592                                         $label = "Created but not running";
593                                 break;
594                                 case 'running':
595                                         $where = "JobStatus = 'R' ";
596                                         $label = "Running";
597                                 break;
598                                 case 'error':
599                                         $where = $interval_where . "JobStatus IN ('e','f') ";
600                                         $label = "Errors";
601                                 break;
602                         }
603                         
604                         $query  = 'SELECT COUNT(JobId) AS ' . $type . ' ';
605                         $query .= 'FROM Job ';
606                         $query .= "WHERE $where ";
607                 
608                         //echo 'query = ' . $query . '<br />';
609                         
610                         $jobs = $this->db_link->query( $query );
611                 
612                         if (PEAR::isError( $jobs ) ) {
613                                 die( "Unable to get last $type jobs status from catalog<br />" . $status->getMessage() );
614                         }else {
615                                 $res = $jobs->fetchRow();
616                                 return array( $label , current($res) );
617                         }
618                 } // end function GetJobsStatistics()
619                 
620                 public function GetPoolsStatistics( $pools )
621                 {
622                         foreach( $pools as $pool_name => $pool ) {
623                                 //var_dump( $pool );
624                                 $query = "SELECT COUNT(*) AS nb_vol FROM Media WHERE PoolId = '$pool'";
625                                 //echo $query . '<br />';
626                                 //echo 'Pool name ' . $pool_name . '<br />';
627                                 $result = $this->db_link->query( $query );
628                                 
629                                 if( PEAR::isError( $result ) ) {
630                                         die("Unable to get volume number from catalog");
631                                 }else{
632                                         $nb_vol = $result->fetchRow(DB_FETCHMODE_ASSOC);
633                                         return array( $pool_name, $nb_vol['nb_vol'] );
634                                 }
635                         }
636                 }
637                 
638                 public function GetStoredFiles( $delay = LAST_DAY )
639                 {
640                         $totalfiles = 0;
641
642                         $query = "SELECT SUM(JobFiles) AS stored_files FROM Job ";
643                         
644                         // Interval calculation
645                         $end_date   = mktime();
646                         $start_date = $end_date - $delay;
647                         
648                         $start_date = date( "Y-m-d H:i:s", $start_date );
649                         $end_date   = date( "Y-m-d H:i:s", $end_date );                 
650
651                         if( $delay != ALL )
652                                 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
653                                 
654                         $result = $this->db_link->query( $query );
655                         
656                         if( !PEAR::isError($result) ) {
657                                 $nbfiles        = $result->fetchRow(DB_FETCHMODE_ASSOC);
658                                 $totalfiles = $totalfiles + $nbfiles['stored_files'];
659                         }else{
660                                 die("Unable to get protected files from catalog <br />" . $result->getMessage() );
661                         }
662                         
663                         return $totalfiles;
664                 }
665                 
666                 public function GetStoredBytes( $delay = LAST_DAY )
667                 {
668                         $query = "SELECT SUM(JobBytes) as stored_bytes FROM Job ";
669                         
670                         // Interval calculation
671                         $end_date   = mktime();
672                         $start_date = $end_date - $delay;
673                         
674                         $start_date = date( "Y-m-d H:i:s", $start_date );
675                         $end_date   = date( "Y-m-d H:i:s", $end_date );
676                         
677                         if( $delay != ALL )
678                                 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
679                         
680                         $result = $this->db_link->query( $query );
681                         
682                         if( PEAR::isError( $result ) ) {
683                                 die( "Unable to get Job Bytes from catalog" );
684                         }else{
685                                 return $result->fetchRow( DB_FETCHMODE_ASSOC );
686                         }
687                 }
688                 
689                 public function GetStoredBytesByInterval( $start_date, $end_date )
690                 {
691                         $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
692                         
693                         $result = $this->db_link->query( $query );
694                         
695                         if( PEAR::isError( $result ) ) {
696                                 die( "Unable to get Job Bytes from catalog" );
697                         }else{
698                                 $stored_bytes = 0;
699                                 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
700                                 
701                                 $day = date( "D d", strtotime($end_date) );
702                                 
703                                 if( isset( $tmp['stored_bytes'] ) ) {
704                                         $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
705                                         $hbytes = explode( " ", $hbytes );
706                                         $stored_bytes = $hbytes[0];
707                                 }
708                                 
709                                 return array( $day, $stored_bytes );
710                         }
711                 }
712                 
713                 public function GetStoredBytesByJob( $jobname, $start_date, $end_date )
714                 {
715                         $query  = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job ";
716                         $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
717                         $query .= "Name = '$jobname'";
718                         
719                         $result = $this->db_link->query( $query );
720                         
721                         if( PEAR::isError( $result ) ) {
722                                 die( "Unable to get Job Bytes from catalog" );
723                         }else{
724                                 $stored_bytes = 0;
725                                 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
726                                 
727                                 $day = date( "D d", strtotime($end_date) );
728                                 
729                                 if( isset( $tmp['stored_bytes'] ) ) {
730                                         $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
731                                         $hbytes = explode( " ", $hbytes );
732                                         $stored_bytes = $hbytes[0];
733                                 }
734                                 
735                                 return array( $day, $stored_bytes );
736                         }                       
737                 }
738                 
739                 public function GetStoredFilesByJob( $jobname, $start_date, $end_date )
740                 {
741                         $query  = "SELECT SUM(JobFiles) as stored_files, EndTime FROM Job ";
742                         $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
743                         $query .= "Name = '$jobname'";
744                         
745                         $result = $this->db_link->query( $query );
746                         
747                         if( PEAR::isError( $result ) ) {
748                                 die( "Unable to get Job Files from catalog" );
749                         }else{
750                                 $stored_bytes = 0;
751                                 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
752                                 
753                                 $day                    = date( "D d", strtotime($end_date) );
754                                 $stored_files   = $tmp['stored_files'];
755                                 
756                                 return array( $day, $stored_files );
757                         }                       
758                 }
759
760 } // end class Bweb
761 ?>