/[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 55 by dpavlin, Sun Aug 21 13:44:20 2005 UTC revision 59 by dpavlin, Sun Aug 21 15:29:24 2005 UTC
# Line 16  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 68  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, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'});
73    
# Line 99  sub getFiles($$) { Line 98  sub getFiles($$) {
98                  files.date                      AS date,                  files.date                      AS date,
99                  files.type                      AS filetype,                  files.type                      AS filetype,
100                  files.size                      AS size,                  files.size                      AS size,
         };  
   
         my $sql_dvd_cols = qq{  
101                  dvds.name                       AS dvd                  dvds.name                       AS dvd
102          };          };
103    
# Line 121  sub getFiles($$) { Line 117  sub getFiles($$) {
117    
118          my $sql_order = qq{          my $sql_order = qq{
119                  ORDER BY files.id                  ORDER BY files.id
120                          LIMIT $on_page                  LIMIT $on_page
121                          OFFSET ?                  OFFSET ?
122          };          };
123    
124            my $sql_count = qq{ select count(files.id) $sql_from $sql_where };
125            my $sql_results = qq{ select $sql_cols $sql_from $sql_dvd_from $sql_where $sql_order };
126    
127          $offset ||= 0;          $offset ||= 0;
128          $offset = ($offset * $on_page) + 1;          $offset = ($offset * $on_page);
129    
130          my $sth = $dbh->prepare(qq{ select count(files.id) $sql_from $sql_where });          my $sth = $dbh->prepare($sql_count);
131          $sth->execute();          $sth->execute();
   
