use unit all over
[BackupPC.git] / lib / BackupPC / Search.pm
index c996568..f2dac44 100644 (file)
@@ -12,17 +12,21 @@ use XML::Writer;
 use IO::File;
 use Data::Dump qw(dump);
 
 use IO::File;
 use Data::Dump qw(dump);
 
+require Exporter;
+our @ISA=qw(Exporter);
+our @EXPORT=qw(unit);
+
 my $on_page = 100;
 my $pager_pages = 10;
 
 my $on_page = 100;
 my $pager_pages = 10;
 
-my $dsn = $Conf{SearchDSN};
-my $db_user = $Conf{SearchUser} || '';
+my $dbh;
+
+my $bpc = BackupPC::Lib->new || die;
+$bpc->ConfigRead('_search_archive');
+my %Conf = $bpc->Conf();
 
 sub search_module {
 
 
 sub search_module {
 
-       my $bpc = BackupPC::Lib->new || die;
-       my %Conf = $bpc->Conf();
-
        my $search_module = $Conf{SearchModule} || die "search is disabled";
        eval "use $search_module";
        if ( $@ ) {
        my $search_module = $Conf{SearchModule} || die "search is disabled";
        eval "use $search_module";
        if ( $@ ) {
@@ -37,7 +41,7 @@ sub search_module {
 my $dbh;
 
 sub get_dbh {
 my $dbh;
 
 sub get_dbh {
-       $dbh ||= DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
+       $dbh ||= DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 1 } );
        return $dbh;
 }
 
        return $dbh;
 }
 
@@ -454,8 +458,6 @@ print STDERR "## sort=". ($param->{'sort'} || 'no sort param') . " burn sql orde
                        ($row->{volumes}, $row->{inc_size_calc}) = getVolumes($row->{id});
                }
 
                        ($row->{volumes}, $row->{inc_size_calc}) = getVolumes($row->{id});
                }
 
-               $row->{size} = sprintf("%0.2f", $row->{size} / 1024 / 1024);
-
                # do some cluster calculation (approximate)
                $row->{inc_size} = int(( ($row->{inc_size} + 1023 ) / 2 )  * 2);
                $row->{inc_size_calc} ||= $row->{inc_size};
                # do some cluster calculation (approximate)
                $row->{inc_size} = int(( ($row->{inc_size} + 1023 ) / 2 )  * 2);
                $row->{inc_size_calc} ||= $row->{inc_size};
@@ -470,7 +472,7 @@ sub displayBackupsGrid($) {
        my $param = shift;
 
        my $max_archive_size = $Conf{ArchiveMediaSize} || die "no ArchiveMediaSize";
        my $param = shift;
 
        my $max_archive_size = $Conf{ArchiveMediaSize} || die "no ArchiveMediaSize";
-       my $max_archive_file_size = $Conf{MaxArchiveFileSize}  || die "no MaxFileInSize";
+       my $max_archive_file_size = $Conf{ArchiveChunkSize}  || die "no MaxFileInSize";
 
        my $retHTML .= q{
                <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">
 
        my $retHTML .= q{
                <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">
@@ -828,8 +830,8 @@ EOF3
        } .
                sort_header($param, 'Date', 'date', 'center') .
                sort_header($param, 'Age/days', 'age', 'center') .
        } .
                sort_header($param, 'Date', 'date', 'center') .
                sort_header($param, 'Age/days', 'age', 'center') .
-               sort_header($param, 'Size/Mb', 'size', 'center') .
-               sort_header($param, 'gzip size/Kb', 'incsize', 'center') .
+               sort_header($param, 'Size', 'size', 'center') .
+               sort_header($param, 'gzip size', 'incsize', 'center') .
        qq{
                        <td align="center">medias</td></tr>
        };
        qq{
                        <td align="center">medias</td></tr>
        };
@@ -867,8 +869,8 @@ EOF3
                        '<td align="center">' . $backup->{'type'} . '</td>' .
                        '<td align="center">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
                        '<td align="center">' . $backup->{'age'} . '</td>' .
                        '<td align="center">' . $backup->{'type'} . '</td>' .
                        '<td align="center">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
                        '<td align="center">' . $backup->{'age'} . '</td>' .
-                       '<td align="right">' . $backup->{'size'} . '</td>' .
-                       '<td align="right">' . sprintf("%0.1f", $backup->{'inc_size'} / 1024 ) .
+                       '<td align="right">' . unit($backup->{'size'}) . '</td>' .
+                       '<td align="right">' . unit($backup->{'inc_size'}) . '</td>' .
                        '<input type="hidden" id="fss'.$checkbox_key .'" value="'. $backup->{'inc_size_calc'} .'"></td>' .
                        '<input type="hidden" id="prt'.$checkbox_key .'" value="'. $backup->{'volumes'} .'"></td>' .
                        '<td align="left">' . ( qq{<img src="$img_url/icon-cd.gif" alt="media">} x $backup->{volumes} ) . '</td>' .
                        '<input type="hidden" id="fss'.$checkbox_key .'" value="'. $backup->{'inc_size_calc'} .'"></td>' .
                        '<input type="hidden" id="prt'.$checkbox_key .'" value="'. $backup->{'volumes'} .'"></td>' .
                        '<td align="left">' . ( qq{<img src="$img_url/icon-cd.gif" alt="media">} x $backup->{volumes} ) . '</td>' .
@@ -1118,4 +1120,24 @@ sub displayGrid($) {
        return $retHTML;
 }
 
        return $retHTML;
 }
 
+my @units = qw/b k M G/;
+sub unit {
+       my $v = shift;
+
+       my $o = 0;
+
+       while ( ( $v / 10000 ) >= 1 ) {
+               $o++;
+               $v /= 1024;
+       }
+
+       if ( $v >= 1 ) {
+               return sprintf("%d%s", $v, $units[$o]);
+       } elsif ( $v == 0 ) {
+               return 0;
+       } else {
+               return sprintf("%.1f%s", $v, $units[$o]);
+       }
+}
+
 1;
 1;