/[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 102 by dpavlin, Tue Aug 30 22:43:01 2005 UTC revision 149 by dpavlin, Fri Oct 7 12:27:07 2005 UTC
# Line 15  my $pager_pages = 10; Line 15  my $pager_pages = 10;
15  my $dsn = $Conf{SearchDSN};  my $dsn = $Conf{SearchDSN};
16  my $db_user = $Conf{SearchUser} || '';  my $db_user = $Conf{SearchUser} || '';
17    
18  my $index_path = $Conf{HyperEstraierIndex};  my $hest_index_path = $Conf{HyperEstraierIndex};
 if ($index_path) {  
         $index_path = $TopDir . '/' . $index_path;  
         $index_path =~ s#//#/#g;  
 }  
19    
20  my $dbh;  my $dbh;
21    
# Line 175  sub getFiles($) { Line 171  sub getFiles($) {
171          return ($results, \@ret);          return ($results, \@ret);
172  }  }
173    
174    sub getHyperEstraier_url($) {
175            my ($use_hest) = @_;
176    
177            return unless $use_hest;
178    
179            use HyperEstraier;
180            my ($index_path, $index_node_url);
181    
182            if ($use_hest =~ m#^http://#) {
183                    $index_node_url = $use_hest;
184            } else {
185                    $index_path = $TopDir . '/' . $index_path;
186                    $index_path =~ s#//#/#g;
187            }
188            return ($index_path, $index_node_url);
189    }
190    
191  sub getFilesHyperEstraier($) {  sub getFilesHyperEstraier($) {
192          my ($param) = @_;          my ($param) = @_;
193    
194          my $offset = $param->{'offset'} || 0;          my $offset = $param->{'offset'} || 0;
195          $offset *= $on_page;          $offset *= $on_page;
196    
197          die "no index_path?" unless ($index_path);          die "no index_path?" unless ($hest_index_path);
198    
199          use HyperEstraier;          use HyperEstraier;
200    
201            my ($index_path, $index_node_url) = getHyperEstraier_url($hest_index_path);
202    
203          # open the database          # open the database
204          my $db = HyperEstraier::Database->new();          my $db;
205          $db->open($index_path, $HyperEstraier::ESTDBREADER);          if ($index_path) {
206                    $db = HyperEstraier::Database->new();
207                    $db->open($index_path, $HyperEstraier::ESTDBREADER);
208            } elsif ($index_node_url) {
209                    $db ||= HyperEstraier::Node->new($index_node_url);
210                    $db->set_auth('admin', 'admin');
211            } else {
212                    die "BUG: unimplemented";
213            }
214    
215          # create a search condition object          # create a search condition object
216          my $cond = HyperEstraier::Condition->new();          my $cond = HyperEstraier::Condition->new();
# Line 219  sub getFilesHyperEstraier($) { Line 242  sub getFilesHyperEstraier($) {
242          $cond->set_order( 'date NUMA' );          $cond->set_order( 'date NUMA' );
243    
244          # get the result of search          # get the result of search
         my $result = $db->search($cond, 0);  
   
245          my @res;          my @res;
246          my $hits = $result->size;          my ($result, $hits);
247    
248            if ($index_path) {
249                    $result = $db->search($cond, 0);
250                    $hits = $result->size;
251            } elsif ($index_node_url) {
252                    $result = $db->search($cond, 0);
253                    $hits = $result->doc_num;
254            } else {
255                    die "BUG: unimplemented";
256            }
257    
258          # for each document in result          # for each document in result
259          for my $i ($offset .. ($offset + $on_page - 1)) {          for my $i ($offset .. ($offset + $on_page - 1)) {
260                  last if ($i >= $hits);                  last if ($i >= $hits);
261    
262                  my $id = $result->get($i);                  my $doc;
263                  my $doc = $db->get_doc($id, 0);                  if ($index_path) {
264                            my $id = $result->get($i);
265                            $doc = $db->get_doc($id, 0);
266                    } elsif ($index_node_url) {
267                            $doc = $result->get_doc($i);
268                    } else {
269                            die "BUG: unimplemented";
270                    }
271    
272                  my $row;                  my $row;
273                  foreach my $c (qw/fid hname sname backupnum fiilename filepath date type size/) {                  foreach my $c (qw/fid hname sname backupnum fiilename filepath date type size/) {
# Line 241  sub getFilesHyperEstraier($) { Line 279  sub getFilesHyperEstraier($) {
279          return ($hits, \@res);          return ($hits, \@res);
280  }  }
281    
282    sub getGzipName($$$)
283    {
284            my ($host, $share, $backupnum) = @_;
285            my $ret = $Conf{GzipSchema};
286            
287            $share =~ s/\//_/g;
288            $ret =~ s/\\h/$host/ge;
289            $ret =~ s/\\s/$share/ge;
290            $ret =~ s/\\n/$backupnum/ge;
291            
292            return $ret;
293            
294    }
295    
296    sub getGzipSize($$)
297    {
298            my ($hostID, $backupNum) = @_;
299            my $ret;
300            my $sql;
301            my $dbh = get_dbh();
302            
303            $sql = q{
304                                    SELECT hosts.name  as host,
305                                               shares.name as share,
306                                               backups.num as backupnum
307                                    FROM hosts, backups, shares
308                                    WHERE shares.id=backups.shareid AND
309                                              hosts.id =backups.hostid AND
310                                              hosts.id=? AND
311                                              backups.num=?
312                            };
313            my $sth = $dbh->prepare($sql);
314            $sth->execute($hostID, $backupNum);
315    
316            my $row = $sth->fetchrow_hashref();
317            
318            my (undef,undef,undef,undef,undef,undef,undef,$ret,undef,undef,undef,undef,undef) =
319                            stat( $Conf{InstallDir}.'/'.$Conf{GzipTempDir}.'/'.
320                                    getGzipName($row->{'host'}, $row->{share}, $row->{'backupnum'}));
321            
322            return $ret;    
323    }
324    
325  sub getBackupsNotBurned() {  sub getBackupsNotBurned() {
326    
327          my $dbh = get_dbh();          my $dbh = get_dbh();
328          my $sql = q{  
329          SELECT          my $sql = q{
330                  backups.hostID          AS hostid,                  SELECT
331                  min(hosts.name)         AS host,                          backups.hostID AS hostID,
332                  min(shares.name)        AS share,                          hosts.name AS host,
333                  backups.num             AS backupnum,                          shares.name AS share,
334                  min(backups.type)       AS type,                          backups.num AS backupnum,
335                  min(backups.date)       AS date,                          backups.type AS type,
336                  min(backups.size)       AS size                          backups.date AS date,
337          FROM files                          backups.size AS size,
338                  INNER JOIN shares       ON files.shareID=shares.ID                          backups.id AS id
339                  INNER JOIN hosts        ON hosts.ID = shares.hostID                  FROM backups
340                  INNER JOIN backups      ON backups.num = files.backupnum and backups.hostID = hosts.ID AND backups.shareID = shares.ID                  INNER JOIN shares       ON backups.shareID=shares.ID
341          GROUP BY                  INNER JOIN hosts        ON backups.hostID = hosts.ID
342                  backups.hostID, backups.num, backups.shareid                  LEFT OUTER JOIN archive_backup ON archive_backup.backup_id = backups.id
343          ORDER BY min(backups.date)                  WHERE backups.size > 0 AND archive_backup.backup_id IS NULL
344                    GROUP BY
345                            backups.hostID,
346                            hosts.name,
347                            shares.name,
348                            backups.num,
349                            backups.shareid,
350                            backups.id,
351                            backups.type,
352                            backups.date,
353                            backups.size
354                    ORDER BY backups.date
355          };          };
356          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
357          my @ret;          my @ret;
# Line 268  sub getBackupsNotBurned() { Line 360  sub getBackupsNotBurned() {
360          while ( my $row = $sth->fetchrow_hashref() ) {          while ( my $row = $sth->fetchrow_hashref() ) {
361                  $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );                  $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );
362                  $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);                  $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);
363                    my (undef,undef,undef,undef,undef,undef,undef,$fs_size,undef,undef,undef,undef,undef) =
364                            stat( $Conf{InstallDir}.'/'.$Conf{GzipTempDir}.'/'.
365                                    getGzipName($row->{'host'}, $row->{share}, $row->{'backupnum'}));
366                    $row->{'fs_size'} = $fs_size;
367                  push @ret, $row;                  push @ret, $row;
368          }          }
369                
370          return @ret;                return @ret;      
371  }  }
372    
373  sub displayBackupsGrid()  sub displayBackupsGrid() {
374    {  
375        my $retHTML = "";          my $retHTML .= q{
376                          <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">
377          $retHTML .= <<EOF3;          };
378  <script language="javascript" type="text/javascript">  
379            $retHTML .= <<'EOF3';
380    <style type="text/css">
381  <!--  <!--
382    DIV#fixedBox {
383            position: absolute;
384            top: 50em;
385            left: -24%;
386            padding: 0.5em;
387            width: 20%;
388            background-color: #E0F0E0;
389            border: 1px solid #00C000;
390    }
391    
392    DIV#fixedBox, DIV#fixedBox INPUT, DIV#fixedBox TEXTAREA {
393            font-size: 10pt;
394    }
395    
396    FORM>DIV#fixedBox {
397            position: fixed !important;
398            left: 0.5em !important;
399            top: auto !important;
400            bottom: 1em !important;
401            width: 15% !important;
402    }
403    
404    DIV#fixedBox INPUT[type=text], DIV#fixedBox TEXTAREA {
405            border: 1px solid #00C000;
406    }
407    
408    DIV#fixedBox #note {
409            display: block;
410            width: 100%;
411    }
412    
413    DIV#fixedBox #submitBurner {
414            display: block;
415            width: 100%;
416            margin-top: 0.5em;
417            cursor: pointer;
418    }
419    
420    * HTML {
421            overflow-y: hidden;
422    }
423    
424    * HTML BODY {
425            overflow-y: auto;
426            height: 100%;
427            font-size: 100%;
428    }
429    
430    * HTML DIV#fixedBox {
431            position: absolute;
432    }
433    
434    #mContainer, #gradient, #mask, #progressIndicator {
435            display: block;
436            width: 100%;
437            font-size: 10pt;
438            font-weight: bold;
439            text-align: center;
440            vertical-align: middle;
441            padding: 1px;
442    }
443    
444    #gradient, #mask, #progressIndicator {
445            left: 0;
446            border-width: 1px;
447            border-style: solid;
448            border-color: #000000;
449            color: #404040;
450            margin: 0.4em;
451            position: absolute;
452            margin-left: -1px;
453            margin-top: -1px;
454            margin-bottom: -1px;
455            overflow: hidden;
456    }
457    
458    #mContainer {
459            display: block;
460            position: relative;
461            padding: 0px;
462            margin-top: 0.4em;
463            margin-bottom: 0.5em;
464    }
465    
466    #gradient {
467            z-index: 1;
468            background-color: #FFFF00;
469    }
470    
471    #mask {
472            z-index: 2;
473            background-color: #FFFFFF;
474    }
475    
476    #progressIndicator {
477            z-index: 3;
478            background-color: transparent;
479    }
480    -->
481    </style>
482    <script type="text/javascript">
483    <!--
484    
485    var debug_div = null;
486    EOF3
487    
488            # take maximum archive size from configuration
489            $retHTML .= 'var media_size = '. $Conf{MaxArchiveSize} .';';
490    
491            $retHTML .= <<'EOF3';
492    
493    function debug(msg) {
494    //      return; // Disable debugging
495    
496            if (! debug_div) debug_div = document.getElementById('debug');
497    
498            // this will create debug div if it doesn't exist.
499            if (! debug_div) {
500                    debug_div = document.createElement('div');
501                    if (document.body) document.body.appendChild(debug_div);
502                    else debug_div = null;
503            }
504            if (debug_div) {
505                    debug_div.appendChild(document.createTextNode(msg));
506                    debug_div.appendChild(document.createElement("br"));
507            }
508    }
509    
510    
511    var element_id_cache = Array();
512    
513    function element_id(name,element) {
514            if (! element_id_cache[name]) {
515                    element_id_cache[name] = self.document.getElementById(name);
516            }
517            return element_id_cache[name];
518    }
519    
520      function checkAll(location)  function checkAll(location) {
521      {          var f = element_id('forma') || null;
522        for (var i=0;i<document.forma.elements.length;i++)          if (!f) return false;
523        {  
524          var e = document.forma.elements[i];          var len = f.elements.length;
525          if ((e.checked || !e.checked) && e.name != \'all\') {          var check_all = element_id('allFiles');
526              if (eval("document.forma."+location+".checked")) {          var suma = check_all.checked ? (parseInt(f.elements['totalsize'].value) || 0) : 0;
527                  e.checked = true;  
528              } else {          for (var i = 0; i < len; i++) {
529                  e.checked = false;                  var e = f.elements[i];
530              }                  if (e.name != 'all' && e.name.substr(0, 3) == 'fcb') {
531          }                          if (check_all.checked) {
532        }                                  if (e.checked) continue;
533      }                                  var el = element_id("fss" + e.name.substr(3));
534  //-->                                  var size = parseInt(el.value) || 0;
535  </script>                                        debug('suma: '+suma+' size: '+size);
536                                    if ((suma + size) < media_size) {
537                                            suma += size;
538                                            e.checked = true;
539                                    } else {
540                                            break;
541                                    }
542                            } else {
543                                    e.checked = false;
544                            }
545                    }
546            }
547            update_sum(suma);
548    }
549    
550    function update_sum(suma) {
551            element_id('forma').elements['totalsize'].value = suma;
552            pbar_set(suma, media_size);
553            debug('total size: ' + suma);
554    }
555    
556    function sumiraj(e) {
557            var suma = parseInt(element_id('forma').elements['totalsize'].value) || 0;
558            var len = element_id('forma').elements.length;
559            if (e) {
560                    var size = parseInt( element_id("fss" + e.name.substr(3)).value);
561                    if (e.checked) {
562                            suma += size;
563                    } else {
564                            suma -= size;
565                    }
566            } else {
567                    suma = 0;
568                    for (var i = 0; i < len; i++) {
569                            var e = element_id('forma').elements[i];
570                            if (e.name != 'all' && e.checked && e.name.substr(0,3) == 'fcb') {
571                                    var el = element_id("fss" + e.name.substr(3));
572                                    if (el && el.value) suma += parseInt(el.value) || 0;
573                            }
574                    }
575            }
576            update_sum(suma);
577            return suma;
578    }
579    
580    /* progress bar */
581    
582    var _pbar_width = null;
583    var _pbar_warn = 10;    // change color in last 10%
584    
585    function pbar_reset() {
586            element_id("mask").style.left = "0px";
587            _pbar_width = element_id("mContainer").offsetWidth - 2;
588            element_id("mask").style.width = _pbar_width + "px";
589            element_id("mask").style.display = "block";
590            element_id("progressIndicator").style.zIndex  = 10;
591            element_id("progressIndicator").innerHTML = "0";
592    }
593    
594    function dec2hex(d) {
595            var hch = '0123456789ABCDEF';
596            var a = d % 16;
597            var q = (d - a) / 16;
598            return hch.charAt(q) + hch.charAt(a);
599    }
600    
601    function pbar_set(amount, max) {
602            debug('pbar_set('+amount+', '+max+')');
603    
604            if (_pbar_width == null) {
605                    var _mc = element_id("mContainer");
606                    if (_pbar_width == null) _pbar_width = parseInt(_mc.offsetWidth ? (_mc.offsetWidth - 2) : 0) || null;
607                    if (_pbar_width == null) _pbar_width = parseInt(_mc.clientWidth ? (_mc.clientWidth + 2) : 0) || null;
608                    if (_pbar_width == null) _pbar_width = 0;
609            }
610    
611            var pcnt = Math.floor(amount * 100 / max);
612            var p90 = 100 - _pbar_warn;
613            var pcol = pcnt - p90;
614            if (Math.round(pcnt) <= 100) {
615                    if (pcol < 0) pcol = 0;
616                    var e = element_id("submitBurner");
617                    debug('enable_button');
618                    e.disabled = false;
619                    var a = e.getAttributeNode('disabled') || null;
620                    if (a) e.removeAttributeNode(a);
621            } else {
622                    debug('disable button');
623                    pcol = _pbar_warn;
624                    var e = element_id("submitBurner");
625                    if (!e.disabled) e.disabled = true;
626            }
627            var col_g = Math.floor((_pbar_warn - pcol) * 255 / _pbar_warn);
628            var col = '#FF' + dec2hex(col_g) + '00';
629    
630            //debug('pcol: '+pcol+' g:'+col_g+' _pbar_warn:'+ _pbar_warn + ' color: '+col);
631            element_id("gradient").style.backgroundColor = col;
632    
633            element_id("progressIndicator").innerHTML = pcnt + '%';
634            //element_id("progressIndicator").innerHTML = amount;
635    
636            element_id("mask").style.clip = 'rect(' + Array(
637                    '0px',
638                    element_id("mask").offsetWidth + 'px',
639                    element_id("mask").offsetHeight + 'px',
640                    Math.round(_pbar_width * amount / max) + 'px'
641            ).join(' ') + ')';
642    }
643    
644    if (!self.body) self.body = new Object();
645    self.onload = self.document.onload = self.body.onload = function() {
646            //pbar_reset();
647            sumiraj();
648    };
649    
650    // -->
651    </script>
652    <div id="fixedBox">
653    
654    Size: <input type="text" name="totalsize" size="7" readonly="readonly" style="text-align:right;" value="0" /> kB
655    
656    <div id="mContainer">
657            <div id="gradient">&nbsp;</div>
658            <div id="mask">&nbsp;</div>
659            <div id="progressIndicator">0%</div>
660    </div>
661    <br/>
662    
663    Note:
664    <textarea name="note" cols="10" rows="5" id="note"></textarea>
665    
666    <input type="submit" id="submitBurner" value="Burn selected" name="submitBurner" />
667    
668    </div>
669    <!--
670    <div id="debug" style="float: right; width: 10em; border: 1px #ff0000 solid; background-color: #ffe0e0; -moz-opacity: 0.7;">
671    no debug output yet
672    </div>
673    -->
674  EOF3  EOF3
675          $retHTML .= q{          $retHTML .= q{
676                  <form name="forma" method="GET" action="$MyURL?action=burn">                          <input type="hidden" value="burn" name="action">
677                  <input type="hidden" value="burn" name="action">                          <input type="hidden" value="results" name="search_results">
678                  <input type="hidden" value="results" name="search_results">                          <table style="fview" border="0" cellspacing="0" cellpadding="2">
679                  <table style="fview" border="0" cellspacing="0" cellpadding="2">                          <tr class="tableheader">
680                  <tr class="tableheader">                          <td class="tableheader">
681                  <td class="tableheader">                                  <input type="checkbox" name="allFiles" id="allFiles" onClick="checkAll('allFiles');">
682                          <input type="checkbox" name="allFiles" onClick="checkAll('allFiles');">                          </td>
683                  </td>                          <td align="center">Share</td>
684                  <td align="center">Share</td>                          <td align="center">Backup no</td>
685                  <td align="center">Backup no</td>                          <td align="center">Type</td>
686                  <td align="center">Type</td>                          <td align="center">date</td>
687                  <td align="center">date</td>                          <td align="center">age/days</td>
688                  <td align="center">age/days</td>                          <td align="center">size/MB</td>
689                  <td align="center">size/MB</td>                          <td align="center">gzip size</td>
690                  </tr>                          </tr>
691    
                 <tr><td colspan=7 style="tableheader">  
                 <input type="submit" value="Burn selected backups on medium" name="submitBurner">  
                 </td></tr>  
692          };          };
693    
694          my @color = (' bgcolor="#e0e0e0"', '');          my @color = (' bgcolor="#e0e0e0"', '');
# Line 333  EOF3 Line 703  EOF3
703                          $host = $backup->{'host'};                          $host = $backup->{'host'};
704                  }                  }
705                  my $ftype = "";                  my $ftype = "";
706                
707                  $retHTML .= "<tr" . $color[$i %2 ] . ">";                  my $checkbox_key = $backup->{'hostid'}. '_' .$backup->{'backupnum'} . '_' . $backup->{'id'};
708                  $retHTML .= '<td class="fview"><input type="checkbox" name="fcb' .  
                                 $backup->{'hostid'}.'_'.$backup->{'backupnum'} .  
                                 '" value="' . $backup->{'hostid'}.'_'.$backup->{'backupnum'} .  
                                 '"></td>';  
               
