/[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 225 by dpavlin, Mon Oct 24 14:02:00 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 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) {
381                    $size = (stat($tgz))[7];
382            } elsif (-d $tgz) {
383                    opendir(my $dir, $tgz) || die "can't opendir $tgz: $!";
384                    my @parts = grep { !/^\./ && -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            }
391    
392            return $size;
393    }
394    
395  sub getGzipSize($$)  sub getGzipSize($$)
396  {  {
397          my ($hostID, $backupNum) = @_;          my ($hostID, $backupNum) = @_;
         my $ret;  
398          my $sql;          my $sql;
399          my $dbh = get_dbh();          my $dbh = get_dbh();
400                    
# Line 326  sub getGzipSize($$) Line 412  sub getGzipSize($$)
412          $sth->execute($hostID, $backupNum);          $sth->execute($hostID, $backupNum);
413    
414          my $row = $sth->fetchrow_hashref();          my $row = $sth->fetchrow_hashref();
415            
416          my (undef,undef,undef,undef,undef,undef,undef,$ret,undef,undef,undef,undef,undef) =          return get_tgz_size_by_name(
417                          stat( $Conf{InstallDir}.'/'.$Conf{GzipTempDir}.'/'.                  getGzipName($row->{'host'}, $row->{share}, $row->{'backupnum'})
418                                  getGzipName($row->{'host'}, $row->{share}, $row->{'backupnum'}));          );
           
         return $ret;      
419  }  }
420    
421  sub getBackupsNotBurned() {  sub getBackupsNotBurned($) {
422    
423            my $param = shift;
424          my $dbh = get_dbh();          my $dbh = get_dbh();
425    
426          my $sql = q{          my $order = getSort('burn', 'sql', $param->{'sort'});
427    
428    print STDERR "## sort=". ($param->{'sort'} || 'no sort param') . " burn sql order: $order\n";
429    
430            my $sql = qq{
431                  SELECT                  SELECT
432                          backups.hostID AS hostID,                          backups.hostID AS hostID,
433                          hosts.name AS host,                          hosts.name AS host,
# Line 346  sub getBackupsNotBurned() { Line 435  sub getBackupsNotBurned() {
435                          backups.num AS backupnum,                          backups.num AS backupnum,
436                          backups.type AS type,                          backups.type AS type,
437                          backups.date AS date,                          backups.date AS date,
438                            date_part('epoch',now()) - backups.date as age,
439                          backups.size AS size,                          backups.size AS size,
440                          backups.id AS id,                          backups.id AS id,
441                          backups.inc_size AS inc_size                          backups.inc_size AS inc_size,
442                            backups.parts AS parts
443                  FROM backups                  FROM backups
444                  INNER JOIN shares       ON backups.shareID=shares.ID                  INNER JOIN shares       ON backups.shareID=shares.ID
445                  INNER JOIN hosts        ON backups.hostID = hosts.ID                  INNER JOIN hosts        ON backups.hostID = hosts.ID
# Line 364  sub getBackupsNotBurned() { Line 455  sub getBackupsNotBurned() {
455                          backups.type,                          backups.type,
456                          backups.date,                          backups.date,
457                          backups.size,                          backups.size,
458                          backups.inc_size                          backups.inc_size,
459                  ORDER BY backups.date                          backups.parts
460                    ORDER BY $order
461          };          };
462          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
463          my @ret;          my @ret;
464          $sth->execute();          $sth->execute();
465    
466          while ( my $row = $sth->fetchrow_hashref() ) {          while ( my $row = $sth->fetchrow_hashref() ) {
467                  $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );                  $row->{'age'} = sprintf("%0.1f", ( $row->{'age'} / 86400 ) );
468                    #$row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );
469                  $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);                  $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);
470    
471                  # do some cluster calculation (approximate) and convert to kB                  # do some cluster calculation (approximate) and convert to kB
# Line 383  sub getBackupsNotBurned() { Line 476  sub getBackupsNotBurned() {
476          return @ret;                return @ret;      
477  }  }
478    
479  sub displayBackupsGrid() {  sub displayBackupsGrid($) {
480    
481            my $param = shift;
482    
483          my $retHTML .= q{          my $retHTML .= q{
484                  <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 585  DIV#fixedBox #submitBurner {
585          z-index: 3;          z-index: 3;
586          background-color: transparent;          background-color: transparent;
587  }  }
588    
589    #parts {
590            padding: 0.4em;
591            display: none;
592            width: 100%;
593            font-size: 80%;
594            color: #ff0000;
595            text-align: center;
596    }
597  -->  -->
598  </style>  </style>
599  <script type="text/javascript">  <script type="text/javascript">
# Line 504  EOF3 Line 608  EOF3
608          $retHTML .= <<'EOF3';          $retHTML .= <<'EOF3';
609    
610  function debug(msg) {  function debug(msg) {
611  //      return; // Disable debugging          return; // Disable debugging
612    
613          if (! debug_div) debug_div = document.getElementById('debug');          if (! debug_div) debug_div = document.getElementById('debug');
614    
# Line 560  function checkAll(location) { Line 664  function checkAll(location) {
664          update_sum(suma);          update_sum(suma);
665  }  }
666    
667  function update_sum(suma) {  function update_sum(suma, suma_disp) {
668          element_id('forma').elements['totalsize'].value = suma;          if (! suma_disp) suma_disp = suma;
669            element_id('forma').elements['totalsize'].value = suma_disp;
670          pbar_set(suma, media_size);          pbar_set(suma, media_size);
671          debug('total size: ' + suma);          debug('total size: ' + suma);
672  }  }
# Line 576  function sumiraj(e) { Line 681  function sumiraj(e) {
681                  } else {                  } else {
682                          suma -= size;                          suma -= size;
683                  }                  }
684    
685                    var parts = parseInt( element_id("prt" + e.name.substr(3)).value);
686                    if (suma > media_size && suma == size && parts > 1) {
687                            element_id("parts").innerHTML = "This will take "+parts+" mediums!";
688                            element_id("parts").style.display = 'block';
689                            update_sum(media_size, suma);
690                            suma = media_size;
691                            return suma;
692                    } else {
693                            element_id("parts").style.display = 'none';
694                    }
695    
696                    if (suma < 0) suma = 0;
697          } else {          } else {
698                  suma = 0;                  suma = 0;
699                  for (var i = 0; i < len; i++) {                  for (var i = 0; i < len; i++) {
# Line 673  Size: <input type="text" name="totalsize Line 791  Size: <input type="text" name="totalsize
791  </div>  </div>
792  <br/>  <br/>
793    
794    <div id="parts">&nbsp;</div>
795    
796  Note:  Note:
797  <textarea name="note" cols="10" rows="5" id="note"></textarea>  <textarea name="note" cols="10" rows="5" id="note"></textarea>
798    
# Line 693  EOF3 Line 813  EOF3
813                          <td class="tableheader">                          <td class="tableheader">
814                                  <input type="checkbox" name="allFiles" id="allFiles" onClick="checkAll('allFiles');">                                  <input type="checkbox" name="allFiles" id="allFiles" onClick="checkAll('allFiles');">
815                          </td>                          </td>
816                          <td align="center">Share</td>          } .
817                          <td align="center">Backup no</td>                  sort_header($param, 'Share', 'share', 'center') .
818                    sort_header($param, '#', 'num', 'center') .
819            qq{
820                          <td align="center">Type</td>                          <td align="center">Type</td>
821                          <td align="center">date</td>          } .
822                          <td align="center">age/days</td>                  sort_header($param, 'Date', 'date', 'center') .
823                          <td align="center">size/MB</td>                  sort_header($param, 'Age/days', 'age', 'center') .
824                          <td align="center">gzip size/kB</td>                  sort_header($param, 'Size/Mb', 'size', 'center') .
825                    sort_header($param, 'gzip size/Kb', 'incsize', 'center') .
826            qq{
827                          </tr>                          </tr>
   
828          };          };
829    
830          my @color = (' bgcolor="#e0e0e0"', '');          my @color = (' bgcolor="#e0e0e0"', '');
# Line 709  EOF3 Line 832  EOF3
832          my $i = 0;          my $i = 0;
833          my $host = '';          my $host = '';
834    
835          foreach my $backup ( getBackupsNotBurned() ) {          foreach my $backup ( getBackupsNotBurned($param) ) {
836    
837                  if ($host ne $backup->{'host'}) {                  if ($host ne $backup->{'host'}) {
838                          $i++;                          $i++;
# Line 737  EOF3 Line 860  EOF3
860                          '<td align="center">' . $backup->{'age'} . '</td>' .                          '<td align="center">' . $backup->{'age'} . '</td>' .
861                          '<td align="right">' . $backup->{'size'} . '</td>' .                          '<td align="right">' . $backup->{'size'} . '</td>' .
862                          '<td align="right">' . $backup->{'inc_size'} .                          '<td align="right">' . $backup->{'inc_size'} .
863                          '<input type="hidden" iD="fss'.$checkbox_key .'" value="'. $backup->{'inc_size'} .'"></td>' .                          '<input type="hidden" id="fss'.$checkbox_key .'" value="'. $backup->{'inc_size'} .'"></td>' .
864                            '<input type="hidden" id="prt'.$checkbox_key .'" value="'. $backup->{'parts'} .'"></td>' .
865    
866                          "</tr>\n";                          "</tr>\n";
867          }          }
# Line 789  sub displayGrid($) { Line 913  sub displayGrid($) {
913          <table style="fview" width="100%" border="0" cellpadding="2" cellspacing="0">          <table style="fview" width="100%" border="0" cellpadding="2" cellspacing="0">
914                  <tr class="fviewheader">                  <tr class="fviewheader">
915                  <td></td>                  <td></td>
916                  <td align="center">Share</td>          };
917                  <td align="center">Type and Name</td>  
918                  <td align="center">#</td>          sub sort_header($$$$) {
919                  <td align="center">Size</td>                  my ($param, $display, $name, $align) = @_;
920                  <td align="center">Date</td>  
921                    my ($sort_what, $sort_dir) = split(/_/,$param->{'sort'},2);
922    
923                    my $old_sort = $param->{'sort'};
924    
925                    my $html = qq{<td align="$align"};
926                    if (lc($sort_what) eq lc($name)) {
927                            my $dir = lc($sort_dir);
928                            $dir =~ tr/ad/da/;
929                            $param->{'sort'} = $name . '_' . $dir;
930                            $html .= ' style="border: 1px solid #808080;"';
931                    } else {
932                            $param->{'sort'} = $name . '_a';
933                    }
934                    $html .= '<a href="' . page_uri($param) . '">' . $display . '</a></td>';
935                    $param->{'sort'} = $old_sort;
936    
937                    return $html;
938            }
939    
940            $retHTML .=
941                    sort_header($param, 'Share', 'share', 'center') .
942                    sort_header($param, 'Type and Name', 'path', 'center') .
943                    sort_header($param, '#', 'num', 'center') .
944                    sort_header($param, 'Size', 'size', 'center') .
945                    sort_header($param, 'Date', 'date', 'center');
946    
947            $retHTML .= qq{
948                  <td align="center">Media</td>                  <td align="center">Media</td>
949                  </tr>                  </tr>
950          };          };
# Line 813  sub displayGrid($) { Line 964  sub displayGrid($) {
964                  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, @_);
965          }          }
966    
967            my $sth_archived;
968            my %archived_cache;
969    
970            sub check_archived($$$) {
971                    my ($host, $share, $num) = @_;
972    
973                    if (my $html = $archived_cache{"$host $share $num"}) {
974                            return $html;
975                    }
976    
977                    $sth_archived ||= $dbh->prepare(qq{
978                            select
979                                    dvd_nr, note,
980                                    count(archive_burned.copy) as copies
981                            from archive
982                            inner join archive_burned on archive_burned.archive_id = archive.id
983                            inner join archive_backup on archive.id = archive_backup.archive_id
984                            inner join backups on backups.id = archive_backup.backup_id
985                            inner join hosts on hosts.id = backups.hostid
986                            inner join shares on shares.id = backups.shareid
987                            where hosts.name = ? and shares.name = ? and backups.num = ?
988                            group by dvd_nr, note
989                    });
990    
991                    my @mediums;
992    
993                    $sth_archived->execute($host, $share, $num);
994                    while (my $row = $sth_archived->fetchrow_hashref()) {
995                            push @mediums, '<abbr title="' .
996                                    $row->{'note'} .
997                                    ' [' . $row->{'copies'} . ']' .
998                                    '">' .$row->{'dvd_nr'} .
999                                    '</abbr>';
1000                    }
1001    
1002                    my $html = join(", ",@mediums);
1003                    $archived_cache{"$host $share $num"} = $html;
1004                    return $html;
1005            }
1006    
1007          my $i = $offset * $on_page;          my $i = $offset * $on_page;
1008    
1009          foreach $file (@{ $files }) {          foreach $file (@{ $files }) {
# Line 829  sub displayGrid($) { Line 1020  sub displayGrid($) {
1020                          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>} .
1021                          qq{<td class="fviewborder" align="right">} . $file->{'size'} . qq{</td>} .                          qq{<td class="fviewborder" align="right">} . $file->{'size'} . qq{</td>} .
1022                          qq{<td class="fviewborder">} . epoch_to_iso( $file->{'date'} ) . qq{</td>} .                          qq{<td class="fviewborder">} . epoch_to_iso( $file->{'date'} ) . qq{</td>} .
1023                          qq{<td class="fviewborder">} . '?' . qq{</td>};                          qq{<td class="fviewborder">} . check_archived( $file->{'hname'}, $file->{'sname'}, $file->{'backupnum'} ) . qq{</td>};
1024    
1025                  $retHTML .= "</tr>";                  $retHTML .= "</tr>";
1026          }          }
# Line 844  sub displayGrid($) { Line 1035  sub displayGrid($) {
1035          my $max_page = int( $results / $on_page );          my $max_page = int( $results / $on_page );
1036          my $page = 0;          my $page = 0;
1037    
1038          sub page_link($$$) {          sub page_uri($) {
1039                  my ($param,$page,$display) = @_;                  my $param = shift || die "no param?";
   
                 $param->{'offset'} = $page;  
1040    
1041                  my $html = '<a href = "' . $MyURL;                  my $uri = $MyURL;
1042                  my $del = '?';                  my $del = '?';
1043                  foreach my $k (keys %{ $param }) {                  foreach my $k (keys %{ $param }) {
1044                          if ($param->{$k}) {                          if ($param->{$k}) {
1045                                  $html .= $del . $k . '=' . ${EscURI( $param->{$k} )};                                  $uri .= $del . $k . '=' . ${EscURI( $param->{$k} )};
1046                                  $del = '&';                                  $del = '&';
1047                          }                          }
1048                  }                  }
1049                  $html .= '">' . $display . '</a>';                  return $uri;
1050            }
1051    
1052            sub page_link($$$) {
1053                    my ($param,$page,$display) = @_;
1054    
1055                    $param->{'offset'} = $page if (defined($page));
1056    
1057                    my $html = '<a href = "' . page_uri($param) . '">' . $display . '</a>';
1058          }          }
1059    
1060          $retHTML .= '<div style="text-align: center;">';          $retHTML .= '<div style="text-align: center;">';

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

  ViewVC Help
Powered by ViewVC 1.1.26