--- trunk/lib/BackupPC/SearchLib.pm 2005/06/23 12:36:22 9 +++ trunk/lib/BackupPC/SearchLib.pm 2005/09/19 11:07:31 123 @@ -4,253 +4,347 @@ use strict; use BackupPC::CGI::Lib qw(:all); use BackupPC::Attrib qw(:all); -use Data::Dumper; use DBI; +use DateTime; +use vars qw(%In $MyURL); +use Time::HiRes qw/time/; + +my $on_page = 100; +my $pager_pages = 10; + +my $dsn = $Conf{SearchDSN}; +my $db_user = $Conf{SearchUser} || ''; + +my $hest_index_path = $Conf{HyperEstraierIndex}; + +my $dbh; + +sub get_dbh { + $dbh ||= DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + return $dbh; +} sub getUnits() { - my @ret = (); - my $tmp; - my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", - "", "", { RaiseError => 1, AutoCommit => 1 } ); - my $st = - $dbh->prepare( - " SELECT shares.ID AS ID, shares.share AS name FROM shares;"); - $st->execute(); - push (@ret, { 'ID' => '', 'name' => '-'}); - while ( $tmp = $st->fetchrow_hashref() ) { - push( @ret, { 'ID' => $tmp->{'ID'}, 'name' => $tmp->{'name'} } ); - } - $dbh->disconnect(); - return @ret; + my @ret; + + my $dbh = get_dbh(); + my $sth = $dbh->prepare(qq{ + SELECT + shares.id as id, + hosts.name || ':' || shares.name as share + FROM shares + JOIN hosts on hostid = hosts.id + ORDER BY share + } ); + $sth->execute(); + push @ret, { 'id' => '', 'share' => '-'}; # dummy any + + while ( my $row = $sth->fetchrow_hashref() ) { + push @ret, $row; + } + return @ret; } +sub epoch_to_iso { + my $t = shift || return; + my $iso = BackupPC::Lib::timeStamp(undef, $t); + $iso =~ s/\s/ /g; + return $iso; +} + +sub dates_from_form($) { + my $param = shift || return; + + sub mk_epoch_date($$) { + my ($name,$suffix) = @_; + + my $yyyy = $param->{ $name . '_year_' . $suffix} || return undef; + my $mm .= $param->{ $name . '_month_' . $suffix} || + ( $suffix eq 'from' ? 1 : 12); + my $dd .= $param->{ $name . '_day_' . $suffix} || + ( $suffix eq 'from' ? 1 : 31); + + $yyyy =~ s/\D//g; + $mm =~ s/\D//g; + $dd =~ s/\D//g; + + my $dt = new DateTime( + year => $yyyy, + month => $mm, + day => $dd + ); + print STDERR "mk_epoch_date($name,$suffix) [$yyyy-$mm-$dd] = " . $dt->ymd . " " . $dt->hms . "\n"; + return $dt->epoch || 'NULL'; + } + + my @ret = ( + mk_epoch_date('search_backup', 'from'), + mk_epoch_date('search_backup', 'to'), + mk_epoch_date('search', 'from'), + mk_epoch_date('search', 'to'), + ); + + return @ret; + +} + + sub getWhere($) { - my ($param) = @_; - my $retSQL = ""; - my @conditions = (); - my $cond; + my $param = shift || return; - - - - if ( defined( $param->{'search_backup_day_from'} ) && $param->{'search_backup_day_from'} ne "") { - push( @conditions, - ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) >= "' - . $param->{'search_backup_day_from'} ."\""); - } - if ( defined( $param->{'search_backup_day_to'} ) && $param->{'search_backup_day_to'} ne "") { - push( @conditions, - ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) <= "' - . $param->{'search_backup_day_from'} ."\""); - } - if ( defined( $param->{'search_backup_month_from'} ) && $param->{'search_backup_month_from'} ne "") { - push( @conditions, - ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) >= "' - . $param->{'search_backup_month_from'} ."\""); - } - if ( defined( $param->{'search_backup_month_to'} ) && $param->{'search_backup_month_to'} ne "") { - push( @conditions, - ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) <= "' - . $param->{'search_backup_month_to'} ."\""); - } - if ( defined( $param->{'search_backup_year_from'} ) && $param->{'search_backup_year_from'} ne "") { - push( @conditions, - ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) >= "' - . $param->{'search_backup_year_from'} ."\""); - } - if ( defined( $param->{'search_backup_year_to'} ) && $param->{'search_backup_year_to'} ne "") { - push( @conditions, - ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) <= "' - . $param->{'search_backup_year_to'} ."\""); - } + my ($backup_from, $backup_to, $files_from, $files_to) = dates_from_form($param); - if ( defined( $param->{'search_day_from'} ) && $param->{'search_day_from'} ne "" ) { - push( @conditions, - ' strftime("%d", datetime(files.date, "unixepoch","localtime")) >= "' - . $param->{'search_day_from'} ."\""); - } - if ( defined( $param->{'search_month_from'} ) && $param->{'search_month_from'} ne "") { - push( @conditions, - ' strftime("%m", datetime(files.date, "unixepoch","localtime")) >= "' - . $param->{'search_month_from'} ."\""); - } - if ( defined( $param->{'search_year_from'} ) && $param->{'search_year_from'} ne "") { - push( @conditions, - ' strftime("%Y", datetime(files.date, "unixepoch","localtime")) >= "' - . $param->{'search_year_from'} ."\""); - } - if ( defined( $param->{'search_day_to'} ) && $param->{'search_day_to'} ne "" ) { - push( @conditions, - ' strftime("%d", datetime(files.date, "unixepoch","localtime")) <= "' - . $param->{'search_day_to'} ."\""); - } - if ( defined( $param->{'search_month_to'} ) && $param->{'search_month_to'} ne "" ) { - push( @conditions, - ' strftime("%m", datetime(files.date, "unixepoch","localtime")) <= "' - . $param->{'search_month_to'} ."\"" ); - } - if ( defined( $param->{'search_year_to'} )&& $param->{'search_year_to'} ne "" ) { - push( @conditions, - ' strftime("%Y", datetime(files.date, "unixepoch","localtime")) <= "' - . $param->{'search_year_to'} ."\""); - } + my @conditions; + push @conditions, qq{ backups.date >= $backup_from } if ($backup_from); + push @conditions, qq{ backups.date <= $backup_to } if ($backup_to); + push @conditions, qq{ files.date >= $files_from } if ($files_from); + push @conditions, qq{ files.date <= $files_to } if ($files_to); - if ( defined( $param->{'search_host'} ) && $param->{'search_host'} ne "") { - push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ); - } + print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:" . join(" | ",@conditions); - if ( defined ($param->{'search_filename'}) && $param->{'search_filename'} ne "") { - push (@conditions, " files.name LIKE '".$param->{'search_filename'}."%'"); - } - - $retSQL = ""; - foreach $cond(@conditions) - { - if ($retSQL ne "") - { - $retSQL .= " AND "; - } - $retSQL .= $cond; - } + 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'}); - - return $retSQL; + return join(" and ", @conditions); } -sub getFiles($$) - { - my ($where, $offset) = @_; - - - my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", - "", "", { RaiseError => 1, AutoCommit => 1 } ); - my $sql = - q{ - SELECT files.id AS fid, - hosts.name AS hname, - shares.name AS sname, - shares.share AS sharename, - backups.num AS backupNum, - files.name AS filename, - files.path AS filepath, - shares.share||files.fullpath AS networkPath, - date(files.date, 'unixepoch', 'localtime') AS date, - files.type AS filetype, - files.size AS size, - dvds.name AS dvd - FROM - files - INNER JOIN shares ON files.shareID=shares.ID - INNER JOIN hosts ON hosts.ID = shares.hostID - INNER JOIN backups ON backups.hostID = hosts.ID - LEFT JOIN dvds ON dvds.ID = files.dvdid - - }; - if (defined($where) && $where ne "") - { - $sql .= " WHERE ". $where; - } +sub getFiles($) { + my ($param) = @_; + + my $offset = $param->{'offset'} || 0; + $offset *= $on_page; + + my $dbh = get_dbh(); - $sql .= - q{ - ORDER BY files.id - LIMIT 100 - OFFSET ? * 100 + 1 + my $sql_cols = qq{ + files.id AS fid, + hosts.name AS hname, + shares.name AS sname, + files.backupnum AS backupnum, + files.path AS filepath, + files.date AS date, + files.type AS type, + files.size AS size }; - - - - my $st = $dbh->prepare( - $sql - ); - if (!defined($offset) && $offset ne "") - { - $st->bind_param(1, $offset); - } - else - { - $st->bind_param(1,0); - } - $st->execute; - - my @ret = (); - my $tmp; - - while ($tmp = $st->fetchrow_hashref()) - { - push(@ret, { - 'hname' => $tmp->{'hname'}, - 'sname' => $tmp->{'sname'}, - 'sharename' => $tmp->{'sharename'}, - 'backupno' => $tmp->{'backupNum'}, - 'fname' => $tmp->{'filename'}, - 'fpath' => $tmp->{'filepath'}, - 'networkpath' => $tmp->{'networkPath'}, - 'date' => $tmp->{'date'}, - 'type' => $tmp->{'filetype'}, - 'size' => $tmp->{'size'}, - 'id' => $tmp->{'fid'}, - 'dvd' => $tmp->{'dvd'} - } - ); - + + my $sql_from = qq{ + 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 = files.shareID + }; + + my $sql_where; + my $where = getWhere($param); + $sql_where = " WHERE ". $where if ($where); + + my $sql_order = qq{ + ORDER BY files.date + LIMIT $on_page + OFFSET ? + }; + + my $sql_count = qq{ select count(files.id) $sql_from $sql_where }; + my $sql_results = qq{ select $sql_cols $sql_from $sql_where $sql_order }; + + my $sth = $dbh->prepare($sql_count); + $sth->execute(); + my ($results) = $sth->fetchrow_array(); + + $sth = $dbh->prepare($sql_results); + $sth->execute( $offset ); + + if ($sth->rows != $results) { + my $bug = "$0 BUG: [[ $sql_count ]] = $results while [[ $sql_results ]] = " . $sth->rows; + $bug =~ s/\s+/ /gs; + print STDERR "$bug\n"; } + + my @ret; - $st->finish(); - $dbh->disconnect(); - return @ret; - } + while (my $row = $sth->fetchrow_hashref()) { + push @ret, $row; + } + + $sth->finish(); + return ($results, \@ret); +} -sub getBackupsNotBurned() - { - my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", - "", "", { RaiseError => 1, AutoCommit => 1 } ); - my $sql = q{ - SELECT - hosts.ID AS hostID, - hosts.name AS host, - backups.num AS backupno, - backups.type AS type, - backups.date AS date - FROM backups, shares, files, hosts - WHERE - backups.num = files.backupNum AND - shares.ID = files.shareID AND - backups.hostID = shares.hostID AND - hosts.ID = backups.hostID AND - files.dvdid IS NULL - GROUP BY - backups.hostID, backups.num - }; - my $st = $dbh -> prepare( $sql ); - my @ret = (); - $st -> execute(); - - while ( my $tmp = $st -> fetchrow_hashref() ) - { - push(@ret, { - 'host' => $tmp->{'host'}, - 'hostid' => $tmp->{'hostID'}, - 'backupno' => $tmp->{'backupno'}, - 'type' => $tmp->{'type'}, - 'date' => $tmp->{'date'} - } - ); +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 ($hest_index_path); + + use HyperEstraier; + + my ($index_path, $index_node_url) = getHyperEstraier_url($hest_index_path); + + # open the database + 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(); + + my $q = $param->{'search_filename'}; + my $shareid = $param->{'search_share'}; + + 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); + } + + my ($backup_from, $backup_to, $files_from, $files_to) = dates_from_form($param); + + $cond->add_attr("backup_date NUMGE $backup_from") if ($backup_from); + $cond->add_attr("backup_date NUMLE $backup_to") if ($backup_to); + + $cond->add_attr("date NUMGE $files_from") if ($files_from); + $cond->add_attr("date NUMLE $files_to") if ($files_to); + + $cond->add_attr("shareid NUMEQ $shareid") if ($shareid); + +# $cond->set_max( $offset + $on_page ); + $cond->set_options( $HyperEstraier::Condition::SURE ); + $cond->set_order( 'date NUMA' ); + + # get the result of search + my @res; + 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 $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/) { + $row->{$c} = $doc->attr($c); + } + push @res, $row; + } + + 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; + + return $ret; + +} + +sub getBackupsNotBurned() { + + my $dbh = get_dbh(); + + my $sql = q{ + SELECT + backups.hostID AS hostID, + hosts.name AS host, + shares.name AS share, + backups.id AS backupnum, + backups.type AS type, + backups.date AS date, + backups.size AS 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 AND archive_backup.backup_id IS NULL + WHERE backups.size > 0 + GROUP BY + backups.hostID, + hosts.name, + shares.name, + backups.num, + backups.shareid, + backups.id, + backups.type, + backups.date, + backups.size + ORDER BY backups.date + }; + my $sth = $dbh->prepare( $sql ); + my @ret; + $sth->execute(); + + while ( my $row = $sth->fetchrow_hashref() ) { + $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ); + $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024); + my (undef,undef,undef,undef,undef,undef,undef,$fs_size,undef,undef,undef,undef,undef) = + stat( $Conf{InstallDir}.'/'.$Conf{GzipTempDir}.'/'. + getGzipName($row->{'host'}, $row->{share}, $row->{'backupnum'})); + $row->{'fs_size'} = $fs_size; + push @ret, $row; } - return @ret; - } + return @ret; +} sub displayBackupsGrid() { my $retHTML = ""; - my $addForm = 1; - if ($addForm) - { - - $retHTML .= < EOF3 - $retHTML .= q{
}; - $retHTML .= q{}; - } - $retHTML .= ""; - $retHTML .= " "; - if ($addForm) - { - $retHTML .= ""; - } - $retHTML .= ""; - my @backups = getBackupsNotBurned(); - my $backup; - - if ($addForm) - { - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - - } - foreach $backup(@backups) - { - my $ftype = ""; + $retHTML .= q{ + + + +
Host Backup no Type date
"; - $retHTML .= ""; - $retHTML .= "
+ + + + + + + + + + + + + }; + + 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 .= ''; - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - } - $retHTML .= "
+ + ShareBackup noTypedateage/dayssize/MBgzip size
+ +
{'hostid'}."_".$backup->{'backupno'} - ."\" value=\"".$backup->{'hostid'}."_".$backup->{'backupno'}."\"> " . $backup->{'host'} . "" . $backup->{'backupno'} . "" . $backup->{'type'} . "" . $backup->{'date'} . ""; - $retHTML .= "
"; - if ($addForm) - { - $retHTML .= "
"; - } - - return $retHTML; - - - } + $retHTML .= + '' . $backup->{'host'} . ':' . $backup->{'share'} . '' . + '' . $backup->{'backupnum'} . '' . + '' . $backup->{'type'} . '' . + '' . epoch_to_iso( $backup->{'date'} ) . '' . + '' . $backup->{'age'} . '' . + '' . $backup->{'size'} . '' . + '' . $backup->{'fs_size'} . + '' . + "\n"; + -sub displayGrid($$$) - { - my ($where, $addForm, $offset) = @_; - my $retHTML = ""; - - if ($addForm) - { - $retHTML .= q{
}; - $retHTML .= q{}; - } - $retHTML .= ""; - $retHTML .= " "; - $retHTML .= ""; - my @files = getFiles($where, $offset); - my $file; - - foreach $file(@files) - { - my $ftype = ""; - - if ($file->{'type'} == BPC_FTYPE_DIR) - { - $ftype = "dir"; - } - else - { - $ftype = "file"; - } - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; } - $retHTML .= "
Host Name Type backup no. size date Media
" . $file->{'hname'} ."" . $file->{'fname'} . "" . $ftype . "" . $file->{'backupno'} . "" . $file->{'size'} . "" . $file->{'date'} . "" . $file->{'dvd'} . "
"; - + $retHTML .= ""; + $retHTML .= "total gzip size:
"; + $retHTML .= "Note:"; + $retHTML .= "
"; + + return $retHTML; +} + +sub displayGrid($) { + my ($param) = @_; + + my $offset = $param->{'offset'}; + my $hilite = $param->{'search_filename'}; + + my $retHTML = ""; + + my $start_t = time(); + + my ($results, $files); + if ($param->{'use_hest'} && length($hilite) > 0) { + ($results, $files) = getFilesHyperEstraier($param); + } else { + ($results, $files) = getFiles($param); + } - $retHTML .= ""; - for (my $ii = 1; $ii <= $#files; $ii++) - { - $retHTML .= "$ii"; - if ($ii < $#files) - { - $retHTML .= " | "; - } - } + my $dur_t = time() - $start_t; + my $dur = sprintf("%0.4fs", $dur_t); + my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page); - if ($addForm) - { - $retHTML .= ""; - } - - return $retHTML; - } + if ($results <= 0) { + $retHTML .= qq{ +

