]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/bweb.inc.php
bacula-web: Several improvments with php code
[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($smarty_path."Smarty.class.php");
19
20 require_once "DB.php";                                                                                                                  // Pear DB
21 require_once "config.inc.php";
22 require_once "bgraph.inc.php";
23
24 class Bweb extends DB {
25
26     var $StartDate;
27     var $EndDate;
28     var $driver;
29         var $dbs;
30         var $dbs_name;
31         
32         public $tpl;
33         public $db_link;                                                // Database link
34         private $db_dsn;                                                // Data Source Name
35         
36         private $config_file;                                   // Config filename
37         private $config;                                                // Loaded config from bacula.conf
38         private $catalogs = array();                    // Catalog array
39
40     function __construct()
41         {             
42                 $this->catalogs = array();
43                 
44                 // Loading configuration
45                 $this->config_file = getcwd() . '/configs/bacula.conf';
46                 
47                 if( !$this->load_config() )
48                         die( "Unable to load configuration");
49                         
50                 // Construct a valid dsn
51         $this->db_dsn['hostspec'] = $this->catalogs[0]["host"];
52         $this->db_dsn['username'] = $this->catalogs[0]["login"];
53                 $this->db_dsn['password'] = $this->catalogs[0]["pass"];
54                 $this->db_dsn['database'] = $this->catalogs[0]["db_name"];
55                 $this->db_dsn['phptype']  = $this->catalogs[0]["db_type"];
56                 
57                                         
58         $this->db_link = $this->connect($this->db_dsn);
59         
60                 if (DB::isError($this->db_link)) {
61                         die($this->db_link->getMessage());
62                 }else {
63                         $this->driver = $this->db_dsn['phptype'];                            
64             register_shutdown_function(array(&$this,'close'));
65                         $this->dbs_name = $this->db_dsn['database'];
66                 }
67                 
68                 // Initialize smarty template classe
69                 $this->init_tpl();
70                 // Initialize smarty gettext function
71                 $this->init_gettext();
72         }
73                 
74     // Initialize Smarty template classe
75         function init_tpl()
76         {
77                 $this->tpl = new Smarty();
78                 
79                 $this->tpl->compile_check       = true;
80                 $this->tpl->debugging           = false;
81                 $this->tpl->force_compile       = true;
82
83                 $this->tpl->template_dir        = "./templates";
84                 $this->tpl->compile_dir         = "./templates_c";
85                 $this->tpl->config_dir          = "./configs";
86         }
87         
88         function init_gettext()
89         {
90                 global $smarty_gettext_path;
91                 
92                 if ( function_exists("gettext") ) {
93                         require_once( $smarty_gettext_path."smarty_gettext.php" );     
94                         $this->tpl->register_block('t','smarty_translate');
95         
96                         $language = $this->get_config_param("lang");
97                         $domain = "messages";   
98                         putenv("LANG=$language"); 
99                         setlocale(LC_ALL, $language);
100                         bindtextdomain($domain,"./locale");
101                         textdomain($domain);
102                 }
103                 else {
104                         function smarty_translate($params, $text, &$smarty) {
105                 return $text;
106                         }
107                         $smarty->register_block('t','smarty_translate');
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         } // end function GetDbSize()
245                 
246                 public function Get_Nb_Clients()
247                 {
248                         $clients = $this->db_link->query("SELECT COUNT(*) AS nb_client FROM Client");
249                         if( PEAR::isError($clients) )
250                                 die( "Unable to get client number" );
251                         else
252                                 return $clients->fetchRow( DB_FETCHMODE_ASSOC );
253                 }
254
255                 // Return an array of volumes ordered by poolid and volume name
256                 function GetVolumeList() {
257
258                 $volumes   = array();
259                 $query     = "";
260                                 $debug     = false;
261                                 
262                                 // Get the list of pools id
263                                 $query = "SELECT Pool.poolid, Pool.name FROM Pool ORDER BY Pool.poolid";
264                                 
265                                 //$this->db_link->setFetchMode(DB_FETCHMODE_ASSOC);
266                                 $pools = $this->db_link->query( $query );
267                                 
268                                 if( PEAR::isError( $pools ) )
269                                         die("Error: Failed to get pool list <br />SQL Query: $query<br />" . $pools->getMessage() );
270                                 
271                                 while( $pool = $pools->fetchRow( DB_FETCHMODE_ASSOC ) ) {
272                                         switch( $this->driver )
273                                         {
274                                                 case 'mysql':
275                                                         $query  = "SELECT Media.volumename, Media.volbytes, Media.volstatus, Media.mediatype, Media.lastwritten, Media.volretention
276                                                                            FROM Media LEFT JOIN Pool ON Media.poolid = Pool.poolid
277                                                                        WHERE Media.poolid = '". $pool['poolid'] . "' ORDER BY Media.volumename";
278                                                 break;
279                                                 case 'pgsql':
280                                                         $query  = "SELECT media.volumename, media.volbytes, media.volstatus, media.mediatype, media.lastwritten, media.volretention
281                                                                            FROM media LEFT JOIN pool ON media.poolid = pool.poolid
282                                                                        WHERE media.poolid = '". $pool['poolid'] . "' ORDER BY media.volumename";
283                                                 break;
284                                                 case 'sqlite':
285                                                         $query  = "";           // not yet implemented
286                                                 break;
287                                                 default:
288                                                 break;
289                                         } // end switch
290                                         
291                                         $medias = $this->db_link->query( $query );
292
293                                         if( PEAR::isError( $medias ) ) {
294                                                 die( "Failed to get media list for pool $volume[0] <br /> " . $medias->getMessage() );
295                                         }else {
296                                                 if( $debug ) echo "Found " . $medias->numRows() . " medias for pool " . $pool['name'] . " <br />";
297                                         
298                                                 // Create array key for each pool
299                                                 if( !array_key_exists( $pool['name'], $volumes) )
300                                                 {
301                                                         $volumes[ $pool['name'] ] = array();
302                                                 }
303                                                 while( $media = $medias->fetchRow( DB_FETCHMODE_ASSOC ) ) {
304                                                         if( $debug ) {
305                                                                 var_dump( $media );
306                                                         }
307                                                         // If the pool is empty (no volumes in this pool)
308                                                         if( $medias->numRows() == 0 ) {
309                                                                 if( $debug ) echo "No media in pool " . $pool['name'] . "<br />";
310                                                         } else {
311                                                                         if( $media['lastwritten'] != "0000-00-00 00:00:00" ) {
312                                                                                 // Calculate expiration date if the volume is Full
313                                                                                 if( $media['volstatus'] == 'Full' ) {
314                                                                                         $expire_date     = strtotime($media['lastwritten']) + $media['volretention'];
315                                                                                         $media['expire'] = strftime("%Y-%m-%d", $expire_date);
316                                                                                 }else {
317                                                                                         $media['expire'] = 'N/A';
318                                                                                 }
319                                                                                 // Media used bytes in a human format
320                                                                                 $media['volbytes'] = $this->human_file_size( $media['volbytes'] );
321                                                                         } else {
322                                                                                 $media['lastwritten'] = "N/A";
323                                                                                 $media['expire']      = "N/A";
324                                                                                 $media['volbytes']        = "0 KB";
325                                                                         }                                                               
326                                                                 
327                                                                 // Odd or even row
328                                                                 if( count(  $volumes[ $pool['name'] ] ) % 2)
329                                                                         $media['class'] = 'odd';
330
331                                                                 // Add the media in pool array
332                                                                 array_push( $volumes[ $pool['name']], $media);
333                                                         }
334                                                 } // end while
335                                         } // end if else
336                                 } // end while
337                                 return $volumes;
338         } // end function GetVolumeList()
339                 
340                 public function CountJobsbyLevel( $delay = LAST_DAY, $level = 'F' )
341                 {
342                         $end_date    = mktime();
343                         $start_date  = $end_date - $delay;
344                         
345                         $start_date  = date( "Y-m-d H:i:s", $start_date );
346                         $end_date    = date( "Y-m-d H:i:s", $end_date );
347                         
348                         $query   = "SELECT COUNT(JobId) as jobs FROM Job ";
349                         $query  .= "WHERE (EndTime BETWEEN '$start_date' AND '$end_date') AND ";
350                         $query  .= "Level = '$level' ";
351                         
352                         $result  = $this->db_link->query( $query );
353                         
354                         if (PEAR::isError( $result ) ) {
355                                 die( "Unable to get number of jobs with $level status from catalog <br />" . $result->getMessage() );
356                         }else {
357                                 $jobs = $result->fetchRow( DB_FETCHMODE_ASSOC ); 
358                                 return $jobs['jobs'];
359                         }
360                         
361                 }
362                 
363                 public function CountJobs( $delay = LAST_DAY, $status = 'any' )
364                 {
365                         $query                  = "SELECT COUNT(JobId) AS job_nb FROM Job ";
366                         $where_delay    = "";
367                         $where_status   = "";
368                         
369                         // Interval condition for SQL query
370                         if( $delay != ALL ) {
371                                 $end_date    = mktime();
372                                 $start_date  = $end_date - $delay;
373                         
374                                 $start_date  = date( "Y-m-d H:i:s", $start_date );
375                                 $end_date    = date( "Y-m-d H:i:s", $end_date );
376                         
377                                 $where_delay = "WHERE EndTime BETWEEN '$start_date' AND '$end_date' ";
378                         }
379                         
380                         if( $status != 'any' ) {
381                                 switch( $status )
382                                 {
383                                         case 'completed':
384                                                 $where_status = "JobStatus = 'T' ";
385                                         break;
386                                         case 'failed':
387                                                 $where_status = "JobStatus IN ('f','E') ";
388                                         break;
389                                         case 'canceled':
390                                                 $where_status = "JobStatus = 'A' ";
391                                         break;
392                                         case 'waiting':
393                                                 $where_status = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
394                                         break;
395                                 } // end switch
396                         }
397                         
398                         if( !empty($where_delay) )
399                                 $query = $query . $where_delay . 'AND ' . $where_status;
400                         else {
401                                 if( !empty($where_status) )
402                                         $query = $query . 'WHERE ' . $where_status;
403                         }
404                                 
405                         $jobs = $this->db_link->query( $query );
406                 
407                         if (PEAR::isError( $jobs ) ) {
408                                 die( "Unable to get last $status jobs number from catalog <br />" . $jobs->getMessage() );
409                         }else {
410                                 $jobs = $jobs->fetchRow( DB_FETCHMODE_ASSOC ); 
411                                 return $jobs['job_nb'];
412                         }
413                 }
414                 
415                 // Return the list of Pools in a array
416                 public function Get_Pools_List()
417                 {
418                         $pool_list = array();
419                         $result    = "";
420                         
421                         $query = "SELECT Name, PoolId FROM Pool";
422                         
423                         $result = $this->db_link->query ( $query );
424         
425                         if( PEAR::isError( $result ) ) {
426                                 die( "Unable to get the pool list from catalog" );                              
427                         }else {
428                                 while( $pool = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
429                                         array_push( $pool_list, array( $pool['Name'] => $pool['PoolId'] ) );
430                                 }
431                                 return $pool_list;
432                         }
433                 }
434                 
435                 public function Get_BackupJob_Names()
436                 {
437                         $query          = "SELECT Name FROM Job GROUP BY Name";
438                         $backupjobs = array();
439                         
440                         $result = $this->db_link->query( $query );
441                         
442                         if (PEAR::isError( $result ) ) {
443                                 die("Unable to get BackupJobs list from catalog" );
444                         }else{
445                                 while( $backupjob = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
446                                         array_push( $backupjobs, $backupjob["Name"] );
447                                 }
448                                 return $backupjobs;
449                         }
450                 }
451                 
452                 // Return elasped time string for a job
453                 function Get_ElapsedTime( $start_time, $end_time ) 
454                 { 
455                         $diff = $end_time - $start_time;
456                         
457                         $daysDiff = sprintf("%02d", floor($diff/60/60/24) );
458                         $diff -= $daysDiff*60*60*24;
459                         
460                         $hrsDiff = sprintf("%02d", floor($diff/60/60) );
461                         $diff -= $hrsDiff*60*60;
462                         
463                         $minsDiff = sprintf("%02d", floor($diff/60) );
464                         $diff -= $minsDiff*60;
465                         $secsDiff = sprintf("%02d", $diff );
466                         
467                         if( $daysDiff > 0 )
468                                 return $daysDiff . 'day(s) ' . $hrsDiff.':' . $minsDiff . ':' . $secsDiff;
469                         else
470                                 return $hrsDiff . ':' . $minsDiff . ':' . $secsDiff;
471                 }
472                 
473                 public function Get_ElapsedTime_Job( $delay = LAST_DAY )
474                 {
475                         $query                  = "";
476                         $total_elapsed  = 0;
477                         
478                         // Interval calculation
479                         $end_date   = mktime();
480                         $start_date = $end_date - $delay;
481                         
482                         $start_date = date( "Y-m-d H:i:s", $start_date );
483                         $end_date   = date( "Y-m-d H:i:s", $end_date );
484                         
485                         switch( $this->driver )
486                         {
487                                 case 'mysql':
488                                         $query  = "SELECT UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime) AS elapsed from Job ";
489                                         $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
490                                 break;
491                         }
492                         $result = $this->db_link->query( $query );
493                         
494                         if( PEAR::isError($result) ){
495                                 die( "Unable to get elapsed time for jobs from catalog<br />query = $query <br />" . $result->getMessage() );
496                         }else {
497                                 while( $time = $result->fetchRow( DB_FETCHMODE_ASSOC ) ) {
498                                         //echo 'elapsed = ' . $time['elapsed'] . '<br />';
499                                         $total_elapsed += $time['elapsed'];
500                                 }
501                                 // Verify if elapsed time is more than 1 day
502                                 if ( $total_elapsed > LAST_DAY ) {
503                                         return date("%d days H:i:s", $total_elapsed );
504                                 }else {
505                                         return date("H:i:s", $total_elapsed );
506                                 }
507                         }
508                 }
509                 
510                 // Return Jobs statistics for a specific interval such as
511                 // - Completed jobs number
512                 // - Failed jobs number
513                 // - Waiting jobs number
514                 // The returned values will be used by a Bgraph classe
515                 public function GetJobsStatistics( $type = 'completed', $delay = LAST_DAY )
516                 {
517                         $query  = "";
518                         $where  = "";
519                         $jobs   = "";
520                         $label  = "";
521                         $res    = "";
522                         
523                         // Interval calculation
524                         $end_date   = mktime();
525                         $start_date = $end_date - $delay;
526                         
527                         $start_date = date( "Y-m-d H:i:s", $start_date );
528                         $end_date   = date( "Y-m-d H:i:s", $end_date );
529                         
530                         $interval_where = "(EndTime BETWEEN '$start_date' AND '$end_date') AND ";
531                         
532                         // Job status
533                         switch( $type )
534                         {
535                                 case 'completed':
536                                         $where = $interval_where . "JobStatus = 'T' ";
537                                         $label = "Completed";
538                                 break;
539                                 case 'terminated_errors':
540                                         $where = $interval_where . "JobStatus = 'E' ";
541                                         $label = "Terminated with errors";
542                                 break;
543                                 case 'failed':
544                                         $where = $interval_where . "JobStatus = 'f' ";
545                                         $label = "Failed";
546                                 break;
547                                 case 'waiting':
548                                         $where = "JobStatus IN ('F','S','M','m','s','j','c','d','t') ";
549                                         $label = "Waiting";
550                                 break;
551                                 case 'created':
552                                         $where = "JobStatus = 'C' ";
553                                         $label = "Created but not running";
554                                 break;
555                                 case 'running':
556                                         $where = "JobStatus = 'R' ";
557                                         $label = "Running";
558                                 break;
559                                 case 'error':
560                                         $where = $interval_where . "JobStatus IN ('e','f') ";
561                                         $label = "Errors";
562                                 break;
563                         }
564                         
565                         $query  = 'SELECT COUNT(JobId) AS ' . $type . ' ';
566                         $query .= 'FROM Job ';
567                         $query .= "WHERE $where ";
568                 
569                         //echo 'query = ' . $query . '<br />';
570                         
571                         $jobs = $this->db_link->query( $query );
572                 
573                         if (PEAR::isError( $jobs ) ) {
574                                 die( "Unable to get last $type jobs status from catalog<br />" . $status->getMessage() );
575                         }else {
576                                 $res = $jobs->fetchRow();
577                                 return array( $label , current($res) );
578                         }
579                 } // end function GetJobsStatistics()
580                 
581                 public function GetPoolsStatistics( $pools )
582                 {
583                         foreach( $pools as $pool_name => $pool ) {
584                                 //var_dump( $pool );
585                                 $query = "SELECT COUNT(*) AS nb_vol FROM Media WHERE PoolId = '$pool'";
586                                 //echo $query . '<br />';
587                                 //echo 'Pool name ' . $pool_name . '<br />';
588                                 $result = $this->db_link->query( $query );
589                                 
590                                 if( PEAR::isError( $result ) ) {
591                                         die("Unable to get volume number from catalog");
592                                 }else{
593                                         $nb_vol = $result->fetchRow(DB_FETCHMODE_ASSOC);
594                                         return array( $pool_name, $nb_vol['nb_vol'] );
595                                 }
596                         }
597                 }
598                 
599                 public function GetStoredFiles( $delay = LAST_DAY )
600                 {
601                         $totalfiles = 0;
602
603                         $query = "SELECT SUM(JobFiles) AS stored_files FROM Job ";
604                         
605                         // Interval calculation
606                         $end_date   = mktime();
607                         $start_date = $end_date - $delay;
608                         
609                         $start_date = date( "Y-m-d H:i:s", $start_date );
610                         $end_date   = date( "Y-m-d H:i:s", $end_date );                 
611
612                         if( $delay != ALL )
613                                 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
614                                 
615                         $result = $this->db_link->query( $query );
616                         
617                         if( !PEAR::isError($result) ) {
618                                 $nbfiles        = $result->fetchRow(DB_FETCHMODE_ASSOC);
619                                 $totalfiles = $totalfiles + $nbfiles['stored_files'];
620                         }else{
621                                 die("Unable to get protected files from catalog <br />" . $result->getMessage() );
622                         }
623                         
624                         return $totalfiles;
625                 }
626                 
627                 public function GetStoredBytes( $delay = LAST_DAY )
628                 {
629                         $query = "SELECT SUM(JobBytes) as stored_bytes FROM Job ";
630                         
631                         // Interval calculation
632                         $end_date   = mktime();
633                         $start_date = $end_date - $delay;
634                         
635                         $start_date = date( "Y-m-d H:i:s", $start_date );
636                         $end_date   = date( "Y-m-d H:i:s", $end_date );
637                         
638                         if( $delay != ALL )
639                                 $query .= "WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
640                         
641                         $result = $this->db_link->query( $query );
642                         
643                         if( PEAR::isError( $result ) ) {
644                                 die( "Unable to get Job Bytes from catalog" );
645                         }else{
646                                 return $result->fetchRow( DB_FETCHMODE_ASSOC );
647                         }
648                 }
649                 
650                 public function GetStoredBytesByInterval( $start_date, $end_date )
651                 {
652                         $query = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job WHERE EndTime BETWEEN '$start_date' AND '$end_date'";
653                         
654                         $result = $this->db_link->query( $query );
655                         
656                         if( PEAR::isError( $result ) ) {
657                                 die( "Unable to get Job Bytes from catalog" );
658                         }else{
659                                 $stored_bytes = 0;
660                                 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
661                                 
662                                 $day = date( "D d", strtotime($end_date) );
663                                 
664                                 if( isset( $tmp['stored_bytes'] ) ) {
665                                         $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
666                                         $hbytes = explode( " ", $hbytes );
667                                         $stored_bytes = $hbytes[0];
668                                 }
669                                 
670                                 return array( $day, $stored_bytes );
671                         }
672                 }
673                 
674                 public function GetStoredBytesByJob( $jobname, $start_date, $end_date )
675                 {
676                         $query  = "SELECT SUM(JobBytes) as stored_bytes, EndTime FROM Job ";
677                         $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
678                         $query .= "Name = '$jobname'";
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                                 $stored_bytes = 0;
686                                 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
687                                 
688                                 $day = date( "D d", strtotime($end_date) );
689                                 
690                                 if( isset( $tmp['stored_bytes'] ) ) {
691                                         $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
692                                         $hbytes = explode( " ", $hbytes );
693                                         $stored_bytes = $hbytes[0];
694                                 }
695                                 
696                                 return array( $day, $stored_bytes );
697                         }                       
698                 }
699                 
700                 public function GetStoredFilesByJob( $jobname, $start_date, $end_date )
701                 {
702                         $query  = "SELECT SUM(JobFiles) as stored_files, EndTime FROM Job ";
703                         $query .= "WHERE ( EndTime BETWEEN '$start_date' AND '$end_date' ) AND ";
704                         $query .= "Name = '$jobname'";
705                         
706                         $result = $this->db_link->query( $query );
707                         
708                         if( PEAR::isError( $result ) ) {
709                                 die( "Unable to get Job Files from catalog" );
710                         }else{
711                                 $stored_bytes = 0;
712                                 $tmp = $result->fetchRow( DB_FETCHMODE_ASSOC );
713                                 
714                                 $day                    = date( "D d", strtotime($end_date) );
715                                 $stored_files   = $tmp['stored_files'];
716                                 
717                                 return array( $day, $stored_files );
718                         }                       
719                 }
720 } // end class Bweb
721 ?>