--- trunk/lib/BackupPC/SearchLib.pm 2005/08/28 14:05:08 87 +++ trunk/lib/BackupPC/SearchLib.pm 2005/10/12 12:53:27 186 @@ -15,11 +15,7 @@ my $dsn = $Conf{SearchDSN}; my $db_user = $Conf{SearchUser} || ''; -my $index_path = $Conf{HyperEstraierIndex}; -if ($index_path) { - $index_path = $TopDir . '/' . $index_path; - $index_path =~ s#//#/#g; -} +my $hest_index_path = $Conf{HyperEstraierIndex}; my $dbh; @@ -72,10 +68,20 @@ $mm =~ s/\D//g; $dd =~ s/\D//g; + my $h = my $m = my $s = 0; + if ($suffix eq 'to') { + $h = 23; + $m = 59; + $s = 59; + } + my $dt = new DateTime( year => $yyyy, month => $mm, - day => $dd + day => $dd, + hour => $h, + minute => $m, + second => $s, ); print STDERR "mk_epoch_date($name,$suffix) [$yyyy-$mm-$dd] = " . $dt->ymd . " " . $dt->hms . "\n"; return $dt->epoch || 'NULL'; @@ -104,7 +110,7 @@ push @conditions, qq{ files.date >= $files_from } if ($files_from); push @conditions, qq{ files.date <= $files_to } if ($files_to); - print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:" . join(" | ",@conditions); + print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:" . join(" and ",@conditions); push( @conditions, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'}); push (@conditions, " upper(files.path) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'}); @@ -175,19 +181,46 @@ return ($results, \@ret); } +sub getHyperEstraier_url($) { + my ($use_hest) = @_; + + return unless $use_hest; + + use HyperEstraier; + my ($index_path, $index_node_url); + + if ($use_hest =~ m#^http://#) { + $index_node_url = $use_hest; + } else { + $index_path = $TopDir . '/' . $index_path; + $index_path =~ s#//#/#g; + } + return ($index_path, $index_node_url); +} + sub getFilesHyperEstraier($) { my ($param) = @_; my $offset = $param->{'offset'} || 0; $offset *= $on_page; - die "no index_path?" unless ($index_path); + die "no index_path?" unless ($hest_index_path); use HyperEstraier; + my ($index_path, $index_node_url) = getHyperEstraier_url($hest_index_path); + # open the database - my $db = HyperEstraier::Database->new(); - $db->open($index_path, $HyperEstraier::ESTDBREADER); + my $db; + if ($index_path) { + $db = HyperEstraier::Database->new(); + $db->open($index_path, $HyperEstraier::ESTDBREADER); + } elsif ($index_node_url) { + $db ||= HyperEstraier::Node->new($index_node_url); + $db->set_auth('admin', 'admin'); + } else { + die "BUG: unimplemented"; + } # create a search condition object my $cond = HyperEstraier::Condition->new(); @@ -195,9 +228,11 @@ my $q = $param->{'search_filename'}; my $shareid = $param->{'search_share'}; - if ($q) { - $q =~ s/(.)/$1 /g; + if (length($q) > 0) { + # exact match + $cond->add_attr("filepath ISTRINC $q"); + $q =~ s/(.)/$1 /g; # set the search phrase to the search condition object $cond->set_phrase($q); } @@ -217,17 +252,32 @@ $cond->set_order( 'date NUMA' ); # get the result of search - my $result = $db->search($cond, 0); - my @res; - my $hits = $result->size; + my ($result, $hits); + + if ($index_path) { + $result = $db->search($cond, 0); + $hits = $result->size; + } elsif ($index_node_url) { + $result = $db->search($cond, 0); + $hits = $result->doc_num; + } else { + die "BUG: unimplemented"; + } # for each document in result for my $i ($offset .. ($offset + $on_page - 1)) { last if ($i >= $hits); - my $id = $result->get($i); - my $doc = $db->get_doc($id, 0); + my $doc; + if ($index_path) { + my $id = $result->get($i); + $doc = $db->get_doc($id, 0); + } elsif ($index_node_url) { + $doc = $result->get_doc($i); + } else { + die "BUG: unimplemented"; + } my $row; foreach my $c (qw/fid hname sname backupnum fiilename filepath date type size/) { @@ -239,24 +289,83 @@ return ($hits, \@res); } +sub getGzipName($$$) +{ + my ($host, $share, $backupnum) = @_; + my $ret = $Conf{GzipSchema}; + + $share =~ s/\//_/g; + $ret =~ s/\\h/$host/ge; + $ret =~ s/\\s/$share/ge; + $ret =~ s/\\n/$backupnum/ge; + + $ret =~ s/__+/_/g; + + return $ret; + +} + +sub getGzipSize($$) +{ + my ($hostID, $backupNum) = @_; + my $ret; + my $sql; + my $dbh = get_dbh(); + + $sql = q{ + SELECT hosts.name as host, + shares.name as share, + backups.num as backupnum + FROM hosts, backups, shares + WHERE shares.id=backups.shareid AND + hosts.id =backups.hostid AND + hosts.id=? AND + backups.num=? + }; + my $sth = $dbh->prepare($sql); + $sth->execute($hostID, $backupNum); + + my $row = $sth->fetchrow_hashref(); + + my (undef,undef,undef,undef,undef,undef,undef,$ret,undef,undef,undef,undef,undef) = + stat( $Conf{InstallDir}.'/'.$Conf{GzipTempDir}.'/'. + getGzipName($row->{'host'}, $row->{share}, $row->{'backupnum'})); + + return $ret; +} + sub getBackupsNotBurned() { my $dbh = get_dbh(); - my $sql = q{ - SELECT - backups.hostID AS hostid, - min(hosts.name) AS host, - backups.num AS backupnum, - min(backups.type) AS type, - min(backups.date) AS date, - min(backups.size) AS size - FROM files - INNER JOIN shares ON files.shareID=shares.ID - INNER JOIN hosts ON hosts.ID = shares.hostID - INNER JOIN backups ON backups.num = files.backupnum and backups.hostID = hosts.ID AND backups.shareID = shares.ID - GROUP BY - backups.hostID, backups.num - ORDER BY min(backups.date) + + my $sql = q{ + SELECT + backups.hostID AS hostID, + hosts.name AS host, + shares.name AS share, + backups.num AS backupnum, + backups.type AS type, + backups.date AS date, + backups.size AS size, + backups.id AS id, + backups.inc_size AS inc_size + FROM backups + INNER JOIN shares ON backups.shareID=shares.ID + INNER JOIN hosts ON backups.hostID = hosts.ID + LEFT OUTER JOIN archive_backup ON archive_backup.backup_id = backups.id + WHERE backups.inc_size > 0 AND backups.inc_deleted is false AND archive_backup.backup_id IS NULL + GROUP BY + backups.hostID, + hosts.name, + shares.name, + backups.num, + backups.shareid, + backups.id, + backups.type, + backups.date, + backups.size, + backups.inc_size + ORDER BY backups.date }; my $sth = $dbh->prepare( $sql ); my @ret; @@ -265,99 +374,376 @@ while ( my $row = $sth->fetchrow_hashref() ) { $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ); $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024); + + # do some cluster calculation (approximate) and convert to kB + $row->{'inc_size'} = int(($row->{'inc_size'} + 1023 ) / ( 2 * 1024 ) * 2); push @ret, $row; } return @ret; } -sub displayBackupsGrid() - { - my $retHTML = ""; - my $addForm = 1; - - if ($addForm) { +sub displayBackupsGrid() { + + my $retHTML .= q{ +
+ }; - $retHTML .= < + $retHTML .= <<'EOF3'; + + +
+ +Size: kB + +
+
 