709                  $retHTML .=                  $retHTML .=
710                            '<tr' . $color[$i %2 ] . '>
711                            <td class="fview">';
712    
713                    # FIXME
714                    $backup->{'fs_size'} = int($backup->{'size'} * 1024);
715    
716                    if (($backup->{'fs_size'} || 0) > 0) {
717                            $retHTML .= '
718                            <input type="checkbox" name="fcb' . $checkbox_key . '" value="' . $checkbox_key . '" onClick="sumiraj(this);">';
719                    }
720    
721                    $retHTML .=
722                            '</td>' .
723                          '<td align="right">' . $backup->{'host'} . ':' . $backup->{'share'} . '</td>' .                          '<td align="right">' . $backup->{'host'} . ':' . $backup->{'share'} . '</td>' .
724                          '<td align="center">' . $backup->{'backupnum'} . '</td>' .                          '<td align="center">' . $backup->{'backupnum'} . '</td>' .
725                          '<td align="center">' . $backup->{'type'} . '</td>' .                          '<td align="center">' . $backup->{'type'} . '</td>' .
726                          '<td align="center">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .                          '<td align="center">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
727                          '<td align="center">' . $backup->{'age'} . '</td>' .                          '<td align="center">' . $backup->{'age'} . '</td>' .
728                          '<td align="right">' . $backup->{'size'} . '</td>' .                          '<td align="right">' . $backup->{'size'} . '</td>' .
729                          "</tr>\n";                          '<td align="right">' . $backup->{'fs_size'} .
730                            '<input type="hidden" iD="fss'.$checkbox_key .'" value="'. $backup->{'fs_size'} .'"></td>' .
731    
732                            "</tr>\n";
733          }          }
734    
735          $retHTML .= "</table>";          $retHTML .= "</table>";

Legend:
Removed from v.102  
changed lines
  Added in v.149

  ViewVC Help
Powered by ViewVC 1.1.26