/[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 186 by dpavlin, Wed Oct 12 12:53:27 2005 UTC revision 254 by dpavlin, Mon Dec 12 16:07:27 2005 UTC
# Line 8  use DBI; Line 8  use DBI;
8  use DateTime;  use DateTime;
9  use vars qw(%In $MyURL);  use vars qw(%In $MyURL);
10  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
11    use XML::Writer;
12    use IO::File;
13    
14  my $on_page = 100;  my $on_page = 100;
15  my $pager_pages = 10;  my $pager_pages = 10;
# Line 118  sub getWhere($) { Line 120  sub getWhere($) {
120          return join(" and ", @conditions);          return join(" and ", @conditions);
121  }  }
122    
123    my $sort_def = {
124            search => {
125                    default => 'date_a',
126                    sql => {
127                            share_d => 'shares.name DESC',
128                            share_a => 'shares.name ASC',
129                            path_d => 'files.path DESC',
130                            path_a => 'files.path ASC',
131                            num_d => 'files.backupnum DESC',
132                            num_a => 'files.backupnum ASC',
133                            size_d => 'files.size DESC',
134                            size_a => 'files.size ASC',
135                            date_d => 'files.date DESC',
136                            date_a => 'files.date ASC',
137                    },
138                    est => {
139                            share_d => 'sname STRD',
140                            share_a => 'sname STRA',
141                            path_d => 'filepath STRD',
142                            path_a => 'filepath STRA',
143                            num_d => 'backupnum NUMD',
144                            num_a => 'backupnum NUMA',
145                            size_d => 'size NUMD',
146                            size_a => 'size NUMA',
147                            date_d => 'date NUMD',
148                            date_a => 'date NUMA',
149                    }
150            }, burn => {
151                    default => 'date_a',
152                    sql => {
153                            share_d => 'host DESC, share DESC',
154                            share_a => 'host ASC, share ASC',
155                            num_d => 'backupnum DESC',
156                            num_a => 'backupnum ASC',
157                            date_d => 'date DESC',
158                            date_a => 'date ASC',
159                            age_d => 'age DESC',
160                            age_a => 'age ASC',
161                            size_d => 'size DESC',
162                            size_a => 'size ASC',
163                            incsize_d => 'inc_size DESC',
164                            incsize_a => 'inc_size ASC',
165                    }
166            }
167    };
168    
169    sub getSort($$$) {
170            my ($part,$type, $sort_order) = @_;
171    
172            die "unknown part: $part" unless ($sort_def->{$part});
173            die "unknown type: $type" unless ($sort_def->{$part}->{$type});
174    
175            $sort_order ||= $sort_def->{$part}->{'default'};
176    
177            if (my $ret = $sort_def->{$part}->{$type}->{$sort_order}) {
178                    return $ret;
179            } else {
180                    # fallback to default sort order
181                    return $sort_def->{$part}->{$type}->{ $sort_def->{$part}->{'default'} };
182            }
183    }
184    
185  sub getFiles($) {  sub getFiles($) {
186          my ($param) = @_;          my ($param) = @_;
# Line 149  sub getFiles($) { Line 212  sub getFiles($) {
212          my $where = getWhere($param);          my $where = getWhere($param);
213          $sql_where = " WHERE ". $where if ($where);          $sql_where = " WHERE ". $where if ($where);
214    
215            my $order = getSort('search', 'sql', $param->{'sort'});
216    
217          my $sql_order = qq{          my $sql_order = qq{
218                  ORDER BY files.date                  ORDER BY $order
219                  LIMIT $on_page                  LIMIT $on_page
220                  OFFSET ?                  OFFSET ?
221          };          };
# Line 192  sub getHyperEstraier_url($) { Line 257  sub getHyperEstraier_url($) {
257          if ($use_hest =~ m#^http://#) {          if ($use_hest =~ m#^http://#) {
258                  $index_node_url = $use_hest;                  $index_node_url = $use_hest;
259          } else {          } else {
260                  $index_path = $TopDir . '/' . $index_path;                  $index_path = $TopDir . '/' . $use_hest;
261                  $index_path =~ s#//#/#g;                  $index_path =~ s#//#/#g;
262          }          }
263          return ($index_path, $index_node_url);          return ($index_path, $index_node_url);
# Line 249  sub getFilesHyperEstraier($) { Line 314  sub getFilesHyperEstraier($) {
314    
315  #       $cond->set_max( $offset + $on_page );  #       $cond->set_max( $offset + $on_page );
316          $cond->set_options( $HyperEstraier::Condition::SURE );          $cond->set_options( $HyperEstraier::Condition::SURE );
317          $cond->set_order( 'date NUMA' );          $cond->set_order( getSort('search', 'est', $param->{'sort'} ) );
318    
319          # get the result of search          # get the result of search
320          my @res;          my @res;
# Line 280  sub getFilesHyperEstraier($) { Line 345  sub getFilesHyperEstraier($) {
345                  }                  }
346    
347                  my $row;                  my $row;
348                  foreach my $c (qw/fid hname sname backupnum fiilename filepath date type size/) {                  foreach my $c (qw/fid hname sname backupnum filepath date type size/) {
349                          $row->{$c} = $doc->attr($c);                          $row->{$c} = $doc->attr($c);
350                  }                  }
351                  push @res, $row;                  push @res, $row;
# Line 305  sub getGzipName($$$) Line 370  sub getGzipName($$$)
370                    
371  }  }
372    
373    sub get_tgz_size_by_name($) {
374            my $name = shift;
375    
376            my $tgz = $Conf{InstallDir}.'/'.$Conf{GzipTempDir}.'/'.$name;
377    
378            my $size = -1;
379    
380            if (-f "${tgz}.tar.gz") {
381                    $size = (stat("${tgz}.tar.gz"))[7];
382            } elsif (-d $tgz) {
383                    opendir(my $dir, $tgz) || die "can't opendir $tgz: $!";
384                    my @parts = grep { !/^\./ && !/md5/ && -f "$tgz/$_" } readdir($dir);
385                    $size = 0;
386                    foreach my $part (@parts) {
387                            $size += (stat("$tgz/$part"))[7] || die "can't stat $tgz/$part: $!";
388                    }
389                    closedir $dir;
390            } else {
391                    return -1;
392            }
393    
394            return $size;
395    }
396    
397  sub getGzipSize($$)  sub getGzipSize($$)
398  {  {
399          my ($hostID, $backupNum) = @_;          my ($hostID, $backupNum) = @_;
         my $ret;  
400          my $sql;          my $sql;
401          my $dbh = get_dbh();          my $dbh = get_dbh();
402                    
# Line 326  sub getGzipSize($$) Line 414  sub getGzipSize($$)
414          $sth->execute($hostID, $backupNum);          $sth->execute($hostID, $backupNum);
415    
416          my $row = $sth->fetchrow_hashref();          my $row = $sth->fetchrow_hashref();
417            
418          my (undef,undef,undef,undef,undef,undef,undef,$ret,undef,undef,undef,undef,undef) =          return get_tgz_size_by_name(
419                          stat( $Conf{InstallDir}.'/'.$Conf{GzipTempDir}.'/'.                  getGzipName($row->{'host'}, $row->{share}, $row->{'backupnum'})
420                                  getGzipName($row->{'host'}, $row->{share}, $row->{'backupnum'}));          );
           
         return $ret;      
421  }  }
422    
423  sub getBackupsNotBurned() {  sub getBackupsNotBurned($) {
424    
425            my $param = shift;
426          my $dbh = get_dbh();          my $dbh = get_dbh();
427    
428          my $sql = q{          my $order = getSort('burn', 'sql', $param->{'sort'});
429    
430    print STDERR "## sort=". ($param->{'sort'} || 'no sort param') . " burn sql order: $order\n";
431    
432            my $sql = qq{
433                  SELECT                  SELECT
434                          backups.hostID AS hostID,                          backups.hostID AS hostID,
435                          hosts.name AS host,                          hosts.name AS host,
# Line 346  sub getBackupsNotBurned() { Line 437  sub getBackupsNotBurned() {
437                          backups.num AS backupnum,                          backups.num AS backupnum,
438                          backups.type AS type,                          backups.type AS type,
439                          backups.date AS date,                          backups.date AS date,
440                            date_part('epoch',now()) - backups.date as age,
441                          backups.size AS size,                          backups.size AS size,
442                          backups.id AS id,                          backups.id AS id,
443                          backups.inc_size AS inc_size                          backups.inc_size AS inc_size,
444                            backups.parts AS parts
445                  FROM backups                  FROM backups
446                  INNER JOIN shares       ON backups.shareID=shares.ID                  INNER JOIN shares       ON backups.shareID=shares.ID
447                  INNER JOIN hosts        ON backups.hostID = hosts.ID                  INNER JOIN hosts        ON backups.hostID = hosts.ID
# Line 364  sub getBackupsNotBurned() { Line 457  sub getBackupsNotBurned() {
457                          backups.type,                          backups.type,
458                          backups.date,                          backups.date,
459                          backups.size,                          backups.size,
460                          backups.inc_size                          backups.inc_size,
461                  ORDER BY backups.date                          backups.parts
462                    ORDER BY $order
463          };          };
464          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
465          my @ret;          my @ret;
466          $sth->execute();          $sth->execute();
467    
468          while ( my $row = $sth->fetchrow_hashref() ) {          while ( my $row = $sth->fetchrow_hashref() ) {
469                  $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );                  $row->{'age'} = sprintf("%0.1f", ( $row->{'age'} / 86400 ) );
470                    #$row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );
471                  $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);                  $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);
472    
473                  # do some cluster calculation (approximate) and convert to kB                  # do some cluster calculation (approximate) and convert to kB
# Line 383  sub getBackupsNotBurned() { Line 478  sub getBackupsNotBurned() {
478          return @ret;                return @ret;      
479  }  }
480    
481  sub displayBackupsGrid() {  sub displayBackupsGrid($) {
482    
483            my $param = shift;
484    
485            my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
486            my $max_archive_file_size = $Conf{MaxArchiveFileSize}  || die "no MaxFileInSize";
487    
488          my $retHTML .= q{          my $retHTML .= q{
489                  <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">                  <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">
# Line 490  DIV#fixedBox #submitBurner { Line 590  DIV#fixedBox #submitBurner {
590          z-index: 3;          z-index: 3;
591          background-color: transparent;          background-color: transparent;
592  }  }
593    
594    #parts {
595            padding: 0.4em;
596            display: none;
597            width: 100%;
598            font-size: 80%;
599            color: #ff0000;
600            text-align: center;
601    }
602  -->  -->
603  </style>  </style>
604  <script type="text/javascript">  <script type="text/javascript">
# Line 499  var debug_div = null; Line 608  var debug_div = null;
608  EOF3  EOF3
609    
610          # take maximum archive size from configuration          # take maximum archive size from configuration
611          $retHTML .= 'var media_size = '. $Conf{MaxArchiveSize} .';';          $retHTML .= qq{
612    var media_size = $max_archive_size ;
613    var max_file_size = $max_archive_file_size;
614    
615    };
616    
617          $retHTML .= <<'EOF3';          $retHTML .= <<'EOF3';
618    
619  function debug(msg) {  function debug(msg) {
620  //      return; // Disable debugging          return; // Disable debugging
621    
622          if (! debug_div) debug_div = document.getElementById('debug');          if (! debug_div) debug_div = document.getElementById('debug');
623    
# Line 560  function checkAll(location) { Line 673  function checkAll(location) {
673          update_sum(suma);          update_sum(suma);
674  }  }
675    
676  function update_sum(suma) {  function update_sum(suma, suma_disp) {
677          element_id('forma').elements['totalsize'].value = suma;          if (! suma_disp) suma_disp = suma;
678            element_id('forma').elements['totalsize'].value = suma_disp;
679          pbar_set(suma, media_size);          pbar_set(suma, media_size);
680          debug('total size: ' + suma);          debug('total size: ' + suma);
681  }  }
# Line 576  function sumiraj(e) { Line 690  function sumiraj(e) {
690                  } else {                  } else {
691                          suma -= size;                          suma -= size;
692                  }                  }
693    
694                    var parts = parseInt( element_id("prt" + e.name.substr(3)).value);
695                    if (suma > max_file_size && suma == size && parts > 1) {
696                            element_id("parts").innerHTML = "This will take "+parts+" mediums!";
697                            element_id("parts").style.display = 'block';
698                            update_sum(media_size, suma);
699                            suma = media_size;
700                            return suma;
701                    } else {
702                            element_id("parts").style.display = 'none';
703                    }
704    
705                    if (suma < 0) suma = 0;
706          } else {          } else {
707                  suma = 0;                  suma = 0;
708                  for (var i = 0; i < len; i++) {                  for (var i = 0; i < len; i++) {
# Line 673  Size: <input type="text" name="totalsize Line 800  Size: <input type="text" name="totalsize
800  </div>  </div>
801  <br/>  <br/>
802    
803    <div id="parts">&nbsp;</div>
804    
805  Note:  Note:
806  <textarea name="note" cols="10" rows="5" id="note"></textarea>  <textarea name="note" cols="10" rows="5" id="note"></textarea>
807    
# Line 693  EOF3 Line 822  EOF3
822                          <td class="tableheader">                          <td class="tableheader">
823                                  <input type="checkbox" name="allFiles" id="allFiles" onClick="checkAll('allFiles');">                                  <input type="checkbox" name="allFiles" id="allFiles" onClick="checkAll('allFiles');">
824                          </td>                          </td>
825                          <td align="center">Share</td>          } .
826                          <td align="center">Backup no</td>                  sort_header($param, 'Share', 'share', 'center') .
827                    sort_header($param, '#', 'num', 'center') .
828            qq{
829                          <td align="center">Type</td>                          <td align="center">Type</td>
830                          <td align="center">date</td>          } .
831                          <td align="center">age/days</td>                  sort_header($param, 'Date', 'date', 'center') .
832                          <td align="center">size/MB</td>                  sort_header($param, 'Age/days', 'age', 'center') .
833                          <td align="center">gzip size/kB</td>                  sort_header($param, 'Size/Mb', 'size', 'center') .
834                    sort_header($param, 'gzip size/Kb', 'incsize', 'center') .
835            qq{
836                          </tr>                          </tr>
   
837          };          };
838    
839          my @color = (' bgcolor="#e0e0e0"', '');          my @color = (' bgcolor="#e0e0e0"', '');
# Line 709  EOF3 Line 841  EOF3
841          my $i = 0;          my $i = 0;
842          my $host = '';          my $host = '';
843    
844          foreach my $backup ( getBackupsNotBurned() ) {          foreach my $backup ( getBackupsNotBurned($param) ) {
845    
846                  if ($host ne $backup->{'host'}) {                  if ($host ne $backup->{'host'}) {
847                          $i++;                          $i++;
# Line 737  EOF3 Line 869  EOF3
869                          '<td align="center">' . $backup->{'age'} . '</td>' .                          '<td align="center">' . $backup->{'age'} . '</td>' .
870                          '<td align="right">' . $backup->{'size'} . '</td>' .                          '<td align="right">' . $backup->{'size'} . '</td>' .
871                          '<td align="right">' . $backup->{'inc_size'} .                          '<td align="right">' . $backup->{'inc_size'} .
872                          '<input type="hidden" iD="fss'.$checkbox_key .'" value="'. $backup->{'inc_size'} .'"></td>' .                          '<input type="hidden" id="fss'.$checkbox_key .'" value="'. $backup->{'inc_size'} .'"></td>' .
873                            '<input type="hidden" id="prt'.$checkbox_key .'" value="'. $backup->{'parts'} .'"></td>' .
874    
875                          "</tr>\n";                          "</tr>\n";
876          }          }
# Line 789  sub displayGrid($) { Line 922  sub displayGrid($) {
922          <table style="fview" width="100%" border="0" cellpadding="2" cellspacing="0">          <table style="fview" width="100%" border="0" cellpadding="2" cellspacing="0">
923                  <tr class="fviewheader">                  <tr class="fviewheader">
924                  <td></td>                  <td></td>
925                  <td align="center">Share</td>          };
926                  <td align="center">Type and Name</td>  
927                  <td align="center">#</td>          sub sort_header($$$$) {
928                  <td align="center">Size</td>                  my ($param, $display, $name, $align) = @_;
929                  <td align="center">Date</td>  
930                    my ($sort_what, $sort_direction) = split(/_/,$param->{'sort'},2);
931    
932                    my $old_sort = $param->{'sort'};
933    
934                    my $html = qq{<td align="$align"};
935                    my $arrow = '';
936    
937                    if (lc($sort_what) eq lc($name)) {
938                            my $direction = lc($sort_direction);
939    
940                            # swap direction or fallback to default
941                            $direction =~ tr/ad/da/;
942                            $direction = 'a' unless ($direction =~ /[ad]/);
943    
944                            $param->{'sort'} = $name . '_' . $direction;
945                            $html .= ' style="border: 1px solid #808080;"';
946                    
947                            # add unicode arrow for direction
948                            $arrow .= '&nbsp;';
949                            $arrow .= $direction eq 'a'  ?  '&#9650;'
950                                    : $direction eq 'd'  ?  '&#9660;'
951                                    :                       ''
952                                    ;
953    
954                    } else {
955                            $param->{'sort'} = $name . '_a';
956                    }
957    
958                    $html .= '><a href="' . page_uri($param) . '">' . $display . '</a>' . $arrow . '</td>';
959                    $param->{'sort'} = $old_sort;
960    
961                    return $html;
962            }
963    
964            $retHTML .=
965                    sort_header($param, 'Share', 'share', 'center') .
966                    sort_header($param, 'Type and Name', 'path', 'center') .
967                    sort_header($param, '#', 'num', 'center') .
968                    sort_header($param, 'Size', 'size', 'center') .
969                    sort_header($param, 'Date', 'date', 'center');
970    
971            $retHTML .= qq{
972                  <td align="center">Media</td>                  <td align="center">Media</td>
973                  </tr>                  </tr>
974          };          };
# Line 813  sub displayGrid($) { Line 988  sub displayGrid($) {
988                  return sprintf(qq{<a href="?action=%s&host=%s&num=%d&share=%s&dir=%s">%s</a>}, $action, @_);                  return sprintf(qq{<a href="?action=%s&host=%s&num=%d&share=%s&dir=%s">%s</a>}, $action, @_);
989          }          }
990    
991            my $sth_archived;
992            my %archived_cache;
993    
994            sub check_archived($$$) {
995                    my ($host, $share, $num) = @_;
996    
997                    if (my $html = $archived_cache{"$host $share $num"}) {
998                            return $html;
999                    }
1000    
1001                    $sth_archived ||= $dbh->prepare(qq{
1002                            select
1003                                    dvd_nr, note,
1004                                    count(archive_burned.copy) as copies
1005                            from archive
1006                            inner join archive_burned on archive_burned.archive_id = archive.id
1007                            inner join archive_backup on archive.id = archive_backup.archive_id
1008                            inner join backups on backups.id = archive_backup.backup_id
1009                            inner join hosts on hosts.id = backups.hostid
1010                            inner join shares on shares.id = backups.shareid
1011                            where hosts.name = ? and shares.name = ? and backups.num = ?
1012                            group by dvd_nr, note
1013                    });
1014    
1015                    my @mediums;
1016    
1017                    $sth_archived->execute($host, $share, $num);
1018                    while (my $row = $sth_archived->fetchrow_hashref()) {
1019                            push @mediums, '<abbr title="' .
1020                                    $row->{'note'} .
1021                                    ' [' . $row->{'copies'} . ']' .
1022                                    '">' .$row->{'dvd_nr'} .
1023                                    '</abbr>';
1024                    }
1025    
1026                    my $html = join(", ",@mediums);
1027                    $archived_cache{"$host $share $num"} = $html;
1028                    return $html;
1029            }
1030    
1031          my $i = $offset * $on_page;          my $i = $offset * $on_page;
1032    
1033          foreach $file (@{ $files }) {          foreach $file (@{ $files }) {
# Line 829  sub displayGrid($) { Line 1044  sub displayGrid($) {
1044                          qq{<td class="fviewborder" align="center">} . restore_link( $typeStr, ${EscURI( $file->{'hname'} )}, $file->{'backupnum'}, ${EscURI( $file->{'sname'})}, ${EscURI( $file->{'filepath'} )}, $file->{'backupnum'} ) . qq{</td>} .                          qq{<td class="fviewborder" align="center">} . restore_link( $typeStr, ${EscURI( $file->{'hname'} )}, $file->{'backupnum'}, ${EscURI( $file->{'sname'})}, ${EscURI( $file->{'filepath'} )}, $file->{'backupnum'} ) . qq{</td>} .
1045                          qq{<td class="fviewborder" align="right">} . $file->{'size'} . qq{</td>} .                          qq{<td class="fviewborder" align="right">} . $file->{'size'} . qq{</td>} .
1046                          qq{<td class="fviewborder">} . epoch_to_iso( $file->{'date'} ) . qq{</td>} .                          qq{<td class="fviewborder">} . epoch_to_iso( $file->{'date'} ) . qq{</td>} .
1047                          qq{<td class="fviewborder">} . '?' . qq{</td>};                          qq{<td class="fviewborder">} . check_archived( $file->{'hname'}, $file->{'sname'}, $file->{'backupnum'} ) . qq{</td>};
1048    
1049                  $retHTML .= "</tr>";                  $retHTML .= "</tr>";
1050          }          }
# Line 844  sub displayGrid($) { Line 1059  sub displayGrid($) {
1059          my $max_page = int( $results / $on_page );          my $max_page = int( $results / $on_page );
1060          my $page = 0;          my $page = 0;
1061    
1062          sub page_link($$$) {          sub page_uri($) {
1063                  my ($param,$page,$display) = @_;                  my $param = shift || die "no param?";
   
                 $param->{'offset'} = $page;  
1064    
1065                  my $html = '<a href = "' . $MyURL;                  my $uri = $MyURL;
1066                  my $del = '?';                  my $del = '?';
1067                  foreach my $k (keys %{ $param }) {                  foreach my $k (keys %{ $param }) {
1068                          if ($param->{$k}) {                          if ($param->{$k}) {
1069                                  $html .= $del . $k . '=' . ${EscURI( $param->{$k} )};                                  $uri .= $del . $k . '=' . ${EscURI( $param->{$k} )};
1070                                  $del = '&';                                  $del = '&';
1071                          }                          }
1072                  }                  }
1073                  $html .= '">' . $display . '</a>';                  return $uri;
1074            }
1075    
1076            sub page_link($$$) {
1077                    my ($param,$page,$display) = @_;
1078    
1079                    $param->{'offset'} = $page if (defined($page));
1080    
1081                    my $html = '<a href = "' . page_uri($param) . '">' . $display . '</a>';
1082          }          }
1083    
1084          $retHTML .= '<div style="text-align: center;">';          $retHTML .= '<div style="text-align: center;">';

Legend:
Removed from v.186  
changed lines
  Added in v.254

  ViewVC Help
Powered by ViewVC 1.1.26