[LON-CAPA-cvs] cvs: loncom /interface lonsearchcat.pm

matthew lon-capa-cvs@mail.lon-capa.org
Tue, 30 Jul 2002 20:08:04 -0000


This is a MIME encoded message

--matthew1028059684
Content-Type: text/plain

matthew		Tue Jul 30 16:08:04 2002 EDT

  Modified files:              
    /loncom/interface	lonsearchcat.pm 
  Log:
  Now have real-time status update on search progress.  Search for 'e' and
  watch it run!
  Use parentheses on GDBM functions to avoid causing GDBM to wimper.
  Removed some excess debugging code.
  Added search-status output routines.
  &run_search now outputs and updates the search status.  Also checks for
      aborted connections to the httpd daemon.
  &display_results Produces somewhat tidier HTML.
  
  
--matthew1028059684
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20020730160804.txt"

Index: loncom/interface/lonsearchcat.pm
diff -u loncom/interface/lonsearchcat.pm:1.147 loncom/interface/lonsearchcat.pm:1.148
--- loncom/interface/lonsearchcat.pm:1.147	Tue Jul 30 14:30:56 2002
+++ loncom/interface/lonsearchcat.pm	Tue Jul 30 16:08:04 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Search Catalog
 #
-# $Id: lonsearchcat.pm,v 1.147 2002/07/30 18:30:56 matthew Exp $
+# $Id: lonsearchcat.pm,v 1.148 2002/07/30 20:08:04 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -221,7 +221,7 @@
     ##
     untie %groupsearch_db if (tied(%groupsearch_db));
     if ($ENV{'form.launch'} eq '1') {
-	if (tie(%groupsearch_db,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
+	if (tie(%groupsearch_db,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
 	    &start_fresh_session();
 	    untie %groupsearch_db;
 	} else {
@@ -283,16 +283,11 @@
             ($ENV{'form.phase'} eq 'adv_search')) {
         # Set up table
         if (! defined(&create_results_table())) {
-            my $error = &Apache::lonmysql::get_error();
             $r->print(<<END);
 <html><head><title>Search Error</title></head>
 <body>
 Unable to create table in which to store search results.  
 The search has been aborted.
-<pre>
-ERROR:
-$error
-</pre>
 </body>
 </html>
 END
@@ -627,7 +622,7 @@
     my $filename = shift;
     return 0 if (! -e $filename);
     return undef if (! tie(%persistent_db,'GDBM_File',$filename,
-                           &GDBM_READER,0640));
+                           &GDBM_READER(),0640));
     #
     # These make sure we do not get array references printed out as 'values'.
     my %arrays_allowed = ('form.category'=>1,'form.domains'=>1);
@@ -684,7 +679,7 @@
     my @Values;   # Return array
     return undef if (! -e $filename);
     return undef if (! tie(%persistent_db,'GDBM_File',$filename,
-                           &GDBM_READER,0640));
+                           &GDBM_READER(),0640));
     foreach my $name (@Vars) {
         if (! exists($persistent_db{$name})) {
             push @Values, undef;
@@ -725,7 +720,7 @@
     my %save = %{shift()};
     my $filename = shift;
     return undef if (! tie(%persistent_db,'GDBM_File',
-                           $filename,&GDBM_WRCREAT,0640));
+                           $filename,&GDBM_WRCREAT(),0640));
     foreach my $name (keys(%save)) {
         next if (! exists($save{$name}));
         next if (! defined($save{$name}) || $save{$name} eq '');
@@ -1603,6 +1598,7 @@
     } 
     return undef; # Error...
 }
+
 ######################################################################
 ######################################################################
 
@@ -1628,6 +1624,54 @@
 
 =pod
 
+=item Search Status update functions
+
+Each of the following functions changes the values of one of the
+input fields used to display the search status to the user.
+
+=over 4
+
+=item &update_count_status()
+
+=item &update_contact_status()
+
+=item &update_read_status()
+
+=back
+
+=cut
+
+######################################################################
+######################################################################
+sub update_count_status {
+    my ($r,$text) = @_;
+    $text =~ s/\'/\\\'/g;
+    $r->print
+        ("<script>document.statusform.count.value = ' $text'</script>\n");
+    $r->rflush();
+}
+
+sub update_contact_status {
+    my ($r,$text) = @_;
+    $text =~ s/\'/\\\'/g;
+    $r->print
+        ("<script>document.statusform.c_server.value = ' $text'</script>\n");
+    $r->rflush();
+}
+
+sub update_read_status {
+    my ($r,$text) = @_;
+    $text =~ s/\'/\\\'/g;
+    $r->print
+        ("<script>document.statusform.r_server.value = ' $text'</script>\n");
+    $r->rflush();
+}
+
+######################################################################
+######################################################################
+
+=pod
+
 =item &run_search 
 
 =cut
@@ -1636,6 +1680,7 @@
 ######################################################################
 sub run_search {
     my ($r,$query,$customquery,$customshow,$serverlist,$pretty_string) = @_;
+    my $c = $r->connection;
     #
     # Timing variables
     #
@@ -1645,7 +1690,7 @@
     # Print run_search header
     #
     $r->print("<html><head><title>Search Status</title></head><body>");
-    $r->print("Search: ".$pretty_string."<br />\n");
+    $r->print("Search: ".$pretty_string."\n");
     $r->rflush();
     #
     # Determine the servers we need to contact.
@@ -1660,7 +1705,7 @@
     my $table =$ENV{'form.table'};
     if (! defined($table) || $table eq '') {
         $r->print("Unable to determine table id to store search results in.".
-                  "The search has been aborted.");
+                  "The search has been aborted.</body></html>");
         return;
     }
     my $table_status = &Apache::lonmysql::check_table($table);
@@ -1674,7 +1719,7 @@
     }
     if (! $table_status) {
         $r->print("The table id,$table, we tried to use is invalid.".
-                  "The search has been aborted.");
+                  "The search has been aborted.</body></html>");
         return;
     }
     ##
