[LON-CAPA-cvs] cvs: loncom /interface lonmysql.pm /metadata_database parse_activity_log.pl

matthew lon-capa-cvs@mail.lon-capa.org
Mon, 20 Dec 2004 19:53:36 -0000


matthew		Mon Dec 20 14:53:36 2004 EDT

  Modified files:              
    /loncom/metadata_database	parse_activity_log.pl 
    /loncom/interface	lonmysql.pm 
  Log:
  lonmysql:Added &table_information, which returns the metadata mysql keeps
      about the tables.
      Modified &update_table_info to turn the MySQL dates (creation,
      update, and check times) into unix times.
  parse_activity_log.pl: Use LONCAPA::Configuration to set configuration
      options instead of the removed subroutine &initialize_configuration
      Modified backup handling code - if a table is missing and any of the
      current tables has been modified since the backup file was written,
      back up the current tables (even though one or more is missing) to 
      a filename what will not be overwritten automatically, just to
      be sure no data is being lost.
      &load_backup_tables: Now actually use the filename we pass in instead of
      hard coding a file which may not actually exist. 
  
  
Index: loncom/metadata_database/parse_activity_log.pl
diff -u loncom/metadata_database/parse_activity_log.pl:1.7 loncom/metadata_database/parse_activity_log.pl:1.8
--- loncom/metadata_database/parse_activity_log.pl:1.7	Thu Dec 16 14:29:20 2004
+++ loncom/metadata_database/parse_activity_log.pl	Mon Dec 20 14:53:36 2004
@@ -2,7 +2,7 @@
 #
 # The LearningOnline Network
 #
-# $Id: parse_activity_log.pl,v 1.7 2004/12/16 19:29:20 matthew Exp $
+# $Id: parse_activity_log.pl,v 1.8 2004/12/20 19:53:36 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -37,9 +37,22 @@
 #   5     Unspecified error?
 #
 
+#
+# Notes:
+#
+# Logging is done via the $logthis variable, which may be the result of 
+# overcleverness.  log via $logthis->('logtext');  Those are parentheses,
+# not curly braces.  If the -log command line parameter is set, the $logthis
+# routine is set to a routine which writes to a file.  If the command line
+# parameter is not set $logthis is set to &nothing, which does what you
+# would expect.
+#
+
 use strict;
 use DBI;
-use lib '/home/httpd/lib/perl/Apache';
+use lib '/home/httpd/lib/perl/';
+use LONCAPA::Configuration();
+use Apache::lonmysql();
 use lonmysql();
 use Time::HiRes();
 use Getopt::Long();
@@ -93,8 +106,8 @@
 ##
 ## Read in configuration parameters
 ##
-my %perlvar;
-&initialize_configuration();
+my %perlvar = %{&LONCAPA::Configuration::read_conf('loncapa.conf')};
+
 if (! defined($domain) || $domain eq '') {
     $domain = $perlvar{'lonDefDomain'};
 }
@@ -260,36 +273,33 @@
       'KEY' => [{columns => ['student_id']},
                 {columns => ['time']},],
 };
-my @Activity_Table = ($activity_table_def); 
-
-#my @ID_Tables = ($student_table_def,$res_table_def,
-#                 $action_table_def,$machine_table_def);
 
+my @Activity_Table = ($activity_table_def);
 my @ID_Tables = ($student_table_def,$res_table_def,$machine_table_def);
-                      
-
 ##
 ## End of table definitions
 ##
 
-# 
 $logthis->('Connectiong to mysql');