132          my ($results) = $sth->fetchrow_array();          my ($results) = $sth->fetchrow_array();
133    
134          $sth = $dbh->prepare(qq{ select $sql_cols $sql_dvd_cols $sql_from $sql_dvd_from $sql_where $sql_order });          $sth = $dbh->prepare($sql_results);
135          $sth->execute( $offset );          $sth->execute( $offset );
136    
137            if ($sth->rows != $results) {
138                    my $bug = "$0 BUG: [[ $sql_count ]] = $results while [[ $sql_results ]] = " . $sth->rows;
139                    $bug =~ s/\s+/ /gs;
140                    print STDERR "$bug\n";
141            }
142    
143          my @ret;          my @ret;
144                
145          while (my $row = $sth->fetchrow_hashref()) {          while (my $row = $sth->fetchrow_hashref()) {
# Line 154  sub getFiles($$) { Line 158  sub getFiles($$) {
158                          'dvd'           => $row->{'dvd'}                          'dvd'           => $row->{'dvd'}
159                  });                  });
160          }          }
161              
162          $sth->finish();          $sth->finish();
163          $dbh->disconnect();          $dbh->disconnect();
164          return ($results, \@ret);          return ($results, \@ret);
# Line 179  sub getBackupsNotBurned() { Line 183  sub getBackupsNotBurned() {
183                  files.dvdid     IS NULL                  files.dvdid     IS NULL
184          GROUP BY          GROUP BY
185                  backups.hostID, backups.num, hosts.id                  backups.hostID, backups.num, hosts.id
186            ORDER BY min(backups.date)
187          };          };
188          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
189          my @ret;          my @ret;
# Line 186  sub getBackupsNotBurned() { Line 191  sub getBackupsNotBurned() {
191    
192          while ( my $row = $sth->fetchrow_hashref() ) {                while ( my $row = $sth->fetchrow_hashref() ) {      
193                  push(@ret, {                  push(@ret, {
194                           'host'     => $row->{'host'},                           'host'         => $row->{'host'},
195                           'hostid'   => $row->{'hostid'},                           'hostid'       => $row->{'hostid'},
196                           'backupno' => $row->{'backupno'},                           'backupno'     => $row->{'backupno'},
197                           'type'     => $row->{'type'},                           'type'         => $row->{'type'},
198                           'date'     => $row->{'date'}                           'date'         => $row->{'date'},
199                             'age'          => sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ),
200                         }                         }
201                  );                  );
202          }          }
# Line 235  EOF3 Line 241  EOF3
241          if ($addForm) {          if ($addForm) {
242              $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>";
243          }          }
244          $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{
245                    <td class="tableheader">Host</td>
246                    <td class="tableheader">Backup no</td>
247                    <td class="tableheader">Type</td>
248                    <td class="tableheader">date</td>
249                    <td class="tableheader">age/days</td>
250                    </tr>
251            };
252    
253          my @backups = getBackupsNotBurned();          my @backups = getBackupsNotBurned();
254          my $backup;          my $backup;
255    
256          if ($addForm) {          if ($addForm) {
257                  $retHTML .= qq{<tr><td colspan=7 style="tableheader">                  $retHTML .= qq{
258                            <tr><td colspan=7 style="tableheader">
259                          <input type="submit" value="Burn selected backups on medium" name="submitBurner">                          <input type="submit" value="Burn selected backups on medium" name="submitBurner">
260                          </td></tr>};                          </td></tr>
261                    };
262          }          }
263    
264          foreach $backup(@backups) {          foreach $backup(@backups) {
# Line 252  EOF3 Line 267  EOF3
267                            
268                  $retHTML .= "<tr>";                  $retHTML .= "<tr>";
269                  if ($addForm) {                  if ($addForm) {
270                          $retHTML .= qq{<td class="fview"><input type="checkbox" name="fcb} .                          $retHTML .= '<td class="fview"><input type="checkbox" name="fcb' .
271                                  $backup->{'hostid'}."_".$backup->{'backupno'} .                                  $backup->{'hostid'}.'_'.$backup->{'backupno'} .
272                                  qq{" value="} . $backup->{'hostid'}."_".$backup->{'backupno'} .                                  '" value="' . $backup->{'hostid'}.'_'.$backup->{'backupno'} .
273                                  qq{"></td>};                                  '"></td>';
274                  }                            }          
275                            
276                  $retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' .                  $retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' .
277                          '<td class="fviewborder">' . $backup->{'backupno'} . '</td>' .                          '<td class="fviewborder">' . $backup->{'backupno'} . '</td>' .
278                          '<td class="fviewborder">' . $backup->{'type'} . '</td>' .                          '<td class="fviewborder">' . $backup->{'type'} . '</td>' .
279                          '<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '<td>' .                          '<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
280                            '<td class="fviewborder">' . $backup->{'age'} . '</td>' .
281                          '</tr>';                          '</tr>';
282          }          }
283    
# Line 278  sub displayGrid($$$$) { Line 294  sub displayGrid($$$$) {
294          my ($where, $addForm, $offset, $hilite) = @_;          my ($where, $addForm, $offset, $hilite) = @_;
295          my $retHTML = "";          my $retHTML = "";
296    
         if ($addForm) {  
                 $retHTML .= qq{<form name="forma" method="GET" action="$MyURL">};  
                 $retHTML.= qq{<input type="hidden" value="search" name="action">};  
                 $retHTML .= qq{<input type="hidden" value="results" name="search_results">};  
         }  
   
297          my $start_t = time();          my $start_t = time();
298    
299          my ($results, $files) = getFiles($where, $offset);          my ($results, $files) = getFiles($where, $offset);
# Line 293  sub displayGrid($$$$) { Line 303  sub displayGrid($$$$) {
303    
304          my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page);          my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page);
305    
306            if ($results <= 0) {
307                    $retHTML .= qq{
308                            <p style="color: red;">No results found...</p>
309                    };
310                    return $retHTML;
311            } else {
312                    # DEBUG
313                    #use Data::Dumper;
314                    #$retHTML .= '<pre>' . Dumper($files) . '</pre>';
315            }
316    
317    
318            if ($addForm) {
319                    $retHTML .= qq{<form name="forma" method="GET" action="$MyURL">};
320                    $retHTML.= qq{<input type="hidden" value="search" name="action">};
321                    $retHTML .= qq{<input type="hidden" value="results" name="search_results">};
322            }
323    
324    
325          $retHTML .= qq{          $retHTML .= qq{
326          <br/>Found <b>$results files</b> showing <b>$from - $to</b> (took $dur)          <br/>Found <b>$results files</b> showing <b>$from - $to</b> (took $dur)
327          <table style="fview" width="100%">          <table style="fview" width="100%">

Legend:
Removed from v.55  
changed lines
  Added in v.59

  ViewVC Help
Powered by ViewVC 1.1.26