--- trunk/lib/BackupPC/SearchLib.pm 2005/07/29 17:30:25 26 +++ trunk/lib/BackupPC/SearchLib.pm 2005/08/21 15:29:24 59 @@ -5,29 +5,43 @@ use BackupPC::CGI::Lib qw(:all); use BackupPC::Attrib qw(:all); 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} || ''; 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 = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + my $sth = $dbh->prepare(qq{ SELECT id, share FROM shares} ); + $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; } sub getWhere($) { my ($param) = @_; my @conditions; - sub mk_iso_date($$) { + sub mk_epoch_date($$) { my ($name,$suffix) = @_; my $yyyy = $param->{ $name . '_year_' . $suffix} || return; @@ -35,20 +49,25 @@ ( $suffix eq 'from' ? 1 : 12); my $dd .= $param->{ $name . '_day_' . $suffix} || ( $suffix eq 'from' ? 1 : 31); - return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd); - } - - my $backup_from = mk_iso_date('search_backup', 'from'); - push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from); - my $backup_to = mk_iso_date('search_backup', 'to'); - push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to); - - my $files_from = mk_iso_date('search', 'from'); - push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from); - my $files_to = mk_iso_date('search', 'to'); - push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to); + my $dt = new DateTime( + year => $yyyy, + month => $mm, + day => $dd + ); + return $dt->epoch || 'NULL'; + } + + my $backup_from = mk_epoch_date('search_backup', 'from'); + 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); + 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'}); @@ -62,136 +81,135 @@ } -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, - files.backupNum 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 +sub getFiles($$) { + my ($where, $offset) = @_; + + my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + + 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, + files.path AS filepath, + shares.share||files.fullpath AS networkPath, + files.date AS date, + files.type AS filetype, + files.size AS size, + dvds.name 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 + }; + + my $sql_dvd_from = qq{ LEFT JOIN dvds ON dvds.ID = files.dvdid - }; + }; - if (defined($where) && $where ne "") - { - $sql .= " WHERE ". $where; - } + my $sql_where; + $sql_where = " WHERE ". $where if ($where); - $sql .= - q{ - ORDER BY files.id - LIMIT 100 - OFFSET ? * 100 + 1 + my $sql_order = qq{ + ORDER BY files.id + LIMIT $on_page + OFFSET ? }; - - - - 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'} - } - ); - - } - - $st->finish(); - $dbh->disconnect(); - return @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'} + 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); + + 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; + + 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'} + }); + } + + $sth->finish(); + $dbh->disconnect(); + return ($results, \@ret); +} + +sub getBackupsNotBurned() { + + my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); + my $sql = q{ + SELECT + hosts.ID 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 + 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 + 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 ) ), } - ); + ); } - return @ret; - } + return @ret; +} sub displayBackupsGrid() { my $retHTML = ""; my $addForm = 1; - if ($addForm) - { + if ($addForm) { $retHTML .= < @@ -214,68 +232,98 @@ //--> EOF3 - $retHTML .= q{
}; $retHTML .= q{}; } - $retHTML .= ""; - $retHTML .= " "; - if ($addForm) - { + $retHTML .= qq{
}; + + if ($addForm) { $retHTML .= ""; } - $retHTML .= ""; - my @backups = getBackupsNotBurned(); - my $backup; - - if ($addForm) - { - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - + $retHTML .= qq{ + + + + + + + }; + + my @backups = getBackupsNotBurned(); + my $backup; + + if ($addForm) { + $retHTML .= qq{ + + }; } - foreach $backup(@backups) - { - my $ftype = ""; + + foreach $backup(@backups) { + + my $ftype = ""; - $retHTML .= ""; - if ($addForm) - { - $retHTML .= ""; - } + $retHTML .= ""; + if ($addForm) { + $retHTML .= ''; + } - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - $retHTML .= ""; - } - $retHTML .= "
Host Backup no Type date
"; - $retHTML .= ""; - $retHTML .= "
HostBackup noTypedateage/days
+ +
{'hostid'}."_".$backup->{'backupno'} - ."\" value=\"".$backup->{'hostid'}."_".$backup->{'backupno'}."\">
" . $backup->{'host'} . "" . $backup->{'backupno'} . "" . $backup->{'type'} . "" . $backup->{'date'} . ""; - $retHTML .= "
"; - if ($addForm) - { - $retHTML .= "
"; - } + $retHTML .= '' . $backup->{'host'} . '' . + '' . $backup->{'backupno'} . '' . + '' . $backup->{'type'} . '' . + '' . epoch_to_iso( $backup->{'date'} ) . '' . + '' . $backup->{'age'} . '' . + ''; + } + + $retHTML .= ""; + + if ($addForm) { + $retHTML .= ""; + } - return $retHTML; - - - } + return $retHTML; +} sub displayGrid($$$$) { my ($where, $addForm, $offset, $hilite) = @_; my $retHTML = ""; + my $start_t = time(); + + my ($results, $files) = getFiles($where, $offset); + + 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 ($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{}; } + + $retHTML .= qq{ +
Found $results files showing $from - $to (took $dur) @@ -287,7 +335,7 @@ }; - my @files = getFiles($where, $offset); + my $file; sub hilite_html($$) { @@ -303,7 +351,7 @@ return sprintf(qq{%s}, $action, @_); } - foreach $file (@files) { + foreach $file (@{ $files }) { my $typeStr = BackupPC::Attrib::fileType2Text(undef, $file->{'type'}); $retHTML .= ""; @@ -313,7 +361,7 @@ $typeStr, restore_link( $typeStr, $file->{'hname'}, $file->{'backupno'}, $file->{'sname'}, $file->{'fpath'}, $file->{'backupno'} ), $file->{'size'}, - $file->{'date'}, + epoch_to_iso( $file->{'date'} ), $file->{'dvd'} )) { $retHTML .= qq{}; @@ -323,19 +371,52 @@ } $retHTML .= "
ShareMedia
$v
"; - # skip pager - return $retHTML; + # 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; + + my $link_fmt = '%s'; - $retHTML .= ""; - for (my $ii = 1; $ii <= $#files; $ii++) { - $retHTML .= "$ii"; - if ($ii < $#files) { - $retHTML .= " | "; + $retHTML .= '
'; + + if ($offset > 0) { + $retHTML .= sprintf($link_fmt, $offset - 1, '<<') . ' '; + } + + while ($page <= $max_page) { + if ($page == $offset) { + $retHTML .= $del . '' . ($page + 1) . ''; + } else { + $retHTML .= $del . sprintf($link_fmt, $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 .= ' ' . sprintf($link_fmt, $offset + 1, '>>'); + } + + $retHTML .= "
"; + $retHTML .= "
" if ($addForm); - + return $retHTML; }