/[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 211 by dpavlin, Sun Oct 16 10:57:55 2005 UTC revision 259 by dpavlin, Mon Dec 12 20:59:58 2005 UTC
# Line 121  sub getWhere($) { Line 121  sub getWhere($) {
121  }  }
122    
123  my $sort_def = {  my $sort_def = {
124          default => 'date_a',          search => {
125          sql => {                  default => 'date_a',
126                  share_d => 'shares.name DESC',                  sql => {
127                  share_a => 'shares.name ASC',                          share_d => 'shares.name DESC',
128                  path_d => 'files.path DESC',                          share_a => 'shares.name ASC',
129                  path_a => 'files.path ASC',                          path_d => 'files.path DESC',
130                  num_d => 'files.backupnum DESC',                          path_a => 'files.path ASC',
131                  num_a => 'files.backupnum ASC',                          num_d => 'files.backupnum DESC',
132                  size_d => 'files.size DESC',                          num_a => 'files.backupnum ASC',
133                  size_a => 'files.size ASC',                          size_d => 'files.size DESC',
134                  date_d => 'files.date DESC',                          size_a => 'files.size ASC',
135                  date_a => 'files.date ASC',                          date_d => 'files.date DESC',
136          },                          date_a => 'files.date ASC',
137          est => {                  },
138                  share_d => 'sname STRD',                  est => {
139                  share_a => 'sname STRA',                          share_d => 'sname STRD',
140                  path_d => 'filepath STRD',                          share_a => 'sname STRA',
141                  path_a => 'filepath STRA',                          path_d => 'filepath STRD',
142                  num_d => 'backupnum NUMD',                          path_a => 'filepath STRA',
143                  num_a => 'backupnum NUMA',                          num_d => 'backupnum NUMD',
144                  size_d => 'size NUMD',                          num_a => 'backupnum NUMA',
145                  size_a => 'size NUMA',                          size_d => 'size NUMD',
146                  date_d => 'date NUMD',                          size_a => 'size NUMA',
147                  date_a => 'date NUMA',                          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($$) {  sub getSort($$$) {
170          my ($type, $sort_order) = @_;          my ($part,$type, $sort_order) = @_;
171    
172          $sort_order ||= $sort_def->{'default'};          die "unknown part: $part" unless ($sort_def->{$part});
173            die "unknown type: $type" unless ($sort_def->{$part}->{$type});
174    
175          die "unknown type: $type" unless ($sort_def->{$type});          $sort_order ||= $sort_def->{$part}->{'default'};
176    
177          if (my $ret = $sort_def->{$type}->{$sort_order}) {          if (my $ret = $sort_def->{$part}->{$type}->{$sort_order}) {
178                  return $ret;                  return $ret;
179          } else {          } else {
180                  # fallback to default sort order                  # fallback to default sort order
181                  return $sort_def->{$type}->{ $sort_def->{'default'} };                  return $sort_def->{$part}->{$type}->{ $sort_def->{$part}->{'default'} };
182          }          }
183  }  }
184    
# Line 193  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('sql', $param->{'sort'});          my $order = getSort('search', 'sql', $param->{'sort'});
216    
217          my $sql_order = qq{          my $sql_order = qq{
218                  ORDER BY $order                  ORDER BY $order
# Line 238  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 295  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( getSort('est', $param->{'sort'} ) );          $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 358  sub get_tgz_size_by_name($) { Line 377  sub get_tgz_size_by_name($) {
377    
378          my $size = -1;          my $size = -1;
379    
380          if (-f $tgz) {          if (-f "${tgz}.tar.gz") {
381                  $size = (stat($tgz))[7];                  $size = (stat("${tgz}.tar.gz"))[7];
382          } elsif (-d $tgz) {          } elsif (-d $tgz) {
383                  opendir(my $dir, $tgz) || die "can't opendir $tgz: $!";                  opendir(my $dir, $tgz) || die "can't opendir $tgz: $!";
384                  my @parts = grep { !/^\./ && -f "$tgz/$_" } readdir($dir);                  my @parts = grep { !/^\./ && !/md5/ && -f "$tgz/$_" } readdir($dir);
385                  $size = 0;                  $size = 0;
386                  foreach my $part (@parts) {                  foreach my $part (@parts) {
387                          $size += (stat("$tgz/$part"))[7] || die "can't stat $tgz/$part: $!";                          $size += (stat("$tgz/$part"))[7] || die "can't stat $tgz/$part: $!";
388                  }                  }
389                  closedir $dir;                  closedir $dir;
390            } else {
391                    return -1;
392          }          }
393    
394          return $size;          return $size;
# Line 399  sub getGzipSize($$) Line 420  sub getGzipSize($$)
420          );          );
421  }  }
422    
423  sub getBackupsNotBurned() {  sub getVolumes($) {
424            my $id = shift;
425    
426            my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
427    
428            my $sth = $dbh->prepare(qq{
429                    select
430                            size
431                    from backup_parts
432                    where backup_id = ?
433                    order by part_nr asc
434            });
435    
436            $sth->execute($id);
437    
438            my $cumulative_size = 0;
439            my $volumes = 1;
440    
441            while(my ($size) = $sth->fetchrow_array) {
442                    if ($cumulative_size + $size > $max_archive_size) {
443                            $volumes++;
444                            $cumulative_size = $size;
445                    } else {
446                            $cumulative_size += $size;
447                    }
448            }
449    
450            return ($volumes,$cumulative_size);
451    }
452    
453    sub getBackupsNotBurned($) {
454    
455            my $param = shift;
456          my $dbh = get_dbh();          my $dbh = get_dbh();
457    
458          my $sql = q{          my $order = getSort('burn', 'sql', $param->{'sort'});
459    
460    print STDERR "## sort=". ($param->{'sort'} || 'no sort param') . " burn sql order: $order\n";
461    
462            my $sql = qq{
463                  SELECT                  SELECT
464                          backups.hostID AS hostID,                          backups.hostID AS hostID,
465                          hosts.name AS host,                          hosts.name AS host,
# Line 411  sub getBackupsNotBurned() { Line 467  sub getBackupsNotBurned() {
467                          backups.num AS backupnum,                          backups.num AS backupnum,
468                          backups.type AS type,                          backups.type AS type,
469                          backups.date AS date,                          backups.date AS date,
470                            date_part('epoch',now()) - backups.date as age,
471                          backups.size AS size,                          backups.size AS size,
472                          backups.id AS id,                          backups.id AS id,
473                          backups.inc_size AS inc_size,                          backups.inc_size AS inc_size,
# Line 432  sub getBackupsNotBurned() { Line 489  sub getBackupsNotBurned() {
489                          backups.size,                          backups.size,
490                          backups.inc_size,                          backups.inc_size,
491                          backups.parts                          backups.parts
492                  ORDER BY backups.date                  ORDER BY $order
493          };          };
494          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
495          my @ret;          my @ret;
496          $sth->execute();          $sth->execute();
497    
498          while ( my $row = $sth->fetchrow_hashref() ) {          while ( my $row = $sth->fetchrow_hashref() ) {
499                  $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );                  $row->{'age'} = sprintf("%0.1f", ( $row->{'age'} / 86400 ) );
500                  $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);                  #$row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );
501    
502                    my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
503                    if ($row->{size} > $max_archive_size) {
504                            ($row->{volumes}, $row->{inc_size_calc}) = getVolumes($row->{id});
505                    }
506    
507                    $row->{size} = sprintf("%0.2f", $row->{size} / 1024 / 1024);
508    
509                  # do some cluster calculation (approximate) and convert to kB                  # do some cluster calculation (approximate)
510                  $row->{'inc_size'} = int(($row->{'inc_size'} + 1023 ) / ( 2 * 1024 ) * 2);                  $row->{inc_size} = int(($row->{inc_size} + 1023 ) / ( 2 * 1024 ) * 2);
511                    $row->{inc_size_calc} ||= $row->{inc_size};
512                  push @ret, $row;                  push @ret, $row;
513          }          }
514                
515          return @ret;                return @ret;
516  }  }
517    
518  sub displayBackupsGrid() {  sub displayBackupsGrid($) {
519    
520            my $param = shift;
521    
522            my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
523            my $max_archive_file_size = $Conf{MaxArchiveFileSize}  || die "no MaxFileInSize";
524    
525          my $retHTML .= q{          my $retHTML .= q{
526                  <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">                  <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">
# Line 558  DIV#fixedBox #submitBurner { Line 628  DIV#fixedBox #submitBurner {
628          background-color: transparent;          background-color: transparent;
629  }  }
630    
631  #parts {  #volumes {
632          padding: 0.4em;          padding: 0.4em;
633          display: none;          display: none;
634          width: 100%;          width: 100%;
# Line 575  var debug_div = null; Line 645  var debug_div = null;
645  EOF3  EOF3
646    
647          # take maximum archive size from configuration          # take maximum archive size from configuration
648          $retHTML .= 'var media_size = '. $Conf{MaxArchiveSize} .';';          $retHTML .= qq{
649    var media_size = $max_archive_size ;
650    var max_file_size = $max_archive_file_size;
651    
652    };
653    
654          $retHTML .= <<'EOF3';          $retHTML .= <<'EOF3';
655    
# Line 638  function checkAll(location) { Line 712  function checkAll(location) {
712    
713  function update_sum(suma, suma_disp) {  function update_sum(suma, suma_disp) {
714          if (! suma_disp) suma_disp = suma;          if (! suma_disp) suma_disp = suma;
715          element_id('forma').elements['totalsize'].value = suma_disp;          suma_disp = Math.floor(suma_disp / 1024);
716            element_id('forma').elements['totalsize_kb'].value = suma_disp;
717            element_id('forma').elements['totalsize'].value = suma;
718          pbar_set(suma, media_size);          pbar_set(suma, media_size);
719          debug('total size: ' + suma);          debug('total size: ' + suma);
720  }  }
# Line 654  function sumiraj(e) { Line 730  function sumiraj(e) {
730                          suma -= size;                          suma -= size;
731                  }                  }
732    
733                  var parts = parseInt( element_id("prt" + e.name.substr(3)).value);                  var volumes = parseInt( element_id("prt" + e.name.substr(3)).value);
734                  if (suma > media_size && suma == size && parts > 1) {                  if (volumes > 1) {
735                          element_id("parts").innerHTML = "This will take "+parts+" mediums!";                          if (e.checked) {
736                          element_id("parts").style.display = 'block';                                  element_id("volumes").innerHTML = "This will take "+volumes+" mediums!";
737                          update_sum(media_size, suma);                                  element_id("volumes").style.display = 'block';
738                          suma = media_size;                                  suma = size;
739                          return suma;                                  update_sum(suma);
740                  } else {                                  return suma;
741                          element_id("parts").style.display = 'none';                          } else {
742                                    suma -= size;
743                                    element_id("volumes").style.display = 'none';
744                            }
745                  }                  }
746    
747                  if (suma < 0) suma = 0;                  if (suma < 0) suma = 0;
# Line 754  self.onload = self.document.onload = sel Line 833  self.onload = self.document.onload = sel
833  </script>  </script>
834  <div id="fixedBox">  <div id="fixedBox">
835    
836  Size: <input type="text" name="totalsize" size="7" readonly="readonly" style="text-align:right;" value="0" /> kB  <input type="hidden" name="totalsize"/>
837    Size: <input type="text" name="totalsize_kb" size="7" readonly="readonly" style="text-align:right;" value="0" /> kB
838    
839  <div id="mContainer">  <div id="mContainer">
840          <div id="gradient">&nbsp;</div>          <div id="gradient">&nbsp;</div>
# Line 763  Size: <input type="text" name="totalsize Line 843  Size: <input type="text" name="totalsize
843  </div>  </div>
844  <br/>  <br/>
845    
846  <div id="parts">&nbsp;</div>  <div id="volumes">&nbsp;</div>
847    
848  Note:  Note:
849  <textarea name="note" cols="10" rows="5" id="note"></textarea>  <textarea name="note" cols="10" rows="5" id="note"></textarea>
# Line 785  EOF3 Line 865  EOF3
865                          <td class="tableheader">                          <td class="tableheader">
866                                  <input type="checkbox" name="allFiles" id="allFiles" onClick="checkAll('allFiles');">                                  <input type="checkbox" name="allFiles" id="allFiles" onClick="checkAll('allFiles');">
867                          </td>                          </td>
868                          <td align="center">Share</td>          } .
869                          <td align="center">Backup no</td>                  sort_header($param, 'Share', 'share', 'center') .
870                    sort_header($param, '#', 'num', 'center') .
871            qq{
872                          <td align="center">Type</td>                          <td align="center">Type</td>
873                          <td align="center">date</td>          } .
874                          <td align="center">age/days</td>                  sort_header($param, 'Date', 'date', 'center') .
875                          <td align="center">size/MB</td>                  sort_header($param, 'Age/days', 'age', 'center') .
876                          <td align="center">gzip size/kB</td>                  sort_header($param, 'Size/Mb', 'size', 'center') .
877                          </tr>                  sort_header($param, 'gzip size/Kb', 'incsize', 'center') .
878            qq{
879                            <td align="center">medias</td></tr>
880          };          };
881    
882          my @color = (' bgcolor="#e0e0e0"', '');          my @color = (' bgcolor="#e0e0e0"', '');
# Line 801  EOF3 Line 884  EOF3
884          my $i = 0;          my $i = 0;
885          my $host = '';          my $host = '';
886    
887          foreach my $backup ( getBackupsNotBurned() ) {          foreach my $backup ( getBackupsNotBurned($param) ) {
888    
889                  if ($host ne $backup->{'host'}) {                  if ($host ne $backup->{'host'}) {
890                          $i++;                          $i++;
# Line 820  EOF3 Line 903  EOF3
903                          <input type="checkbox" name="fcb' . $checkbox_key . '" value="' . $checkbox_key . '" onClick="sumiraj(this);">';                          <input type="checkbox" name="fcb' . $checkbox_key . '" value="' . $checkbox_key . '" onClick="sumiraj(this);">';
904                  }                  }
905    
906                    my $img_url = $Conf{CgiImageDirURL};
907    
908                  $retHTML .=                  $retHTML .=
909                          '</td>' .                          '</td>' .
910                          '<td align="right">' . $backup->{'host'} . ':' . $backup->{'share'} . '</td>' .                          '<td align="right">' . $backup->{'host'} . ':' . $backup->{'share'} . '</td>' .
# Line 829  EOF3 Line 914  EOF3
914                          '<td align="center">' . $backup->{'age'} . '</td>' .                          '<td align="center">' . $backup->{'age'} . '</td>' .
915                          '<td align="right">' . $backup->{'size'} . '</td>' .                          '<td align="right">' . $backup->{'size'} . '</td>' .
916                          '<td align="right">' . $backup->{'inc_size'} .                          '<td align="right">' . $backup->{'inc_size'} .
917                          '<input type="hidden" id="fss'.$checkbox_key .'" value="'. $backup->{'inc_size'} .'"></td>' .                          '<input type="hidden" id="fss'.$checkbox_key .'" value="'. $backup->{'inc_size_calc'} .'"></td>' .
918                          '<input type="hidden" id="prt'.$checkbox_key .'" value="'. $backup->{'parts'} .'"></td>' .                          '<input type="hidden" id="prt'.$checkbox_key .'" value="'. $backup->{'volumes'} .'"></td>' .
919                            '<td align="left">' . ( qq{<img src="$img_url/icon-cd.gif" alt="media">} x $backup->{volumes} ) . '</td>' .
920    
921                          "</tr>\n";                          "</tr>\n";
922          }          }
# Line 884  sub displayGrid($) { Line 970  sub displayGrid($) {
970                  <td></td>                  <td></td>
971          };          };
972    
973          my ($sort_what, $sort_dir) = split(/_/,$param->{'sort'},2);          sub sort_header($$$$) {
974                    my ($param, $display, $name, $align) = @_;
975    
976                    my ($sort_what, $sort_direction) = split(/_/,$param->{'sort'},2);
977    
978          sub sort_header($$$) {                  my $old_sort = $param->{'sort'};
979                  my ($param, $display, $name) = @_;  
980                    my $html = qq{<td align="$align"};
981                    my $arrow = '';
982    
                 my $html = '<td align="center"';  
983                  if (lc($sort_what) eq lc($name)) {                  if (lc($sort_what) eq lc($name)) {
984                          my $dir = lc($sort_dir);                          my $direction = lc($sort_direction);
985                          $dir =~ tr/ad/da/;  
986                          $param->{'sort'} = $name . '_' . $dir;                          # swap direction or fallback to default
987                            $direction =~ tr/ad/da/;
988                            $direction = 'a' unless ($direction =~ /[ad]/);
989    
990                            $param->{'sort'} = $name . '_' . $direction;
991                          $html .= ' style="border: 1px solid #808080;"';                          $html .= ' style="border: 1px solid #808080;"';
992                    
993                            # add unicode arrow for direction
994                            $arrow .= '&nbsp;';
995                            $arrow .= $direction eq 'a'  ?  '&#9650;'
996                                    : $direction eq 'd'  ?  '&#9660;'
997                                    :                       ''
998                                    ;
999    
1000                  } else {                  } else {
1001                          $param->{'sort'} = $name . '_a';                          $param->{'sort'} = $name . '_a';
1002                  }                  }
1003                  $html .= '<a href="' . page_uri($param) . '">' . $display . '</a></td>';  
1004                    $html .= '><a href="' . page_uri($param) . '">' . $display . '</a>' . $arrow . '</td>';
1005                    $param->{'sort'} = $old_sort;
1006    
1007                  return $html;                  return $html;
1008          }          }
1009    
1010          $retHTML .=          $retHTML .=
1011                  sort_header($param, 'Share', 'share') .                  sort_header($param, 'Share', 'share', 'center') .
1012                  sort_header($param, 'Type and Name', 'path') .                  sort_header($param, 'Type and Name', 'path', 'center') .
1013                  sort_header($param, '#', 'num') .                  sort_header($param, '#', 'num', 'center') .
1014                  sort_header($param, 'Size', 'size') .                  sort_header($param, 'Size', 'size', 'center') .
1015                  sort_header($param, 'Date', 'date');                  sort_header($param, 'Date', 'date', 'center');
1016    
1017          $retHTML .= qq{          $retHTML .= qq{
1018                  <td align="center">Media</td>                  <td align="center">Media</td>

Legend:
Removed from v.211  
changed lines
  Added in v.259

  ViewVC Help
Powered by ViewVC 1.1.26