--- trunk/lib/BackupPC/SearchLib.pm 2005/08/21 15:29:24 59 +++ trunk/lib/BackupPC/SearchLib.pm 2005/08/28 12:35:59 86 @@ -15,31 +15,49 @@ 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 $dbh; + +sub get_dbh { + $dbh ||= DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + return $dbh; +} + sub getUnits() { my @ret; - my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); - my $sth = $dbh->prepare(qq{ SELECT id, share FROM shares} ); + 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; } - $dbh->disconnect(); return @ret; } sub epoch_to_iso { my $t = shift || return; - my $dt = DateTime->from_epoch( epoch => $t ) || return; - print STDERR "BUG: $t != " . $dt->epoch . "\n" unless ($t == $dt->epoch); - return $dt->ymd . ' ' . $dt->hms; + my $iso = BackupPC::Lib::timeStamp(undef, $t); + $iso =~ s/\s/ /g; + return $iso; } -sub getWhere($) { - my ($param) = @_; - my @conditions; +sub dates_from_form($) { + my $param = shift || return; sub mk_epoch_date($$) { my ($name,$suffix) = @_; @@ -57,66 +75,72 @@ return $dt->epoch || 'NULL'; } - my $backup_from = mk_epoch_date('search_backup', 'from'); + return ( + mk_epoch_date('search_backup', 'from'), + mk_epoch_date('search_backup', 'to'), + mk_epoch_date('search', 'from'), + mk_epoch_date('search', 'to'), + ); +} + + +sub getWhere($) { + my $param = shift || return; + + my ($backup_from, $backup_to, $files_from, $files_to) = dates_from_form($param); + + my @conditions; push @conditions, qq{ backups.date >= $backup_from } if ($backup_from); - my $backup_to = mk_epoch_date('search_backup', 'to'); push @conditions, qq{ backups.date <= $backup_to } if ($backup_to); - - my $files_from = mk_epoch_date('search', 'from'); push @conditions, qq{ files.date >= $files_from } if ($files_from); - my $files_to = mk_epoch_date('search', 'to'); 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); - - push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'}); - push (@conditions, " upper(files.name) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'}); + 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 ( - join(" and ", @conditions), - $files_from, $files_to, - $backup_from, $backup_to - ); + return join(" and ", @conditions); } sub getFiles($$) { - my ($where, $offset) = @_; + my ($param, $offset) = @_; - my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + my $dbh = get_dbh(); my $sql_cols = qq{ files.id AS fid, hosts.name AS hname, shares.name AS sname, - shares.share AS sharename, - files.backupNum AS backupNum, - files.name AS filename, + -- shares.share AS sharename, + files.backupnum AS backupnum, + -- files.name AS filename, files.path AS filepath, - shares.share||files.fullpath AS networkPath, files.date AS date, - files.type AS filetype, + files.type AS type, files.size AS size, - dvds.name AS dvd + -- dvds.name AS dvd + null AS 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 + INNER JOIN backups ON backups.num = files.backupnum and backups.hostID = hosts.ID AND backups.shareID = shares.ID }; my $sql_dvd_from = qq{ - LEFT JOIN dvds ON dvds.ID = files.dvdid + -- LEFT JOIN dvds ON dvds.ID = files.dvdid }; my $sql_where; + my $where = getWhere($param); $sql_where = " WHERE ". $where if ($where); my $sql_order = qq{ - ORDER BY files.id + ORDER BY files.date LIMIT $on_page OFFSET ? }; @@ -143,62 +167,105 @@ my @ret; while (my $row = $sth->fetchrow_hashref()) { - push(@ret, { - 'hname' => $row->{'hname'}, - 'sname' => $row->{'sname'}, - 'sharename' => $row->{'sharename'}, - 'backupno' => $row->{'backupnum'}, - 'fname' => $row->{'filename'}, - 'fpath' => $row->{'filepath'}, - 'networkpath' => $row->{'networkpath'}, - 'date' => $row->{'date'}, - 'type' => $row->{'filetype'}, - 'size' => $row->{'size'}, - 'id' => $row->{'fid'}, - 'dvd' => $row->{'dvd'} - }); + push @ret, $row; } $sth->finish(); - $dbh->disconnect(); return ($results, \@ret); } +sub getFilesHyperEstraier($$) { + my ($param, $offset) = @_; + + die "no index_path?" unless ($index_path); + + use HyperEstraier; + + # open the database + my $db = HyperEstraier::Database->new(); + $db->open($index_path, $HyperEstraier::ESTDBREADER); + + # create a search condition object + my $cond = HyperEstraier::Condition->new(); + + my $q = $param->{'search_filename'}; + my $shareid = $param->{'search_share'}; + + if ($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); + } + + $offset ||= 0; + $offset = ($offset * $on_page); + +# $cond->set_max( $offset + $on_page ); + $cond->set_options( $HyperEstraier::Condition::SURE ); + $cond->set_order( 'date NUMA' ); + + # get the result of search + my $result = $db->search($cond, 0); + $result->get(0); + + my @res; + my $hits = $result->size; + + # for each document in result + for my $i ($offset .. $result->size-1) { + my $id = $result->get($i); + my $doc = $db->get_doc($id, 0); + + 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 getBackupsNotBurned() { - my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + my $dbh = get_dbh(); my $sql = q{ SELECT - hosts.ID AS hostid, + backups.hostID AS hostid, min(hosts.name) AS host, - backups.num AS backupno, + backups.num AS backupnum, min(backups.type) AS type, - min(backups.date) AS date - FROM backups, shares, files, hosts + 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 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, hosts.id + backups.hostID, backups.num ORDER BY min(backups.date) }; my $sth = $dbh->prepare( $sql ); my @ret; $sth->execute(); - while ( my $row = $sth->fetchrow_hashref() ) { - push(@ret, { - 'host' => $row->{'host'}, - 'hostid' => $row->{'hostid'}, - 'backupno' => $row->{'backupno'}, - 'type' => $row->{'type'}, - 'date' => $row->{'date'}, - 'age' => sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ), - } - ); + while ( my $row = $sth->fetchrow_hashref() ) { + $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ); + $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024); + push @ret, $row; } return @ret; @@ -236,17 +303,21 @@ $retHTML.= q{}; $retHTML .= q{}; } - $retHTML .= qq{
"; } $retHTML .= qq{ - | Host | -Backup no | -Type | -date | -age/days | +Host | +Backup no | +Type | +date | +age/days | +size/MB |
'; } $retHTML .= ' | ' . $backup->{'host'} . ' | ' . - '' . $backup->{'backupno'} . ' | ' . + '' . $backup->{'backupnum'} . ' | ' . '' . $backup->{'type'} . ' | ' . '' . epoch_to_iso( $backup->{'date'} ) . ' | ' . '' . $backup->{'age'} . ' | ' . + '' . $backup->{'size'} . ' | ' . '