No results found...

+ }; + return $retHTML; + } else { + # DEBUG + #use Data::Dumper; + #$retHTML .= '
' . Dumper($files) . '
'; + } + + + $retHTML .= qq{ +
+ Found $results files showing $from - $to (took $dur) +
+ + + + + + + + + + + }; + + my $file; + + sub hilite_html($$) { + my ($html, $search) = @_; + $html =~ s#($search)#$1#gis; + return $html; + } + + sub restore_link($$$$$$) { + my $type = shift; + my $action = 'RestoreFile'; + $action = 'browse' if (lc($type) eq 'dir'); + return sprintf(qq{%s}, $action, @_); + } + + my $i = $offset * $on_page; + + foreach $file (@{ $files }) { + $i++; + + my $typeStr = BackupPC::Attrib::fileType2Text(undef, $file->{'type'}); + $retHTML .= qq{}; + + $retHTML .= qq{}; + + $retHTML .= + qq{} . + qq{} . + qq{} . + qq{} . + qq{} . + qq{}; + + $retHTML .= ""; + } + $retHTML .= "
ShareType and Name#SizeDateMedia
$i} . $file->{'hname'} . ':' . $file->{'sname'} . qq{$typeStr } . hilite_html( $file->{'filepath'}, $hilite ) . qq{} . restore_link( $typeStr, ${EscURI( $file->{'hname'} )}, $file->{'backupnum'}, ${EscURI( $file->{'sname'})}, ${EscURI( $file->{'filepath'} )}, $file->{'backupnum'} ) . qq{} . $file->{'size'} . qq{} . epoch_to_iso( $file->{'date'} ) . qq{} . '?' . qq{
"; + + # all variables which has to be transfered + foreach my $n (qw/search_day_from search_month_from search_year_from search_day_to search_month_to search_year_to search_backup_day_from search_backup_month_from search_backup_year_from search_backup_day_to search_backup_month_to search_backup_year_to search_filename offset/) { + $retHTML .= qq{\n}; + } + + my $del = ''; + my $max_page = int( $results / $on_page ); + my $page = 0; + + sub page_link($$$) { + my ($param,$page,$display) = @_; + + $param->{'offset'} = $page; + + my $html = '' . $display . ''; + } + + $retHTML .= '
'; + + if ($offset > 0) { + $retHTML .= page_link($param, $offset - 1, '<<') . ' '; + } + + while ($page <= $max_page) { + if ($page == $offset) { + $retHTML .= $del . '' . ($page + 1) . ''; + } else { + $retHTML .= $del . page_link($param, $page, $page + 1); + } + + if ($page < $offset - $pager_pages && $page != 0) { + $retHTML .= " ... "; + $page = $offset - $pager_pages; + $del = ''; + } elsif ($page > $offset + $pager_pages && $page != $max_page) { + $retHTML .= " ... "; + $page = $max_page; + $del = ''; + } else { + $del = ' | '; + $page++; + } + } + + if ($offset < $max_page) { + $retHTML .= ' ' . page_link($param, $offset + 1, '>>'); + } + + $retHTML .= "
"; + + return $retHTML; +} 1;