/[BackupPC]/trunk/lib/BackupPC/SearchLib.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/BackupPC/SearchLib.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 31 by dpavlin, Sun Jul 31 16:16:55 2005 UTC revision 62 by dpavlin, Sun Aug 21 15:59:55 2005 UTC
# Line 5  use strict; Line 5  use strict;
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;  
21      my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",          my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
22          "", "", { RaiseError => 1, AutoCommit => 1 } );          my $sth = $dbh->prepare(qq{ SELECT id, share FROM shares} );
23      my $st =          $sth->execute();
24        $dbh->prepare(          push @ret, { 'id' => '', 'share' => '-'};       # dummy any
25          " SELECT shares.ID AS ID, shares.share AS name FROM shares;");  
26      $st->execute();          while ( my $row = $sth->fetchrow_hashref() ) {
27      push (@ret, { 'ID' => '', 'name' => '-'});                  push @ret, $row;
28      while ( $tmp = $st->fetchrow_hashref() ) {          }
29          push( @ret, { 'ID' => $tmp->{'ID'}, 'name' => $tmp->{'name'} } );          $dbh->disconnect();
30      }          return @ret;
31      $dbh->disconnect();  }
32      return @ret;  
33    sub epoch_to_iso {
34            my $t = shift || return;
35            my $dt = DateTime->from_epoch( epoch => $t ) || return;
36            print STDERR "BUG: $t != " . $dt->epoch . "\n" unless ($t == $dt->epoch);
37            return $dt->ymd . ' ' . $dt->hms;
38  }  }
39    
40  sub getWhere($) {  sub getWhere($) {
41          my ($param)    = @_;          my ($param)    = @_;
42          my @conditions;          my @conditions;
43    
44          sub mk_iso_date($$) {          sub mk_epoch_date($$) {
45                  my ($name,$suffix) = @_;                  my ($name,$suffix) = @_;
46    
47                  my $yyyy = $param->{ $name . '_year_' . $suffix} || return;                  my $yyyy = $param->{ $name . '_year_' . $suffix} || return;
# Line 39  sub getWhere($) { Line 49  sub getWhere($) {
49                          ( $suffix eq 'from' ? 1 : 12);                          ( $suffix eq 'from' ? 1 : 12);
50                  my $dd .= $param->{ $name . '_day_' . $suffix} ||                  my $dd .= $param->{ $name . '_day_' . $suffix} ||
51                          ( $suffix eq 'from' ? 1 : 31);                          ( $suffix eq 'from' ? 1 : 31);
52                  return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd);                  my $dt = new DateTime(
53          }                          year => $yyyy,
54                            month => $mm,
55                            day => $dd
56                    );
57                    return $dt->epoch || 'NULL';
58            }
59    
60            my $backup_from = mk_epoch_date('search_backup', 'from');
61            push @conditions, qq{ backups.date >= $backup_from } if ($backup_from);
62            my $backup_to = mk_epoch_date('search_backup', 'to');
63            push @conditions, qq{ backups.date <= $backup_to } if ($backup_to);
64    
65            my $files_from = mk_epoch_date('search', 'from');
66            push @conditions, qq{ files.date >= $files_from } if ($files_from);
67            my $files_to = mk_epoch_date('search', 'to');
68            push @conditions, qq{ files.date <= $files_to } if ($files_to);
69    
70          my $backup_from = mk_iso_date('search_backup', 'from');          print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:" . join(" | ",@conditions);
         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);  
   
         print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions);  
