added humanly readable unit (b k M G)
[BackupPC.git] / lib / BackupPC / Search.pm
index a1bd9a7..888a853 100644 (file)
@@ -12,16 +12,20 @@ use XML::Writer;
 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 $dsn = $Conf{SearchDSN};
-my $db_user = $Conf{SearchUser} || '';
+my $dbh;
 
-sub search_module {
+my $bpc = BackupPC::Lib->new || die;
+$bpc->ConfigRead('_search_archive');
+my %Conf = $bpc->Conf();
 
-       my $bpc = BackupPC::Lib->new || die;
-       my %Conf = $bpc->Conf();
+sub search_module {
 
        my $search_module = $Conf{SearchModule} || die "search is disabled";
        eval "use $search_module";
@@ -37,7 +41,7 @@ sub search_module {
 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;
 }
 
@@ -373,7 +377,7 @@ sub getGzipSize($$)
 sub getVolumes($) {
        my $id = shift;
 
-       my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
+       my $max_archive_size = $Conf{ArchiveMediaSize} || die "no ArchiveMediaSize";
 
        my $sth = $dbh->prepare(qq{
                select
@@ -449,7 +453,7 @@ print STDERR "## sort=". ($param->{'sort'} || 'no sort param') . " burn sql orde
                $row->{'age'} = sprintf("%0.1f", ( $row->{'age'} / 86400 ) );
                #$row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );
 
-               my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
+               my $max_archive_size = $Conf{ArchiveMediaSize} || die "no ArchiveMediaSize";
                if ($row->{size} > $max_archive_size) {
                        ($row->{volumes}, $row->{inc_size_calc}) = getVolumes($row->{id});
                }
@@ -469,8 +473,8 @@ sub displayBackupsGrid($) {
 
        my $param = shift;
 
-       my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
-       my $max_archive_file_size = $Conf{MaxArchiveFileSize}  || die "no MaxFileInSize";
+       my $max_archive_size = $Conf{ArchiveMediaSize} || die "no ArchiveMediaSize";
+       my $max_archive_file_size = $Conf{ArchiveChunkSize}  || die "no MaxFileInSize";
 
        my $retHTML .= q{
                <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">
@@ -1118,4 +1122,24 @@ sub displayGrid($) {
        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;