/[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 88 by dpavlin, Sun Aug 28 14:15: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 196  sub getFilesHyperEstraier($) { Line 219  sub getFilesHyperEstraier($) {
219          my $shareid = $param->{'search_share'};          my $shareid = $param->{'search_share'};
220    
221          if (length($q) > 0) {          if (length($q) > 0) {
222                  $q =~ s/(.)/$1 /g;                  # exact match
223                    $cond->add_attr("filepath ISTRINC $q");
224    
225                    $q =~ s/(.)/$1 /g;
226                  # set the search phrase to the search condition object                  # set the search phrase to the search condition object
227                  $cond->set_phrase($q);                  $cond->set_phrase($q);
228          }          }
# Line 217  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 239  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                  backups.num             AS backupnum,                          hosts.name AS host,
333                  min(backups.type)       AS type,                          shares.name AS share,
334                  min(backups.date)       AS date,                          backups.num AS backupnum,
335                  min(backups.size)       AS size                          backups.type AS type,
336          FROM files                          backups.date AS date,
337                  INNER JOIN shares       ON files.shareID=shares.ID                          backups.size AS size,
338                  INNER JOIN hosts        ON hosts.ID = shares.hostID                          backups.id AS id
339                  INNER JOIN backups      ON backups.num = files.backupnum and backups.hostID = hosts.ID AND backups.shareID = shares.ID                  FROM backups
340          GROUP BY                  INNER JOIN shares       ON backups.shareID=shares.ID
341                  backups.hostID, backups.num                  INNER JOIN hosts        ON backups.hostID = hosts.ID
342          ORDER BY min(backups.date)                  LEFT OUTER JOIN archive_backup ON archive_backup.backup_id = backups.id
343                    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 265  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() {
   {  
       my $retHTML = "";  
       my $addForm = 1;  
         
       if ($addForm) {  
374    
375              $retHTML .= <<EOF3;          my $retHTML .= q{
376  <script language="javascript" type="text/javascript">                  <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">
377            };
378    
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      function checkAll(location)  #progressIndicator {
477      {          z-index: 3;
478        for (var i=0;i<document.forma.elements.length;i++)          background-color: transparent;
479        {  }
480          var e = document.forma.elements[i];  -->
481          if ((e.checked || !e.checked) && e.name != \'all\') {  </style>
482              if (eval("document.forma."+location+".checked")) {  <script type="text/javascript">
483                  e.checked = true;  <!--
484              } else {  
485                  e.checked = false;  var debug_div = null;
             }  
         }  
       }  
     }  
 //-->  
 </script>        
486  EOF3  EOF3
               $retHTML .= q{<form name="forma" method="GET" action="}."$MyURL"."?action=burn\"";  
               $retHTML.= q{<input type="hidden" value="burn" name="action">};  
               $retHTML .= q{<input type="hidden" value="results" name="search_results">};  
         }  
         $retHTML .= qq{  
                 <table style="fview" border="1" cellspacing="0" cellpadding="3">  
                 <tr class="tableheader">  
         };  
487    
488          if ($addForm) {          # take maximum archive size from configuration
489              $retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>";          $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          $retHTML .=  qq{          if (debug_div) {
505                  <td align="center">Host</td>                  debug_div.appendChild(document.createTextNode(msg));
506                  <td align="center">Backup no</td>                  debug_div.appendChild(document.createElement("br"));
507                  <td align="center">Type</td>          }
508                  <td align="center">date</td>  }
509                  <td align="center">age/days</td>  
510                  <td align="center">size/MB</td>  
511                  </tr>  var element_id_cache = Array();
         };  
512    
513          my @backups = getBackupsNotBurned();  function element_id(name,element) {
514          my $backup;          if (! element_id_cache[name]) {
515                    element_id_cache[name] = self.document.getElementById(name);
516            }
517            return element_id_cache[name];
518    }
519    
520          if ($addForm) {  function checkAll(location) {
521                  $retHTML .= qq{          var f = element_id('forma') || null;
522                          <tr><td colspan=7 style="tableheader">          if (!f) return false;
523                          <input type="submit" value="Burn selected backups on medium" name="submitBurner">  
524                          </td></tr>          var len = f.elements.length;
525                  };          var check_all = element_id('allFiles');
526            var suma = check_all.checked ? (parseInt(f.elements['totalsize'].value) || 0) : 0;
527    
528            for (var i = 0; i < len; i++) {
529                    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                                    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          foreach $backup(@backups) {  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                  my $ftype = "";  function sumiraj(e) {
557                        var suma = parseInt(element_id('forma').elements['totalsize'].value) || 0;
558                  $retHTML .= "<tr>";          var len = element_id('forma').elements.length;
559                  if ($addForm) {          if (e) {
560                          $retHTML .= '<td class="fview"><input type="checkbox" name="fcb' .                  var size = parseInt( element_id("fss" + e.name.substr(3)).value);
561                                  $backup->{'hostid'}.'_'.$backup->{'backupnum'} .                  if (e.checked) {
562                                  '" value="' . $backup->{'hostid'}.'_'.$backup->{'backupnum'} .                          suma += size;
563                                  '"></td>';                  } else {
564                  }                                    suma -= size;
565                                }
566                  $retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' .          } else {
567                          '<td class="fviewborder">' . $backup->{'backupnum'} . '</td>' .                  suma = 0;
568                          '<td class="fviewborder">' . $backup->{'type'} . '</td>' .                  for (var i = 0; i < len; i++) {
569                          '<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .                          var e = element_id('forma').elements[i];
570                          '<td class="fviewborder">' . $backup->{'age'} . '</td>' .                          if (e.name != 'all' && e.checked && e.name.substr(0,3) == 'fcb') {
571                          '<td class="fviewborder">' . $backup->{'size'} . '</td>' .                                  var el = element_id("fss" + e.name.substr(3));
572                          '</tr>';                                  if (el && el.value) suma += parseInt(el.value) || 0;
573                            }
574                    }
575          }          }
576            update_sum(suma);
577            return suma;
578    }
579    
580          $retHTML .= "</table>";  /* 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          if ($addForm) {  function dec2hex(d) {
595                  $retHTML .= "</form>";          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
675            $retHTML .= q{
676                            <input type="hidden" value="burn" name="action">
677                            <input type="hidden" value="results" name="search_results">
678                            <table style="fview" border="0" cellspacing="0" cellpadding="2">
679                            <tr class="tableheader">
680                            <td class="tableheader">
681                                    <input type="checkbox" name="allFiles" id="allFiles" onClick="checkAll('allFiles');">
682                            </td>
683                            <td align="center">Share</td>
684                            <td align="center">Backup no</td>
685                            <td align="center">Type</td>
686                            <td align="center">date</td>
687                            <td align="center">age/days</td>
688                            <td align="center">size/MB</td>
689                            <td align="center">gzip size</td>
690                            </tr>
691    
692            };
693    
694            my @color = (' bgcolor="#e0e0e0"', '');
695    
696            my $i = 0;
697            my $host = '';
698    
699            foreach my $backup ( getBackupsNotBurned() ) {
700    
701                    if ($host ne $backup->{'host'}) {
702                            $i++;
703                            $host = $backup->{'host'};
704                    }
705                    my $ftype = "";
706    
707                    my $checkbox_key = $backup->{'hostid'}. '_' .$backup->{'backupnum'} . '_' . $backup->{'id'};
708    
709                    $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>' .
724                            '<td align="center">' . $backup->{'backupnum'} . '</td>' .
725                            '<td align="center">' . $backup->{'type'} . '</td>' .
726                            '<td align="center">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
727                            '<td align="center">' . $backup->{'age'} . '</td>' .
728                            '<td align="right">' . $backup->{'size'} . '</td>' .
729                            '<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>";
736            $retHTML .= "</form>";
737                
738          return $retHTML;          return $retHTML;
739  }        }      

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

  ViewVC Help
Powered by ViewVC 1.1.26