/[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 53 by dpavlin, Sat Aug 20 17:19:48 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    
11  my $on_page = 100;  my $on_page = 100;
12  my $pager_pages = 10;  my $pager_pages = 10;
13    
14    my $dsn = $Conf{SearchDSN};
15    my $db_user = $Conf{SearchUser} || '';
16    
17  sub getUnits() {  sub getUnits() {
18      my @ret = ();      my @ret = ();
19      my $tmp;      my $tmp;
20      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 } );  
21      my $st =      my $st =
22        $dbh->prepare(        $dbh->prepare(
23          " SELECT shares.ID AS ID, shares.share AS name FROM shares;");          " SELECT shares.ID AS ID, shares.share AS name FROM shares;");
# Line 27  sub getUnits() { Line 30  sub getUnits() {
30      return @ret;      return @ret;
31  }  }
32    
33    sub epoch_to_iso {
34            my $t = shift || return;
35            my $dt = DateTime->from_epoch( epoch => $t ) || return;
36    print STDERR "$t == ",$dt->epoch,"\n";
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          my $backup_from = mk_iso_date('search_backup', 'from');                          day => $dd
56          push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from);                  );
57          my $backup_to = mk_iso_date('search_backup', 'to');                  return $dt->epoch || 'NULL';
58          push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to);          }
59    
60          my $files_from = mk_iso_date('search', 'from');          my $backup_from = mk_epoch_date('search_backup', 'from');
61          push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from);          push @conditions, qq{ backups.date >= $backup_from } if ($backup_from);
62          my $files_to = mk_iso_date('search', 'to');          my $backup_to = mk_epoch_date('search_backup', 'to');
63          push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to);          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          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            
# 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 81  sub getFiles($$) { Line 95  sub getFiles($$) {
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,                  shares.share||files.fullpath    AS networkPath,
98                  date(files.date, 'unixepoch', 'localtime') AS date,                  files.date                      AS date,
99                  files.type                      AS filetype,                  files.type                      AS filetype,
100                  files.size                      AS size,                  files.size                      AS size,
101                  dvds.name                       AS dvd                  dvds.name                       AS dvd
# Line 122  sub getFiles($$) { Line 136  sub getFiles($$) {
136                          'hname'         => $row->{'hname'},                          'hname'         => $row->{'hname'},
137                          'sname'         => $row->{'sname'},                          'sname'         => $row->{'sname'},
138                          'sharename'     => $row->{'sharename'},                          'sharename'     => $row->{'sharename'},
139                          'backupno'      => $row->{'backupNum'},                          'backupno'      => $row->{'backupnum'},
140                          'fname'         => $row->{'filename'},                          'fname'         => $row->{'filename'},
141                          'fpath'         => $row->{'filepath'},                          'fpath'         => $row->{'filepath'},
142                          'networkpath'   => $row->{'networkPath'},                          'networkpath'   => $row->{'networkpath'},
143                          'date'          => $row->{'date'},                          'date'          => $row->{'date'},
144                          'type'          => $row->{'filetype'},                          'type'          => $row->{'filetype'},
145                          'size'          => $row->{'size'},                          'size'          => $row->{'size'},
# Line 139  sub getFiles($$) { Line 153  sub getFiles($$) {
153          return ($results, \@ret);          return ($results, \@ret);
154  }  }
155    
156  sub getBackupsNotBurned()  sub getBackupsNotBurned() {
157    {  
158        my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",          my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
159          "", "", { RaiseError => 1, AutoCommit => 1 } );                my $sql = q{
160        my $sql = q{          SELECT
161            SELECT                  hosts.ID                AS hostid,
162              hosts.ID         AS hostID,                  min(hosts.name)         AS host,
163              hosts.name       AS host,                  backups.num             AS backupno,
164              backups.num      AS backupno,                  min(backups.type)       AS type,
165              backups.type     AS type,                  min(backups.date)       AS date
166              backups.date     AS date          FROM backups, shares, files, hosts
167            FROM backups, shares, files, hosts          WHERE
168            WHERE                  backups.num     = files.backupNum       AND
169              backups.num    = files.backupNum  AND                  shares.ID       = files.shareID         AND        
170              shares.ID      = files.shareID    AND                            backups.hostID  = shares.hostID         AND
171              backups.hostID = shares.hostID    AND                  hosts.ID        = backups.hostID        AND
172              hosts.ID       = backups.hostID   AND                  files.dvdid     IS NULL
173              files.dvdid    IS NULL          GROUP BY
174            GROUP BY                  backups.hostID, backups.num, hosts.id
175              backups.hostID, backups.num          };
176        };          my $sth = $dbh->prepare( $sql );
177        my $st = $dbh -> prepare( $sql );          my @ret;
178        my @ret = ();          $sth->execute();
179        $st -> execute();  
180            while ( my $row = $sth->fetchrow_hashref() ) {      
181        while ( my $row = $st -> fetchrow_hashref() )                  push(@ret, {
         {            
             push(@ret, {  
182                           'host'     => $row->{'host'},                           'host'     => $row->{'host'},
183                           'hostid'   => $row->{'hostID'},                           'hostid'   => $row->{'hostid'},
184                           'backupno' => $row->{'backupno'},                           'backupno' => $row->{'backupno'},
185                           'type'     => $row->{'type'},                           'type'     => $row->{'type'},
186                           'date'     => $row->{'date'}                           'date'     => $row->{'date'}
187                         }                         }
188              );                  );
189          }          }
190                
191        return @ret;                return @ret;      
192    }  }
193    
194  sub displayBackupsGrid()  sub displayBackupsGrid()
195    {    {
# Line 242  EOF3 Line 254  EOF3
254                  $retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' .                  $retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' .
255                          '<td class="fviewborder">' . $backup->{'backupno'} . '</td>' .                          '<td class="fviewborder">' . $backup->{'backupno'} . '</td>' .
256                          '<td class="fviewborder">' . $backup->{'type'} . '</td>' .                          '<td class="fviewborder">' . $backup->{'type'} . '</td>' .
257                          '<td class="fviewborder">' . $backup->{'date'} . '<td>' .                          '<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '<td>' .
258                          '</tr>';                          '</tr>';
259          }          }
260    
# Line 308  sub displayGrid($$$$) { Line 320  sub displayGrid($$$$) {
320                          $typeStr,                          $typeStr,
321                          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'} ),
322                          $file->{'size'},                          $file->{'size'},
323                          $file->{'date'},                          epoch_to_iso( $file->{'date'} ),
324                          $file->{'dvd'}                          $file->{'dvd'}
325                  )) {                  )) {
326                          $retHTML .= qq{<td class="fviewborder">$v</td>};                          $retHTML .= qq{<td class="fviewborder">$v</td>};

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

  ViewVC Help
Powered by ViewVC 1.1.26