]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/bweb.inc.php
3d756a246fdc475af7d3d9e4a2a037591a9cb507
[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 CountJobs( $delay = LAST_DAY, $status = 'any' )
380                 {
381                         $query                  = "SELECT COUNT(JobId) AS job_nb FROM Job ";
382                         $where_delay    = "";
383                         $where_status   = "";
384                         
385                         // Interval condition for SQL query
386                         if( $delay != ALL ) {
387                                 $end_date    = mktime();
388                                 $start_date  = $end_date - $delay;
389                         
390                                 $start_date  = date( "Y-m-d H:i:s", $start_date );
391                                 $end_date    = date( "Y-m-d H:i:s", $end_date );
392                         
393                                 $where_delay = "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
394                         }
395                         
396                         if( $status != 'any' ) {
397                                 switch( $status )
398                                 {
399                                         case 'completed':
400                                                 $where_status = "JobStatus = 'T' ";
401                                         break;
402                                         case 'failed':
403                                                 $where_status = "JobStatus IN ('f','E') ";
404                                         break;
405                                         case 'canceled':
406                                                 $where_status = "JobStatus = 'A' ";
407                                         break;
408                                         case 'waiting':
409                                                 $where_status = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
410                                         break;
411                                 } // end switch
412                         }
413                         
414                         if( !empty($where_delay) )
415                                 $query = $query . $where_delay . 'AND ' . $where_status;
416                         else {
417                                 if( !empty($where_status) )
418                                         $query = $query . 'WHERE ' . $where_status;
419                         }
420                                 
421                         $jobs = $this->db_link->query( $query );
422                 
423                         if (PEAR::isError( $jobs ) ) {
424                                 die( "Unable to get last $status jobs number from catalog <br />" . $jobs->getMessage() );
425                         }else {
426                                 $jobs = $jobs->fetchRow( DB_FETCHMODE_ASSOC ); 
427                                 return $jobs['job_nb'];
428                         }
429                 }
430                 
431                 // Return the list of Pools in a array
432                 public function Get_Pools_List()
433                 {
434                         $pool_list = array();
435                         $result    = "";
436                         
437                         $query = "SELECT Name, PoolId FROM Pool";
438                         
439                         $result = $this->db_link->query ( $query );
440         
441                         if( PEAR::isError( $result ) ) {
442                                 die( "Unable to get the pool list from catalog" );                              
443                         }else {
444                                 while( $pool = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
445                                         array_push( $pool_list, array( $pool['Name'] => $pool['PoolId'] ) );
446                                 }
447                                 return $pool_list;
448                         }
449                 }
450                 
451                 public function Get_BackupJob_Names()
452                 {
453                         $query          = "SELECT Name FROM Job GROUP BY Name";
454                         $backupjobs = array();
455                         
456                         $result = $this->db_link->query( $query );
457                         
458                         if (PEAR::isError( $result ) ) {
459                                 die("Unable to get BackupJobs list from catalog" );
460                         }else{
461                                 while( $backupjob = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
462                                         array_push( $backupjobs, $backupjob["Name"] );
463                                 }
464                                 return $backupjobs;
465                         }
466                 }
467                 
468                 // Return elasped time string for a job
469                 function Get_ElapsedTime( $start_time, $end_time ) 
470                 { 
471                         $diff = $end_time - $start_time;
472                         
473                         $daysDiff = sprintf("%02d", floor($diff/60/60/24) );
474                         $diff -= $daysDiff*60*60*24;
475                         
476                         $hrsDiff = sprintf("%02d", floor($diff/60/60) );
477                         $diff -= $hrsDiff*60*60;
478                         
479                         $minsDiff = sprintf("%02d", floor($diff/60) );
480                         $diff -= $minsDiff*60;
481                         $secsDiff = sprintf("%02d", $diff );
482                         
483                         if( $daysDiff > 0 )
484                                 return $daysDiff . 'day(s) ' . $hrsDiff.':' . $minsDiff . ':' . $secsDiff;
485                         else
486                                 return $hrsDiff . ':' . $minsDiff . ':' . $secsDiff;
487                 }
488                 
489                 public function Get_ElapsedTime_Job( $delay = LAST_DAY )
490                 {
491                         $query                  = "";
492                         $total_elapsed  = 0;
493                         
494                         // Interval calculation
495                         $end_date   = mktime();
496                         $start_date = $end_date - $delay;
497                         
498                         $start_date = date( "Y-m-d H:i:s", $start_date );
499                         $end_date   = date( "Y-m-d H:i:s", $end_date );
500                         
501                         switch( $this->driver )
502                         {
503                                 case 'mysql':
504                                         $query  = "SELECT UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime) AS elapsed from Job ";
505                                         $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
506                                 break;
507                         }
508                         $result = $this->db_link->query( $query );
509                         
510                         if( PEAR::isError($result) ){
511                                 die( "Unable to get elapsed time for jobs from catalog<br />query = $query <br />" . $result->getMessage() );
512                         }else {
513                                 while( $time = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
514                                         //echo 'elapsed = ' . $time['elapsed'] . '<br />';
515                                         $total_elapsed += $time['elapsed'];
516                                 }
517                                 // Verify if elapsed time is more than 1 day
518                                 if ( $total_elapsed > LAST_DAY ) {
519                                         return date("%d days H:i:s", $total_elapsed );
520                                 }else {
521                                         return date("H:i:s", $total_elapsed );
522                                 }
523                         }
524                 }
525                 
526                 // Return Jobs statistics for a specific interval such as
527                 // - Completed jobs number
528                 // - Failed jobs number
529                 // - Waiting jobs number
530                 // The returned values will be used by a Bgraph classe
531                 public function GetJobsStatistics( $type = 'completed', $delay = LAST_DAY )
532                 {
533                         $query  = "";
534                         $where  = "";
535                         $jobs   = "";
536                         $label  = "";
537                         $res    = "";
538                         
539                         // Interval calculation
540                         $end_date   = mktime();
541                         $start_date = $end_date - $delay;
542                         
543                         $start_date = date( "Y-m-d H:i:s", $start_date );
544                         $end_date   = date( "Y-m-d H:i:s", $end_date );
545                         
546                         $interval_where = "(EndTime BETWEEN '$start_date' AND '$end_date') AND ";
547                         
548                         // Job status
549                         switch( $type )
550                         {
551                                 case 'completed':
552                                         $where = $interval_where . "JobStatus = 'T' ";
553                                         $label = "Completed";
554                                 break;
555                                 case 'terminated_errors':
556                                         $where = $interval_where . "JobStatus = 'E' ";
557                                         $label = "Terminated with errors";
558                                 break;
559                                 case 'failed':
560                                         $where = $interval_where . "JobStatus = 'f' ";
561                                         $label = "Failed";
562                                 break;
563                                 case 'waiting':
564                                         $where = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
565                                         $label = "Waiting";
566                                 break;
567                                 case 'created':
568                                         $where = "JobStatus = 'C' ";
569                                         $label = "Created but not running";
570                                 break;
571                                 case 'running':
572                                         $where = "JobStatus = 'R' ";
573                                         $label = "Running";
574                                 break;
575                                 case 'error':
576                                         $where = $interval_where . "JobStatus IN ('e','f') ";
577                                         $label = "Errors";
578                                 break;
579                         }
580                         
581                         $query  = 'SELECT COUNT(JobId) AS ' . $type . ' ';
582                         $query .= 'FROM Job ';
583                         $query .= "WHERE $where ";
584                 
585                         //echo 'query = ' . $query . '<br />';
586                         
587                         $jobs = $this->db_link->query( $query );
588                 
589                         if (PEAR::isError( $jobs ) ) {
590                                 die( "Unable to get last $type jobs status from catalog<br />" . $status->getMessage() );
591                         }else {
592                                 $res = $jobs->fetchRow();
593                                 return array( $label , current($res) );
594                         }
595                 } // end function GetJobsStatistics()
596                 
597                 public function GetPoolsStatistics( $pools )
598                 {
599                         foreach( $pools as $pool_name => $pool ) {
600                                 //var_dump( $pool );
601                                 $query = "SELECT COUNT(*) AS nb_vol FROM Media WHERE PoolId = '$pool'";
602                                 //echo $query . '<br />';
603                                 //echo 'Pool name ' . $pool_name . '<br />';
604                                 $result = $this->db_link->query( $query );
605                                 
606                                 if( PEAR::isError( $result ) ) {
607                                         die("Unable to get volume number from catalog");
608                                 }else{
609                                         $nb_vol = $result->fetchRow(DB_FETCHMODE_ASSOC);
610                                         return array( $pool_name, $nb_vol['nb_vol'] );
611                                 }
612                         }
613                 }
614                 
615                 public function GetStoredFiles()
616                 {
617                         $nbfiles = 0;
618                         $query = "SELECT COUNT(FilenameId) AS nbfiles FROM Filename";
619                         $result = $this->db_link->query( $query );
620                         
621                         if( !PEAR::isError($result) ) {
622                                 $nbfiles = $result->fetchRow(DB_FETCHMODE_ASSOC);
623                                 $nbfiles = $nbfiles['nbfiles'];
624                         }else{
625                                 die("Unable to get protected files from catalog");
626                         }
627                         return $nbfiles;
628                 }
629                 
630                 public function GetStoredBytes( $delay = LAST_DAY )
631                 {
632                         $query = "SELECT SUM(JobBytes) as stored_bytes FROM Job ";
633                         
634                         // Interval calculation
635                         $end_date   = mktime();
636                         $start_date = $end_date - $delay;
637                         
638                         $start_date = date( "Y-m-d H:i:s", $start_date );
639                         $end_date   = date( "Y-m-d H:i:s", $end_date );
640                         
641                         if( $delay != ALL ) {
642                                 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
643                         }
644                         
645                         $result = $this->db_link->query( $query );
646                         
647                         if( PEAR::isError( $result ) ) {
648                                 die( "Unable to get Job Bytes from catalog" );
649                         }else{
650                                 return $result->fetchRow( DB_FETCHMODE_ASSOC );
651                         }
652                 }
653                 
654                 public function GetStoredBytesByInterval( $start_date, $end_date )
655                 {
656                         $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
657                         
658                         $result = $this->db_link->query( $query );
659                         
660                         if( PEAR::isError( $result ) ) {
661                                 die( "Unable to get Job Bytes from catalog" );
662                         }else{
663                                 $stored_bytes = 0;
664                                 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
665                                 
666                                 $day = date( "D d", strtotime($end_date) );
667                                 
668                                 if( isset( $tmp['stored_bytes'] ) ) {
669                                         $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
670                                         $hbytes = explode( " ", $hbytes );
671                                         $stored_bytes = $hbytes[0];
672                                 }
673                                 
674                                 return array( $day, $stored_bytes );
675                         }
676                 }
677                 
678                 public function GetStoredBytesByJob( $jobname, $start_date, $end_date )
679                 {
680                         $query  = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job ";
681                         $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
682                         $query .= "Name = '$jobname'";
683                         
684                         $result = $this->db_link->query( $query );
685                         
686                         if( PEAR::isError( $result ) ) {
687                                 die( "Unable to get Job Bytes from catalog" );
688                         }else{
689                                 $stored_bytes = 0;
690                                 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
691                                 
692                                 $day = date( "D d", strtotime($end_date) );
693                                 
694                                 if( isset( $tmp['stored_bytes'] ) ) {
695                                         $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
696                                         $hbytes = explode( " ", $hbytes );
697                                         $stored_bytes = $hbytes[0];
698                                 }
699                                 
700                                 return array( $day, $stored_bytes );
701                         }                       
702                 }
703                 
704                 public function GetStoredFilesByJob( $jobname, $start_date, $end_date )
705                 {
706                         $query  = "SELECT SUM(JobFiles) as stored_files, EndTime FROM Job ";
707                         $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
708                         $query .= "Name = '$jobname'";
709                         
710                         $result = $this->db_link->query( $query );
711                         
712                         if( PEAR::isError( $result ) ) {
713                                 die( "Unable to get Job Files from catalog" );
714                         }else{
715                                 $stored_bytes = 0;
716                                 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
717                                 
718                                 $day                    = date( "D d", strtotime($end_date) );
719                                 $stored_files   = $tmp['stored_files'];
720                                 
721                                 return array( $day, $stored_files );
722                         }                       
723                 }
724
725 } // end class Bweb
726 ?>