+
 
+
0%
+
+
+ +Note: + + + + +
+ +EOF3 + $retHTML .= q{ + + + + + + + + + + + + + + + }; + + my @color = (' bgcolor="#e0e0e0"', ''); + + my $i = 0; + my $host = ''; + + foreach my $backup ( getBackupsNotBurned() ) { + + if ($host ne $backup->{'host'}) { + $i++; + $host = $backup->{'host'}; + } my $ftype = ""; - - $retHTML .= ""; - if ($addForm) { - $retHTML .= ''; - } - - $retHTML .= '' . - '' . - '' . - '' . - '' . - '' . - ''; - } - $retHTML .= "
+ + ShareBackup noTypedateage/dayssize/MBgzip size/kB
' . $backup->{'host'} . '' . $backup->{'backupnum'} . '' . $backup->{'type'} . '' . epoch_to_iso( $backup->{'date'} ) . '' . $backup->{'age'} . '' . $backup->{'size'} . '
"; + my $checkbox_key = $backup->{'hostid'}. '_' .$backup->{'backupnum'} . '_' . $backup->{'id'}; + + $retHTML .= + ' + '; + + if (($backup->{'inc_size'} || 0) > 0) { + $retHTML .= ' + '; + } - if ($addForm) { - $retHTML .= ""; + $retHTML .= + '' . + '' . $backup->{'host'} . ':' . $backup->{'share'} . '' . + '' . $backup->{'backupnum'} . '' . + '' . $backup->{'type'} . '' . + '' . epoch_to_iso( $backup->{'date'} ) . '' . + '' . $backup->{'age'} . '' . + '' . $backup->{'size'} . '' . + '' . $backup->{'inc_size'} . + '' . + + "\n"; } + + $retHTML .= ""; + $retHTML .= ""; return $retHTML; } @@ -373,7 +759,7 @@ my $start_t = time(); my ($results, $files); - if ($param->{'use_hest'}) { + if ($param->{'use_hest'} && length($hilite) > 0) { ($results, $files) = getFilesHyperEstraier($param); } else { ($results, $files) = getFiles($param); @@ -435,7 +821,7 @@ my $typeStr = BackupPC::Attrib::fileType2Text(undef, $file->{'type'}); $retHTML .= qq{}; - $retHTML .= qq{$i}; + $retHTML .= qq{$i}; $retHTML .= qq{} . $file->{'hname'} . ':' . $file->{'sname'} . qq{} .