71            
72          push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'});          push( @conditions, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'});
73    
74          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'});
75    
76          return (          return (
77                  join(" and ", @conditions),                  join(" and ", @conditions),
# Line 69  sub getWhere($) { Line 84  sub getWhere($) {
84  sub getFiles($$) {  sub getFiles($$) {
85          my ($where, $offset) = @_;          my ($where, $offset) = @_;
86    
87          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 } );  
88    
89          my $sql_cols = qq{          my $sql_cols = qq{
90                  files.id                        AS fid,                  files.id                        AS fid,
# Line 80  sub getFiles($$) { Line 94  sub getFiles($$) {
94                  files.backupNum                 AS backupNum,                  files.backupNum                 AS backupNum,
95                  files.name                      AS filename,                  files.name                      AS filename,
96                  files.path                      AS filepath,                  files.path                      AS filepath,
97                  shares.share||files.fullpath    AS networkPath,                  files.date                      AS date,
                 date(files.date, 'unixepoch', 'localtime') AS date,  
98                  files.type                      AS filetype,                  files.type                      AS filetype,
99                  files.size                      AS size,                  files.size                      AS size,
100                  dvds.name                       AS dvd                  dvds.name                       AS dvd
# Line 92  sub getFiles($$) { Line 105  sub getFiles($$) {
105                          INNER JOIN shares       ON files.shareID=shares.ID                          INNER JOIN shares       ON files.shareID=shares.ID
106                          INNER JOIN hosts        ON hosts.ID = shares.hostID                          INNER JOIN hosts        ON hosts.ID = shares.hostID
107                          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
108            };
109    
110            my $sql_dvd_from = qq{
111                          LEFT  JOIN dvds         ON dvds.ID = files.dvdid                          LEFT  JOIN dvds         ON dvds.ID = files.dvdid
112          };          };
113    
# Line 100  sub getFiles($$) { Line 116  sub getFiles($$) {
116    
117          my $sql_order = qq{          my $sql_order = qq{
118                  ORDER BY files.id                  ORDER BY files.id
119                          LIMIT $on_page                  LIMIT $on_page
120                          OFFSET ?                  OFFSET ?
121          };          };
122    
123            my $sql_count = qq{ select count(files.id) $sql_from $sql_where };
124            my $sql_results = qq{ select $sql_cols $sql_from $sql_dvd_from $sql_where $sql_order };
125    
126          $offset ||= 0;          $offset ||= 0;
127          $offset = ($offset * $on_page) + 1;          $offset = ($offset * $on_page);
128    
129          my $sth = $dbh->prepare(qq{ select count(files.id) $sql_from $sql_where });          my $sth = $dbh->prepare($sql_count);
130          $sth->execute();          $sth->execute();
   
131          my ($results) = $sth->fetchrow_array();          my ($results) = $sth->fetchrow_array();
132    
133          $sth = $dbh->prepare(qq{ select $sql_cols $sql_from $sql_where $sql_order });          $sth = $dbh->prepare($sql_results);
134          $sth->execute( $offset );          $sth->execute( $offset );
135    
136            if ($sth->rows != $results) {
137                    my $bug = "$0 BUG: [[ $sql_count ]] = $results while [[ $sql_results ]] = " . $sth->rows;
138                    $bug =~ s/\s+/ /gs;
139                    print STDERR "$bug\n";
140            }
141    
142          my @ret;          my @ret;
143                
144          while (my $row = $sth->fetchrow_hashref()) {          while (my $row = $sth->fetchrow_hashref()) {
# Line 122  sub getFiles($$) { Line 146  sub getFiles($$) {
146                          'hname'         => $row->{'hname'},                          'hname'         => $row->{'hname'},
147                          'sname'         => $row->{'sname'},                          'sname'         => $row->{'sname'},
148                          'sharename'     => $row->{'sharename'},                          'sharename'     => $row->{'sharename'},
149                          'backupno'      => $row->{'backupNum'},                          'backupno'      => $row->{'backupnum'},
150                          'fname'         => $row->{'filename'},                          'fname'         => $row->{'filename'},
151                          'fpath'         => $row->{'filepath'},                          'fpath'         => $row->{'filepath'},
152                          'networkpath'   => $row->{'networkPath'},                          'networkpath'   => $row->{'networkpath'},
153                          'date'          => $row->{'date'},                          'date'          => $row->{'date'},
154                          'type'          => $row->{'filetype'},                          'type'          => $row->{'filetype'},
155                          'size'          => $row->{'size'},                          'size'          => $row->{'size'},
# Line 133  sub getFiles($$) { Line 157  sub getFiles($$) {
157                          'dvd'           => $row->{'dvd'}                          'dvd'           => $row->{'dvd'}
158                  });                  });
159          }          }
160              
161          $sth->finish();          $sth->finish();
162          $dbh->disconnect();          $dbh->disconnect();
163          return ($results, \@ret);          return ($results, \@ret);
164  }  }
165    
166  sub getBackupsNotBurned()  sub getBackupsNotBurned() {
167    {  
168        my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",          my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
169          "", "", { RaiseError => 1, AutoCommit => 1 } );                my $sql = q{
170        my $sql = q{          SELECT
171            SELECT                  hosts.ID                AS hostid,
172              hosts.ID         AS hostID,                  min(hosts.name)         AS host,
173              hosts.name       AS host,                  backups.num             AS backupno,
174              backups.num      AS backupno,                  min(backups.type)       AS type,
175              backups.type     AS type,                  min(backups.date)       AS date
176              backups.date     AS date          FROM backups, shares, files, hosts
177            FROM backups, shares, files, hosts          WHERE
178            WHERE                  backups.num     = files.backupNum       AND
179              backups.num    = files.backupNum  AND                  shares.ID       = files.shareID         AND        
180              shares.ID      = files.shareID    AND                            backups.hostID  = shares.hostID         AND
181              backups.hostID = shares.hostID    AND                  hosts.ID        = backups.hostID        AND
182              hosts.ID       = backups.hostID   AND                  files.dvdid     IS NULL
183              files.dvdid    IS NULL          GROUP BY
184            GROUP BY                  backups.hostID, backups.num, hosts.id
185              backups.hostID, backups.num          ORDER BY min(backups.date)
186        };          };
187        my $st = $dbh -> prepare( $sql );          my $sth = $dbh->prepare( $sql );
188        my @ret = ();          my @ret;
189        $st -> execute();          $sth->execute();
190    
191        while ( my $row = $st -> fetchrow_hashref() )          while ( my $row = $sth->fetchrow_hashref() ) {      
192          {                            push(@ret, {
193              push(@ret, {                           'host'         => $row->{'host'},
194                           'host'     => $row->{'host'},                           'hostid'       => $row->{'hostid'},
195                           'hostid'   => $row->{'hostID'},                           'backupno'     => $row->{'backupno'},
196                           'backupno' => $row->{'backupno'},                           'type'         => $row->{'type'},
197                           'type'     => $row->{'type'},                           'date'         => $row->{'date'},
198                           'date'     => $row->{'date'}                           'age'          => sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ),
199                         }                         }
200              );                  );
201          }          }
202                
203        return @ret;                return @ret;      
204    }  }
205    
206  sub displayBackupsGrid()  sub displayBackupsGrid()
207    {    {
# Line 216  EOF3 Line 240  EOF3
240          if ($addForm) {          if ($addForm) {
241              $retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>";              $retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>";
242          }          }
243          $retHTML .=  qq{<td class="tableheader">Host</td><td class="tableheader">Backup no</td><td class="tableheader">Type</td><td class="tableheader">date</td></tr>};          $retHTML .=  qq{
244                    <td class="tableheader">Host</td>
245                    <td class="tableheader">Backup no</td>
246                    <td class="tableheader">Type</td>
247                    <td class="tableheader">date</td>
248                    <td class="tableheader">age/days</td>
249                    </tr>
250            };
251    
252          my @backups = getBackupsNotBurned();          my @backups = getBackupsNotBurned();
253          my $backup;          my $backup;
254    
255          if ($addForm) {          if ($addForm) {
256                  $retHTML .= qq{<tr><td colspan=7 style="tableheader">                  $retHTML .= qq{
257                            <tr><td colspan=7 style="tableheader">
258                          <input type="submit" value="Burn selected backups on medium" name="submitBurner">                          <input type="submit" value="Burn selected backups on medium" name="submitBurner">
259                          </td></tr>};                          </td></tr>
260                    };
261          }          }
262    
263          foreach $backup(@backups) {          foreach $backup(@backups) {
# Line 233  EOF3 Line 266  EOF3
266                            
267                  $retHTML .= "<tr>";                  $retHTML .= "<tr>";
268                  if ($addForm) {                  if ($addForm) {
269                          $retHTML .= qq{<td class="fview"><input type="checkbox" name="fcb} .                          $retHTML .= '<td class="fview"><input type="checkbox" name="fcb' .
270                                  $backup->{'hostid'}."_".$backup->{'backupno'} .                                  $backup->{'hostid'}.'_'.$backup->{'backupno'} .
271                                  qq{" value="} . $backup->{'hostid'}."_".$backup->{'backupno'} .                                  '" value="' . $backup->{'hostid'}.'_'.$backup->{'backupno'} .
272                                  qq{"></td>};                                  '"></td>';
273                  }                            }          
274                            
275                  $retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' .                  $retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' .
276                          '<td class="fviewborder">' . $backup->{'backupno'} . '</td>' .                          '<td class="fviewborder">' . $backup->{'backupno'} . '</td>' .
277                          '<td class="fviewborder">' . $backup->{'type'} . '</td>' .                          '<td class="fviewborder">' . $backup->{'type'} . '</td>' .
278                          '<td class="fviewborder">' . $backup->{'date'} . '<td>' .                          '<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
279                            '<td class="fviewborder">' . $backup->{'age'} . '</td>' .
280                          '</tr>';                          '</tr>';
281          }          }
282    
# Line 259  sub displayGrid($$$$) { Line 293  sub displayGrid($$$$) {
293          my ($where, $addForm, $offset, $hilite) = @_;          my ($where, $addForm, $offset, $hilite) = @_;
294          my $retHTML = "";          my $retHTML = "";
295    
296            my $start_t = time();
297    
298            my ($results, $files) = getFiles($where, $offset);
299    
300            my $dur_t = time() - $start_t;
301            my $dur = sprintf("%0.4fs", $dur_t);
302    
303            my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page);
304    
305            if ($results <= 0) {
306                    $retHTML .= qq{
307                            <p style="color: red;">No results found...</p>
308                    };
309                    return $retHTML;
310            } else {
311                    # DEBUG
312                    #use Data::Dumper;
313                    #$retHTML .= '<pre>' . Dumper($files) . '</pre>';
314            }
315    
316    
317          if ($addForm) {          if ($addForm) {
318                  $retHTML .= qq{<form name="forma" method="GET" action="$MyURL">};                  $retHTML .= qq{<form name="forma" method="GET" action="$MyURL">};
319                  $retHTML.= qq{<input type="hidden" value="search" name="action">};                  $retHTML.= qq{<input type="hidden" value="search" name="action">};
320                  $retHTML .= qq{<input type="hidden" value="results" name="search_results">};                  $retHTML .= qq{<input type="hidden" value="results" name="search_results">};
321          }          }
322    
         my ($results, $files) = getFiles($where, $offset);  
   
         my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page);  
323    
324          $retHTML .= qq{          $retHTML .= qq{
325          <br/>Found $results files, showing $from - $to          <br/>Found <b>$results files</b> showing <b>$from - $to</b> (took $dur)
326          <table style="fview" width="100%">          <table style="fview" width="100%">
327                  <tr>                  <tr>
328                  <td class="tableheader">Share</td>                  <td class="tableheader">Share</td>
# Line 308  sub displayGrid($$$$) { Line 360  sub displayGrid($$$$) {
360                          $typeStr,                          $typeStr,
361                          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'} ),
362                          $file->{'size'},                          $file->{'size'},
363                          $file->{'date'},                          epoch_to_iso( $file->{'date'} ),
364                          $file->{'dvd'}                          $file->{'dvd'}
365                  )) {                  )) {
366                          $retHTML .= qq{<td class="fviewborder">$v</td>};                          $retHTML .= qq{<td class="fviewborder">$v</td>};

Legend:
Removed from v.31  
changed lines
  Added in v.62

  ViewVC Help
Powered by ViewVC 1.1.26