]> git.sur5r.net Git - bacula/bacula/commitdiff
bacula-web: Improved human_file_size function in bweb.inc.php
authorDavide Franco <bacula-dev@dflc.ch>
Tue, 14 Dec 2010 11:17:22 +0000 (12:17 +0100)
committerEric Bollengier <eric@eb.homelinux.org>
Thu, 3 Mar 2011 09:39:08 +0000 (10:39 +0100)
 - New argument unit (auto by default)
 - Modified unit argument to GB in GetStoredBytesByInterval function
 - Fixed Last 7 days stored bytes graph in main dashboard (all days in GB now)

gui/bacula-web/bweb.inc.php

index cab8ed40f7dd4cd60e7230efc1ccf0cb8bd44a58..95b631ab40113b1bc96217d240155aeb6452220d 100644 (file)
@@ -180,26 +180,39 @@ class Bweb extends DB {
                 $this->EndDate=$EndDateYear."-".$EndDateMonth."-".$EndDateDay." 23:59:59";  // last day full
                 
         }//end function
-               function human_file_size( $size, $decimal = 2 )
+               
+               // Return humanized size with default unit of GB
+               // if auto provide for unit argument, automaticaly decide which unit
+               function human_file_size( $size, $decimal = 2, $unit = 'auto' )
                {
                        $unit_id = 0;
                        $lisible = false;
                        $units = array('B','KB','MB','GB','TB');
                        $hsize = $size;
+
+                       switch( $unit )
+                       {
+                               case 'auto';
+                                       while( !$lisible ) {
+                                               if ( $hsize >= 1024 ) {
+                                                       $hsize    = $hsize / 1024;
+                                                       $unit_id += 1;
+                                               }        
+                                               else {
+                                                       $lisible = true;
+                                               } 
+                                       }
+                               break;
                                
-                       while( !$lisible ) {
-                               if ( $hsize >= 1024 ) {
-                                       $hsize    = $hsize / 1024;
-                                       $unit_id += 1;
-                               } 
-                               else {
-                                       $lisible = true;
-                               } 
-                       } 
-                       // Format human size
+                               default:
+                                       $p = array_search( $unit, $units);
+                                       $hsize = $hsize / pow(1024,$p);
+                               break;
+                       } // end switch
+                       
                        $hsize = sprintf("%." . $decimal . "f", $hsize);
-                       return $hsize . ' ' . $units[$unit_id];
+                       $hsize = $hsize . ' ' . $units[$unit_id];
+                       return $hsize;
                } // end function
 
                
@@ -599,7 +612,7 @@ class Bweb extends DB {
                                $day = date( "d/m", strtotime($end_date) );
                                
                                if( isset( $tmp['stored_bytes'] ) ) {
-                                       $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3);
+                                       $hbytes = $this->human_file_size( $tmp['stored_bytes'], 3, 'GB');
                                        $hbytes = explode( " ", $hbytes );
                                        $stored_bytes = $hbytes[0];
                                }