@@ -1683,7 +1728,19 @@
     my $hitcountsum;
     my $server; 
     my $status;
-    $r->print("Searching");
+    $r->print(<<END);
+<form name="statusform" action="" method="post">
+<table>
+<tr><th>Contacting</th><th>Receiving</th><th>Total Matches</th></tr>
+<tr>
+<td><input type="text" name="c_server" value="" size="15" /></td>
+<td><input type="text" name="r_server" value="" size="15" /></td>
+<td><input type="text" name="count" value="" size="10" /></td>
+</tr>
+</table>
+</form>
+END
+    $r->rflush();
     while ((time - $starttime < $max_time) && 
            ((@Servers_to_contact) || keys(%Server_status))) {
         # Send out a search request if it needs to be done.
@@ -1694,26 +1751,25 @@
                                                       $customshow,[$server]);
             ($server) = keys(%$reply);
             $Server_status{$server} = $reply->{$server};
-            # $r->print("Contacted:$server:reply:$Server_status{$server}");
-            $r->print(" .");
-            $r->rflush();
+            &update_contact_status($r,$server);
         } else {
+            &update_contact_status($r,'none');
             sleep(1); # wait a sec. to give time for files to be written
         }
         while (my ($server,$status) = each(%Server_status)) {
+            last if ($c->aborted());
             if ($status eq 'con_lost') {
                 delete ($Server_status{$server});
-                # $r->print("server $server is not responding.");
                 next;
             }
             $status=~/^([\.\w]+)$/; 
        	    my $datafile=$r->dir_config('lonDaemons').'/tmp/'.$1;
             if (-e $datafile && ! -e "$datafile.end") {
-                # Let the user know we are receiving data from the server
-                # $r->print("$server:Receiving file");
                 next;
             }
+            last if ($c->aborted());
             if (-e "$datafile.end") {
+                &update_read_status($r,$server);
                 if (-z "$datafile") {
                     delete($Server_status{$server});
                     next;
@@ -1729,6 +1785,7 @@
                 }
                 # Read in the whole file.
                 while (my $result = <$fh>) {
+                    last if ($c->aborted());
                     # handle custom fields?  Someday we will!
                     chomp($result);
                     next unless $result;
@@ -1743,6 +1800,7 @@
                     }
                     # $r->print(&Apache::lonmysql::get_debug());
                     $hitcountsum ++;
+                    &update_count_status($r,$hitcountsum) if ($hitcountsum % 50 == 0);
                 } # End of foreach (@results)
                 $fh->close();
                 # $server is only deleted if the results file has been 
@@ -1751,19 +1809,23 @@
                 # $r->print("Received $new_count more results from ".
                 #              $server.".");
             }
+            last if ($c->aborted());
+            &update_count_status($r,$hitcountsum);
         }
+        last if ($c->aborted());
         # Finished looping through the servers
     }
+    &update_read_status($r,'none');
     &Apache::lonmysql::disconnect_from_db();
     # Let the user know
     #
     # We have run out of time or run out of servers to talk to and
     # results to get.  
-    $r->print("<br /><b>Search Completed</b><br />");
+    $r->print("<b>Search Completed.</b>&nbsp;&nbsp;");
     if ($hitcountsum) {
-        $r->print($hitcountsum." successful matches were found.<br />");
+        $r->print($hitcountsum." matches were found.");
     } else {
-        $r->print("There were no successful matches to your query.<br />");
+        $r->print("There were no successful matches to your query.");
     }
     $r->print("</body></html>");
     return;
@@ -1845,7 +1907,7 @@
     ##
     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
         if (! tie(%groupsearch_db,'GDBM_File',$diropendb,
-                  &GDBM_WRCREAT,0640)) {
+                  &GDBM_WRCREAT(),0640)) {
             $r->print('Unable to tie hash to db file</body></html>');
             $r->rflush();
             return;
@@ -1907,12 +1969,12 @@
     ##
     $r->print("<center>Results $min to $max out of $total_results</center>\n");
     $r->print
-        ('<br /><center>'.
+        ('<center>'.
          &prev_next_buttons($min,$ENV{'form.show'},$total_results,
                             "table=".$ENV{'form.table'}.
                             "&phase=results".
                             "&persistent_db_id=".$ENV{'form.persistent_db_id'})
-         ."</center><br />\n"
+         ."</center>\n"
          );
     ##
     ## Get results from MySQL table
@@ -1937,13 +1999,13 @@
         $r->print("There were no results matching your query");
     } else {
         $r->print
-            ('<br /><center>'.
+            ('<center>'.
              &prev_next_buttons($min,$ENV{'form.show'},$total_results,
                                 "table=".$ENV{'form.table'}.
                                 "&phase=results".
                                 "&persistent_db_id=".
                                 $ENV{'form.persistent_db_id'})
-             ."</center><br />\n"
+             ."</center>\n"
              );
     }
     $r->print("</body></html>");

--matthew1028059684--