-&Apache::lonmysql::set_mysql_user_and_password($perlvar{'lonSqlUser'},
+&Apache::lonmysql::set_mysql_user_and_password('www',
                                                $perlvar{'lonSqlAccess'});
 if (!&Apache::lonmysql::verify_sql_connection()) {
     warn "Unable to connect to MySQL database.";
     $logthis->("Unable to connect to MySQL database.");
     exit 3;
 }
-
 $logthis->('SQL connection is up');
 
 if ($drop) { &drop_tables(); $logthis->('dropped tables'); }
 
 if (-s $gz_sql_filename) {
-    # if ANY one of the tables does not exist, load the tables from the
-    # backup.
+    my $backup_modification_time = (stat($gz_sql_filename))[9];
+    $logthis->($gz_sql_filename.' was last modified '.
+               localtime($backup_modification_time).
+               '('.$backup_modification_time.')');
+    # Check for missing tables
     my @Current_Tables = &Apache::lonmysql::tables_in_db();
+    $logthis->(join(',',@Current_Tables));
     my %Found;
     foreach my $tablename (@Current_Tables) {
         foreach my $table (@Activity_Table,@ID_Tables) {
@@ -298,14 +308,37 @@
             }
         }
     }
+    $logthis->('Found tables '.join(',',keys(%Found)));
+    my $missing_a_table = 0;
     foreach my $table (@Activity_Table,@ID_Tables) {    
+        # Hmmm, should I dump the tables?
         if (! $Found{$table->{'id'}}) {
-            $time_this->();
-            &load_backup_tables($gz_sql_filename);
-            $time_this->('load backup tables');
+            $logthis->('Missing table '.$table->{'id'});
+            $missing_a_table = 1;
             last;
         }
     }
+    if ($missing_a_table) {
+        my $table_modification_time = $backup_modification_time;
+        # If the backup happened prior to the last table modification,
+        foreach my $table (@Activity_Table,@ID_Tables) {    
+            my %tabledata = &Apache::lonmysql::table_information($table->{'id'});
+            next if (! scalar(keys(%tabledata))); # table does not exist
+            if ($table_modification_time < $tabledata{'Update_time'}) {
+                $table_modification_time = $tabledata{'Update_time'};
+            }
+        }
+        $logthis->("Table modification time = ".$table_modification_time);
+        if ($table_modification_time > $backup_modification_time) {
+            # Save the current tables in case we need them another time.
+            my $backup_name = $gz_sql_filename.'.'.time;
+            $logthis->('Backing existing tables up in '.$backup_name);
+            &backup_tables($backup_name);
+        }
+        $time_this->();
+        &load_backup_tables($gz_sql_filename);
+        $time_this->('load backup tables');
+    }
 }
 
 ##
@@ -609,8 +642,8 @@
 sub load_backup_tables {
     my ($gz_sql_filename) = @_;
     if (-s $gz_sql_filename) {
-        &logthis('loading data from gzipped sql file');
-        my $command='gzip -dc activity.log.sql.gz | mysql --database=loncapa';
+        $logthis->('loading data from gzipped sql file');
+        my $command='gzip -dc '.$gz_sql_filename.' | mysql --database=loncapa';
         system($command);
         $logthis->('finished loading gzipped data');;
     } else {
@@ -621,14 +654,6 @@
 ##
 ## 
 ##
-sub initialize_configuration {
-    # Fake it for now:
-    $perlvar{'lonSqlUser'} = 'www';
-    $perlvar{'lonSqlAccess'} = 'localhostkey';
-    $perlvar{'lonUsersDir'} = '/home/httpd/lonUsers';
-    $perlvar{'lonDefDomain'} = '103';
-}
-
 sub update_process_name {
     my ($text) = @_;
     $0 = 'parse_activity_log.pl: '.$text;
Index: loncom/interface/lonmysql.pm
diff -u loncom/interface/lonmysql.pm:1.24 loncom/interface/lonmysql.pm:1.25
--- loncom/interface/lonmysql.pm:1.24	Thu Aug 19 21:27:05 2004
+++ loncom/interface/lonmysql.pm	Mon Dec 20 14:53:36 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # MySQL utility functions
 #
-# $Id: lonmysql.pm,v 1.24 2004/08/20 01:27:05 matthew Exp $
+# $Id: lonmysql.pm,v 1.25 2004/12/20 19:53:36 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -556,7 +556,12 @@
     #
     my @info=$sth->fetchrow_array;
     for (my $i=0;$i<= $#info ; $i++) {
-        $Tables{$tablename}->{$tabledesc[$i]}= $info[$i];
+        if ($tabledesc[$i] !~ /^(Create_|Update_|Check_)time$/) {
+            $Tables{$tablename}->{$tabledesc[$i]}= 
+                &unsqltime($info[$i]);
+        } else {
+            $Tables{$tablename}->{$tabledesc[$i]}= $info[$i];
+        }
     }
     #
     # Determine the column order
@@ -580,6 +585,29 @@
     $debugstring = "Retrieved table info for $tablename";
     return 1;
 }
+
+###############################
+
+=pod
+
+=item &table_information()
+
+Inputs: table id
+
+Returns: hash with the table status
+
+=cut
+
+###############################
+sub table_information {
+    my $table_id=shift;
+    if (&update_table_info($table_id)) {
+	return %{$Tables{$table_id}};
+    } else {
+	return ();
+    }
+}
+
 ###############################
 
 =pod
@@ -592,7 +620,7 @@
 
 =cut
 
-
+###############################
 sub col_order {
     my $table_id=shift;
     if (&update_table_info($table_id)) {