/[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 51 by dpavlin, Sat Aug 20 16:40:11 2005 UTC revision 62 by dpavlin, Sun Aug 21 15:59:55 2005 UTC
# Line 7  use BackupPC::Attrib qw(:all); Line 7  use BackupPC::Attrib qw(:all);
7  use DBI;  use DBI;
8  use DateTime;  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;
# Line 15  my $dsn = $Conf{SearchDSN}; Line 16  my $dsn = $Conf{SearchDSN};
16  my $db_user = $Conf{SearchUser} || '';  my $db_user = $Conf{SearchUser} || '';
17    
18  sub getUnits() {  sub getUnits() {
19      my @ret = ();          my @ret;
20      my $tmp;  
21      my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );          my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
22      my $st =          my $sth = $dbh->prepare(qq{ SELECT id, share FROM shares} );
23        $dbh->prepare(          $sth->execute();
24          " SELECT shares.ID AS ID, shares.share AS name FROM shares;");          push @ret, { 'id' => '', 'share' => '-'};       # dummy any
25      $st->execute();  
26      push (@ret, { 'ID' => '', 'name' => '-'});          while ( my $row = $sth->fetchrow_hashref() ) {
27      while ( $tmp = $st->fetchrow_hashref() ) {                  push @ret, $row;
28          push( @ret, { 'ID' => $tmp->{'ID'}, 'name' => $tmp->{'name'} } );          }
29      }          $dbh->disconnect();
30      $dbh->disconnect();          return @ret;
     return @ret;  
31  }  }
32    
33  sub epoch_to_iso {  sub epoch_to_iso {
34          my $t = shift || return;          my $t = shift || return;
35          my $dt = DateTime->from_epoch( epoch => $t ) || return;          my $dt = DateTime->from_epoch( epoch => $t ) || return;
36  print STDERR "$t == ",$dt->epoch,"\n";          print STDERR "BUG: $t != " . $dt->epoch . "\n" unless ($t == $dt->epoch);
37          return $dt->ymd . ' ' . $dt->hms;          return $dt->ymd . ' ' . $dt->hms;
38  }  }
39    
# Line 67  sub getWhere($) { Line 67  sub getWhere($) {
67          my $files_to = mk_epoch_date('search', 'to');          my $files_to = mk_epoch_date('search', 'to');
68          push @conditions, qq{ files.date <= $files_to } if ($files_to);          push @conditions, qq{ files.date <= $files_to } if ($files_to);
69    
70          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);
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 94  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,
                 shares.share||files.fullpath    AS networkPath,  
97                  files.date                      AS date,                  files.date                      AS date,
98                  files.type                      AS filetype,                  files.type                      AS filetype,
99                  files.size                      AS size,                  files.size                      AS size,
# Line 106  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 114  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 147  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);
# Line 156  sub getFiles($$) { Line 166  sub getFiles($$) {
166  sub getBackupsNotBurned() {  sub getBackupsNotBurned() {
167    
168          my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );          my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
169        my $sql = q{          my $sql = q{
170            SELECT          SELECT
171              hosts.ID         AS hostID,                  hosts.ID                AS hostid,
172              hosts.name       AS host,                  min(hosts.name)         AS host,
173              backups.num      AS backupno,                  backups.num             AS backupno,
174              backups.type     AS type,                  min(backups.type)       AS type,
175              backups.date     AS date                  min(backups.date)       AS date
176            FROM backups, shares, files, hosts          FROM backups, shares, files, hosts
177            WHERE          WHERE
178              backups.num    = files.backupNum  AND                  backups.num     = files.backupNum       AND
179              shares.ID      = files.shareID    AND                            shares.ID       = files.shareID         AND        
180              backups.hostID = shares.hostID    AND                  backups.hostID  = shares.hostID         AND
181              hosts.ID       = backups.hostID   AND                  hosts.ID        = backups.hostID        AND
182              files.dvdid    IS NULL                  files.dvdid     IS NULL
183            GROUP BY          GROUP BY
184              backups.hostID, backups.num                  backups.hostID, backups.num, hosts.id
185        };          ORDER BY min(backups.date)
186        my $st = $dbh -> prepare( $sql );          };
187        my @ret = ();          my $sth = $dbh->prepare( $sql );
188        $st -> execute();          my @ret;
189            $sth->execute();
190        while ( my $row = $st -> fetchrow_hashref() )  
191          {                    while ( my $row = $sth->fetchrow_hashref() ) {      
192              push(@ret, {                  push(@ret, {
193                           'host'     => $row->{'host'},                           'host'         => $row->{'host'},
194                           'hostid'   => $row->{'hostID'},                           'hostid'       => $row->{'hostid'},
195                           'backupno' => $row->{'backupno'},                           'backupno'     => $row->{'backupno'},
196                           'type'     => $row->{'type'},                           'type'         => $row->{'type'},
197                           'date'     => $row->{'date'}                           'date'         => $row->{'date'},
198                             '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 229  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 246  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">' . epoch_to_iso( $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 272  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>

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

  ViewVC Help
Powered by ViewVC 1.1.26