5 |
use BackupPC::CGI::Lib qw(:all); |
use BackupPC::CGI::Lib qw(:all); |
6 |
use BackupPC::Attrib qw(:all); |
use BackupPC::Attrib qw(:all); |
7 |
use DBI; |
use DBI; |
8 |
|
use DateTime; |
9 |
use vars qw(%In $MyURL); |
use vars qw(%In $MyURL); |
10 |
|
use Time::HiRes qw/time/; |
11 |
|
|
12 |
my $on_page = 100; |
my $on_page = 100; |
13 |
my $pager_pages = 10; |
my $pager_pages = 10; |
14 |
|
|
15 |
|
my $dsn = $Conf{SearchDSN}; |
16 |
|
my $db_user = $Conf{SearchUser} || ''; |
17 |
|
|
18 |
sub getUnits() { |
sub getUnits() { |
19 |
my @ret = (); |
my @ret = (); |
20 |
my $tmp; |
my $tmp; |
21 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); |
|
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
|
22 |
my $st = |
my $st = |
23 |
$dbh->prepare( |
$dbh->prepare( |
24 |
" SELECT shares.ID AS ID, shares.share AS name FROM shares;"); |
" SELECT shares.ID AS ID, shares.share AS name FROM shares;"); |
31 |
return @ret; |
return @ret; |
32 |
} |
} |
33 |
|
|
34 |
|
sub epoch_to_iso { |
35 |
|
my $t = shift || return; |
36 |
|
my $dt = DateTime->from_epoch( epoch => $t ) || return; |
37 |
|
print STDERR "$t == ",$dt->epoch,"\n"; |
38 |
|
return $dt->ymd . ' ' . $dt->hms; |
39 |
|
} |
40 |
|
|
41 |
sub getWhere($) { |
sub getWhere($) { |
42 |
my ($param) = @_; |
my ($param) = @_; |
43 |
my @conditions; |
my @conditions; |
44 |
|
|
45 |
sub mk_iso_date($$) { |
sub mk_epoch_date($$) { |
46 |
my ($name,$suffix) = @_; |
my ($name,$suffix) = @_; |
47 |
|
|
48 |
my $yyyy = $param->{ $name . '_year_' . $suffix} || return; |
my $yyyy = $param->{ $name . '_year_' . $suffix} || return; |
50 |
( $suffix eq 'from' ? 1 : 12); |
( $suffix eq 'from' ? 1 : 12); |
51 |
my $dd .= $param->{ $name . '_day_' . $suffix} || |
my $dd .= $param->{ $name . '_day_' . $suffix} || |
52 |
( $suffix eq 'from' ? 1 : 31); |
( $suffix eq 'from' ? 1 : 31); |
53 |
return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd); |
my $dt = new DateTime( |
54 |
} |
year => $yyyy, |
55 |
|
month => $mm, |
56 |
my $backup_from = mk_iso_date('search_backup', 'from'); |
day => $dd |
57 |
push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from); |
); |
58 |
my $backup_to = mk_iso_date('search_backup', 'to'); |
return $dt->epoch || 'NULL'; |
59 |
push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to); |
} |
60 |
|
|
61 |
my $files_from = mk_iso_date('search', 'from'); |
my $backup_from = mk_epoch_date('search_backup', 'from'); |
62 |
push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from); |
push @conditions, qq{ backups.date >= $backup_from } if ($backup_from); |
63 |
my $files_to = mk_iso_date('search', 'to'); |
my $backup_to = mk_epoch_date('search_backup', 'to'); |
64 |
push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to); |
push @conditions, qq{ backups.date <= $backup_to } if ($backup_to); |
65 |
|
|
66 |
|
my $files_from = mk_epoch_date('search', 'from'); |
67 |
|
push @conditions, qq{ files.date >= $files_from } if ($files_from); |
68 |
|
my $files_to = mk_epoch_date('search', 'to'); |
69 |
|
push @conditions, qq{ files.date <= $files_to } if ($files_to); |
70 |
|
|
71 |
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); |
72 |
|
|
85 |
sub getFiles($$) { |
sub getFiles($$) { |
86 |
my ($where, $offset) = @_; |
my ($where, $offset) = @_; |
87 |
|
|
88 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); |
|
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
|
89 |
|
|
90 |
my $sql_cols = qq{ |
my $sql_cols = qq{ |
91 |
files.id AS fid, |
files.id AS fid, |
96 |
files.name AS filename, |
files.name AS filename, |
97 |
files.path AS filepath, |
files.path AS filepath, |
98 |
shares.share||files.fullpath AS networkPath, |
shares.share||files.fullpath AS networkPath, |
99 |
date(files.date, 'unixepoch', 'localtime') AS date, |
files.date AS date, |
100 |
files.type AS filetype, |
files.type AS filetype, |
101 |
files.size AS size, |
files.size AS size, |
102 |
|
}; |
103 |
|
|
104 |
|
my $sql_dvd_cols = qq{ |
105 |
dvds.name AS dvd |
dvds.name AS dvd |
106 |
}; |
}; |
107 |
|
|
110 |
INNER JOIN shares ON files.shareID=shares.ID |
INNER JOIN shares ON files.shareID=shares.ID |
111 |
INNER JOIN hosts ON hosts.ID = shares.hostID |
INNER JOIN hosts ON hosts.ID = shares.hostID |
112 |
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 |
113 |
|
}; |
114 |
|
|
115 |
|
my $sql_dvd_from = qq{ |
116 |
LEFT JOIN dvds ON dvds.ID = files.dvdid |
LEFT JOIN dvds ON dvds.ID = files.dvdid |
117 |
}; |
}; |
118 |
|
|
133 |
|
|
134 |
my ($results) = $sth->fetchrow_array(); |
my ($results) = $sth->fetchrow_array(); |
135 |
|
|
136 |
$sth = $dbh->prepare(qq{ select $sql_cols $sql_from $sql_where $sql_order }); |
$sth = $dbh->prepare(qq{ select $sql_cols $sql_dvd_cols $sql_from $sql_dvd_from $sql_where $sql_order }); |
137 |
$sth->execute( $offset ); |
$sth->execute( $offset ); |
138 |
|
|
139 |
my @ret; |
my @ret; |
143 |
'hname' => $row->{'hname'}, |
'hname' => $row->{'hname'}, |
144 |
'sname' => $row->{'sname'}, |
'sname' => $row->{'sname'}, |
145 |
'sharename' => $row->{'sharename'}, |
'sharename' => $row->{'sharename'}, |
146 |
'backupno' => $row->{'backupNum'}, |
'backupno' => $row->{'backupnum'}, |
147 |
'fname' => $row->{'filename'}, |
'fname' => $row->{'filename'}, |
148 |
'fpath' => $row->{'filepath'}, |
'fpath' => $row->{'filepath'}, |
149 |
'networkpath' => $row->{'networkPath'}, |
'networkpath' => $row->{'networkpath'}, |
150 |
'date' => $row->{'date'}, |
'date' => $row->{'date'}, |
151 |
'type' => $row->{'filetype'}, |
'type' => $row->{'filetype'}, |
152 |
'size' => $row->{'size'}, |
'size' => $row->{'size'}, |
160 |
return ($results, \@ret); |
return ($results, \@ret); |
161 |
} |
} |
162 |
|
|
163 |
sub getBackupsNotBurned() |
sub getBackupsNotBurned() { |
164 |
{ |
|
165 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); |
166 |
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
my $sql = q{ |
167 |
my $sql = q{ |
SELECT |
168 |
SELECT |
hosts.ID AS hostid, |
169 |
hosts.ID AS hostID, |
min(hosts.name) AS host, |
170 |
hosts.name AS host, |
backups.num AS backupno, |
171 |
backups.num AS backupno, |
min(backups.type) AS type, |
172 |
backups.type AS type, |
min(backups.date) AS date |
173 |
backups.date AS date |
FROM backups, shares, files, hosts |
174 |
FROM backups, shares, files, hosts |
WHERE |
175 |
WHERE |
backups.num = files.backupNum AND |
176 |
backups.num = files.backupNum AND |
shares.ID = files.shareID AND |
177 |
shares.ID = files.shareID AND |
backups.hostID = shares.hostID AND |
178 |
backups.hostID = shares.hostID AND |
hosts.ID = backups.hostID AND |
179 |
hosts.ID = backups.hostID AND |
files.dvdid IS NULL |
180 |
files.dvdid IS NULL |
GROUP BY |
181 |
GROUP BY |
backups.hostID, backups.num, hosts.id |
182 |
backups.hostID, backups.num |
}; |
183 |
}; |
my $sth = $dbh->prepare( $sql ); |
184 |
my $st = $dbh -> prepare( $sql ); |
my @ret; |
185 |
my @ret = (); |
$sth->execute(); |
186 |
$st -> execute(); |
|
187 |
|
while ( my $row = $sth->fetchrow_hashref() ) { |
188 |
while ( my $row = $st -> fetchrow_hashref() ) |
push(@ret, { |
|
{ |
|
|
push(@ret, { |
|
189 |
'host' => $row->{'host'}, |
'host' => $row->{'host'}, |
190 |
'hostid' => $row->{'hostID'}, |
'hostid' => $row->{'hostid'}, |
191 |
'backupno' => $row->{'backupno'}, |
'backupno' => $row->{'backupno'}, |
192 |
'type' => $row->{'type'}, |
'type' => $row->{'type'}, |
193 |
'date' => $row->{'date'} |
'date' => $row->{'date'} |
194 |
} |
} |
195 |
); |
); |
196 |
} |
} |
197 |
|
|
198 |
return @ret; |
return @ret; |
199 |
} |
} |
200 |
|
|
201 |
sub displayBackupsGrid() |
sub displayBackupsGrid() |
202 |
{ |
{ |
261 |
$retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' . |
$retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' . |
262 |
'<td class="fviewborder">' . $backup->{'backupno'} . '</td>' . |
'<td class="fviewborder">' . $backup->{'backupno'} . '</td>' . |
263 |
'<td class="fviewborder">' . $backup->{'type'} . '</td>' . |
'<td class="fviewborder">' . $backup->{'type'} . '</td>' . |
264 |
'<td class="fviewborder">' . $backup->{'date'} . '<td>' . |
'<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '<td>' . |
265 |
'</tr>'; |
'</tr>'; |
266 |
} |
} |
267 |
|
|
284 |
$retHTML .= qq{<input type="hidden" value="results" name="search_results">}; |
$retHTML .= qq{<input type="hidden" value="results" name="search_results">}; |
285 |
} |
} |
286 |
|
|
287 |
|
my $start_t = time(); |
288 |
|
|
289 |
my ($results, $files) = getFiles($where, $offset); |
my ($results, $files) = getFiles($where, $offset); |
290 |
|
|
291 |
|
my $dur_t = time() - $start_t; |
292 |
|
my $dur = sprintf("%0.4fs", $dur_t); |
293 |
|
|
294 |
my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page); |
my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page); |
295 |
|
|
296 |
$retHTML .= qq{ |
$retHTML .= qq{ |
297 |
<br/>Found $results files, showing $from - $to |
<br/>Found <b>$results files</b> showing <b>$from - $to</b> (took $dur) |
298 |
<table style="fview" width="100%"> |
<table style="fview" width="100%"> |
299 |
<tr> |
<tr> |
300 |
<td class="tableheader">Share</td> |
<td class="tableheader">Share</td> |
332 |
$typeStr, |
$typeStr, |
333 |
restore_link( $typeStr, $file->{'hname'}, $file->{'backupno'}, $file->{'sname'}, $file->{'fpath'}, $file->{'backupno'} ), |
restore_link( $typeStr, $file->{'hname'}, $file->{'backupno'}, $file->{'sname'}, $file->{'fpath'}, $file->{'backupno'} ), |
334 |
$file->{'size'}, |
$file->{'size'}, |
335 |
$file->{'date'}, |
epoch_to_iso( $file->{'date'} ), |
336 |
$file->{'dvd'} |
$file->{'dvd'} |
337 |
)) { |
)) { |
338 |
$retHTML .= qq{<td class="fviewborder">$v</td>}; |
$retHTML .= qq{<td class="fviewborder">$v</td>}; |