--- trunk/lib/BackupPC/SearchLib.pm 2005/08/21 14:26:23 58 +++ trunk/lib/BackupPC/SearchLib.pm 2005/08/26 21:43:01 78 @@ -16,25 +16,25 @@ my $db_user = $Conf{SearchUser} || ''; sub getUnits() { - my @ret = (); - my $tmp; - my $dbh = DBI->connect($dsn, $db_user, "", { 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 = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + my $sth = $dbh->prepare(qq{ SELECT id, share FROM shares 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; + $t += 60 * 60 * +2; # FIXME add TZ my $dt = DateTime->from_epoch( epoch => $t ) || return; -print STDERR "$t == ",$dt->epoch,"\n"; + print STDERR "BUG: $t != " . $dt->epoch . "\n" unless ($t == $dt->epoch); return $dt->ymd . ' ' . $dt->hms; } @@ -68,11 +68,11 @@ 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); + 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, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'}); - push (@conditions, " upper(files.name) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'}); + push (@conditions, " upper(files.path) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'}); return ( join(" and ", @conditions), @@ -95,13 +95,9 @@ 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.size AS size, - }; - - my $sql_dvd_cols = qq{ dvds.name AS dvd }; @@ -109,7 +105,7 @@ 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{ @@ -120,22 +116,30 @@ $sql_where = " WHERE ". $where if ($where); my $sql_order = qq{ - ORDER BY files.id - LIMIT $on_page - OFFSET ? + 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_dvd_from $sql_where $sql_order }; + $offset ||= 0; - $offset = ($offset * $on_page) + 1; + $offset = ($offset * $on_page); - my $sth = $dbh->prepare(qq{ select count(files.id) $sql_from $sql_where }); + my $sth = $dbh->prepare($sql_count); $sth->execute(); - my ($results) = $sth->fetchrow_array(); - $sth = $dbh->prepare(qq{ select $sql_cols $sql_dvd_cols $sql_from $sql_dvd_from $sql_where $sql_order }); + $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; while (my $row = $sth->fetchrow_hashref()) { @@ -154,7 +158,7 @@ 'dvd' => $row->{'dvd'} }); } - + $sth->finish(); $dbh->disconnect(); return ($results, \@ret); @@ -165,36 +169,28 @@ my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); my $sql = q{ SELECT - hosts.ID AS hostid, + backups.hostID AS hostid, min(hosts.name) AS host, backups.num AS backupno, 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 backups + INNER JOIN hosts ON hosts.ID = backups.hostID 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; @@ -243,6 +239,7 @@ Type date age/days + size/MB }; @@ -274,6 +271,7 @@ '' . $backup->{'type'} . '' . '' . epoch_to_iso( $backup->{'date'} ) . '' . '' . $backup->{'age'} . '' . + '' . $backup->{'size'} . '' . ''; } @@ -290,12 +288,6 @@ my ($where, $addForm, $offset, $hilite) = @_; my $retHTML = ""; - if ($addForm) { - $retHTML .= qq{
}; - $retHTML.= qq{}; - $retHTML .= qq{}; - } - my $start_t = time(); my ($results, $files) = getFiles($where, $offset); @@ -305,6 +297,25 @@ my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page); + if ($results <= 0) { + $retHTML .= qq{ +

No results found...

+ }; + return $retHTML; + } else { + # DEBUG + #use Data::Dumper; + #$retHTML .= '
' . Dumper($files) . '
'; + } + + + if ($addForm) { + $retHTML .= qq{}; + $retHTML.= qq{}; + $retHTML .= qq{}; + } + + $retHTML .= qq{
Found $results files showing $from - $to (took $dur) @@ -342,7 +353,7 @@ $file->{'sharename'}, qq{ } . hilite_html( $file->{'fpath'}, $hilite ), $typeStr, - restore_link( $typeStr, $file->{'hname'}, $file->{'backupno'}, $file->{'sname'}, $file->{'fpath'}, $file->{'backupno'} ), + restore_link( $typeStr, ${EscURI( $file->{'hname'} )}, $file->{'backupno'}, ${EscURI( $file->{'sname'})}, ${EscURI( $file->{'fpath'} )}, $file->{'backupno'} ), $file->{'size'}, epoch_to_iso( $file->{'date'} ), $file->{'dvd'}