/[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 58 by dpavlin, Sun Aug 21 14:26:23 2005 UTC revision 72 by dpavlin, Thu Aug 25 11:41:58 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 ORDER BY share} );
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            $t += 60 * 60 * +2;     # FIXME add TZ
36          my $dt = DateTime->from_epoch( epoch => $t ) || return;          my $dt = DateTime->from_epoch( epoch => $t ) || return;
37  print STDERR "$t == ",$dt->epoch,"\n";          print STDERR "BUG: $t != " . $dt->epoch . "\n" unless ($t == $dt->epoch);
38          return $dt->ymd . ' ' . $dt->hms;          return $dt->ymd . ' ' . $dt->hms;
39  }  }
40    
# Line 68  sub getWhere($) { Line 68  sub getWhere($) {
68          my $files_to = mk_epoch_date('search', 'to');          my $files_to = mk_epoch_date('search', 'to');
69          push @conditions, qq{ files.date <= $files_to } if ($files_to);          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            
73          push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'});          push( @conditions, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'});
74    
75          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'});
76    
77          return (          return (
78                  join(" and ", @conditions),                  join(" and ", @conditions),
# Line 95  sub getFiles($$) { Line 95  sub getFiles($$) {
95                  files.backupNum                 AS backupNum,                  files.backupNum                 AS backupNum,
96                  files.name                      AS filename,                  files.name                      AS filename,
97                  files.path                      AS filepath,                  files.path                      AS filepath,
                 shares.share||files.fullpath    AS networkPath,  
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 109  sub getFiles($$) { Line 105  sub getFiles($$) {
105                  FROM files                  FROM files
106                          INNER JOIN shares       ON files.shareID=shares.ID                          INNER JOIN shares       ON files.shareID=shares.ID
107                          INNER JOIN hosts        ON hosts.ID = shares.hostID                          INNER JOIN hosts        ON hosts.ID = shares.hostID
108                          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 AND backups.shareID = shares.ID
109          };          };
110    
111          my $sql_dvd_from = qq{          my $sql_dvd_from = qq{
# Line 120  sub getFiles($$) { Line 116  sub getFiles($$) {
116          $sql_where = " WHERE ". $where if ($where);          $sql_where = " WHERE ". $where if ($where);
117    
118          my $sql_order = qq{          my $sql_order = qq{
119                  ORDER BY files.id                  ORDER BY files.date
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 165  sub getBackupsNotBurned() { Line 169  sub getBackupsNotBurned() {
169          my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );          my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
170          my $sql = q{          my $sql = q{
171          SELECT          SELECT
172                  hosts.ID                AS hostid,                  backups.hostID          AS hostid,
173                  min(hosts.name)         AS host,                  min(hosts.name)         AS host,
174                  backups.num             AS backupno,                  backups.num             AS backupno,
175                  min(backups.type)       AS type,                  min(backups.type)       AS type,
176                  min(backups.date)       AS date                  min(backups.date)       AS date,
177          FROM backups, shares, files, hosts                  min(backups.size)       AS size
178            FROM backups
179                    INNER JOIN hosts        ON hosts.ID = backups.hostID
180          WHERE          WHERE
                 backups.num     = files.backupNum       AND  
                 shares.ID       = files.shareID         AND          
                 backups.hostID  = shares.hostID         AND  
                 hosts.ID        = backups.hostID        AND  
181                  files.dvdid     IS NULL                  files.dvdid     IS NULL
182          GROUP BY          GROUP BY
183                  backups.hostID, backups.num, hosts.id                  backups.hostID, backups.num
184          ORDER BY min(backups.date)          ORDER BY min(backups.date)
185          };          };
186          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
187          my @ret;          my @ret;
188          $sth->execute();          $sth->execute();
189    
190          while ( my $row = $sth->fetchrow_hashref() ) {                while ( my $row = $sth->fetchrow_hashref() ) {
191                  push(@ret, {                  $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );
192                           'host'         => $row->{'host'},                  $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);
193                           'hostid'       => $row->{'hostid'},                  push @ret, $row;
                          'backupno'     => $row->{'backupno'},  
                          'type'         => $row->{'type'},  
                          'date'         => $row->{'date'},  
                          'age'          => sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ),  
                        }  
                 );  
194          }          }
195                
196          return @ret;                return @ret;      
# Line 243  EOF3 Line 239  EOF3
239                  <td class="tableheader">Type</td>                  <td class="tableheader">Type</td>
240                  <td class="tableheader">date</td>                  <td class="tableheader">date</td>
241                  <td class="tableheader">age/days</td>                  <td class="tableheader">age/days</td>
242                    <td class="tableheader">size/MB</td>
243                  </tr>                  </tr>
244          };          };
245    
# Line 274  EOF3 Line 271  EOF3
271                          '<td class="fviewborder">' . $backup->{'type'} . '</td>' .                          '<td class="fviewborder">' . $backup->{'type'} . '</td>' .
272                          '<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .                          '<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
273                          '<td class="fviewborder">' . $backup->{'age'} . '</td>' .                          '<td class="fviewborder">' . $backup->{'age'} . '</td>' .
274                            '<td class="fviewborder">' . $backup->{'size'} . '</td>' .
275                          '</tr>';                          '</tr>';
276          }          }
277    
# Line 290  sub displayGrid($$$$) { Line 288  sub displayGrid($$$$) {
288          my ($where, $addForm, $offset, $hilite) = @_;          my ($where, $addForm, $offset, $hilite) = @_;
289          my $retHTML = "";          my $retHTML = "";
290    
         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">};  
         }  
   
291          my $start_t = time();          my $start_t = time();
292    
293          my ($results, $files) = getFiles($where, $offset);          my ($results, $files) = getFiles($where, $offset);
# Line 305  sub displayGrid($$$$) { Line 297  sub displayGrid($$$$) {
297    
298          my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page);          my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page);
299    
300            if ($results <= 0) {
301                    $retHTML .= qq{
302                            <p style="color: red;">No results found...</p>
303                    };
304                    return $retHTML;
305            } else {
306                    # DEBUG
307                    #use Data::Dumper;
308                    #$retHTML .= '<pre>' . Dumper($files) . '</pre>';
309            }
310    
311    
312            if ($addForm) {
313                    $retHTML .= qq{<form name="forma" method="GET" action="$MyURL">};
314                    $retHTML.= qq{<input type="hidden" value="search" name="action">};
315                    $retHTML .= qq{<input type="hidden" value="results" name="search_results">};
316            }
317    
318    
319          $retHTML .= qq{          $retHTML .= qq{
320          <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)
321          <table style="fview" width="100%">          <table style="fview" width="100%">

Legend:
Removed from v.58  
changed lines
  Added in v.72

  ViewVC Help
Powered by ViewVC 1.1.26