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

matthew lon-capa-cvs@mail.lon-capa.org
Wed, 09 Feb 2005 21:24:33 -0000


matthew		Wed Feb  9 16:24:33 2005 EDT

  Modified files:              
    /loncom/metadata_database	parse_activity_log.pl 
  Log:
  Many changes:
     corrected help text
     changed command line option 'drop' to 'dropwhendone'
     allow user to specify the name of the logfile
     call lonmysql::fix_table_name
     remove zero length files when done processing
     use "--quote-names " with mysqldump
     increase the amount of logging when table creation fails
  
  
Index: loncom/metadata_database/parse_activity_log.pl
diff -u loncom/metadata_database/parse_activity_log.pl:1.11 loncom/metadata_database/parse_activity_log.pl:1.12
--- loncom/metadata_database/parse_activity_log.pl:1.11	Wed Dec 22 15:42:39 2004
+++ loncom/metadata_database/parse_activity_log.pl	Wed Feb  9 16:24:33 2005
@@ -2,7 +2,7 @@
 #
 # The LearningOnline Network
 #
-# $Id: parse_activity_log.pl,v 1.11 2004/12/22 20:42:39 matthew Exp $
+# $Id: parse_activity_log.pl,v 1.12 2005/02/09 21:24:33 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -64,15 +64,16 @@
 
 #
 # Determine parameters
-my ($help,$course,$domain,$drop,$file,$time_run,$nocleanup,$log,$backup);
+my ($help,$course,$domain,$drop_when_done,$srcfile,$logfile,$time_run,$nocleanup,$log,$backup);
 &Getopt::Long::GetOptions( "course=s"  => \$course,
                            "domain=s"  => \$domain,
                            "backup"    => \$backup,
                            "help"      => \$help,
-                           "logfile=s" => \$file,
+                           "logfile=s" => \$logfile,
+                           "srcfile=s" => \$srcfile,
                            "timerun"   => \$time_run,
                            "nocleanup" => \$nocleanup,
-                           "drop"      => \$drop,
+                           "dropwhendone" => \$drop_when_done,
                            "log"       => \$log);
 if (! defined($course) || $help) {
     print<<USAGE;
@@ -81,18 +82,20 @@
 Process a lon-capa activity log into a database.
 Parameters:
    course             Required
-   domain             Optional
+   domain             optional
    backup             optional   if present, backup the activity log file
                                  before processing it
-   drop               optional   if present, drop all course 
-                                 specific activity log tables.
-   file               optional   Specify the file to parse, including path
+   dropwhendone       optional   if present, drop all course 
+                                 specific activity log tables after processing.
+   srcfile            optional   Specify the file to parse, including path
    time               optional   if present, print out timing data
    nocleanup          optional   if present, do not remove old files
    log                optional   if present, prepare log file of activity
+   logfile            optional   specifies the logfile to use
 Examples:
   $0 -course=123456abcdef -domain=msu
-  $0 -course=123456abcdef -file=activity.log
+  $0 -course=123456abcdef -srcfile=activity.log
+  $0 -course-123456abcdef -log -logfile=/tmp/logfile -dropwhendone
 USAGE
     exit;
 }
@@ -118,8 +121,11 @@
 ##
 ## Set up logging code
 my $logthis = \&nothing;
+
 if ($log) {
-    my $logfile = $perlvar{'lonDaemons'}.'/tmp/parse_activity_log.log.'.time;
+    if (! $logfile) {
+        $logfile = $perlvar{'lonDaemons'}.'/tmp/parse_activity_log.log.'.time;
+    }
     print STDERR "$0: logging to $logfile".$/;
     if (! open(LOGFILE,">$logfile")) {
         warn("Unable to open $logfile for writing.  Run aborted.");
@@ -137,8 +143,8 @@
 my $newfilename;      # $sourcefilename will be renamed to this
 my $gz_sql_filename;  # the gzipped mysql backup data file name.
 my $error_filename;   # Errors in parsing the activity log will be written here
-if ($file) {
-    $sourcefilename = $file;
+if ($srcfile) {
+    $sourcefilename = $srcfile;
 } else {
     $sourcefilename = &get_filename($course,$domain);
 }
@@ -187,7 +193,7 @@
 ## Table definitions
 ##
 my $prefix = $course.'_'.$domain.'_';
-my $student_table = $prefix.'students';
+my $student_table = &Apache::lonmysql::fix_table_name($prefix.'students');
 my $student_table_def = 
 { id => $student_table,
   permanent => 'no',
@@ -203,7 +209,7 @@
       'PRIMARY KEY' => ['student_id',],
           };
 
-my $res_table = $prefix.'resource';
+my $res_table = &Apache::lonmysql::fix_table_name($prefix.'resource');
 my $res_table_def = 
 { id => $res_table,
   permanent => 'no',
@@ -218,7 +224,7 @@
   'PRIMARY KEY' => ['res_id'],
 };
 
-#my $action_table = $prefix.'actions';
+#my $action_table = &Apache::lonmysql::fix_table_name($prefix.'actions');
 #my $action_table_def =
 #{ id => $action_table,
 #  permanent => 'no',
@@ -233,7 +239,7 @@
 #  'PRIMARY KEY' => ['action_id',], 
 #};
 
-my $machine_table = $prefix.'machine_table';
+my $machine_table = &Apache::lonmysql::fix_table_name($prefix.'machine_table');
 my $machine_table_def =
 { id => $machine_table,
   permanent => 'no',
@@ -248,7 +254,7 @@
   'PRIMARY KEY' => ['machine_id',],
  };
 
-my $activity_table = $prefix.'activity';
+my $activity_table = &Apache::lonmysql::fix_table_name($prefix.'activity');
 my $activity_table_def = 
 { id => $activity_table,
   permanent => 'no',
@@ -283,7 +289,7 @@
 my @Activity_Table = ($activity_table_def);
 my @ID_Tables = ($student_table_def,$res_table_def,$machine_table_def);
 ##
-## End of table definitions
+## End of table definitionsOB
 ##
 
 $logthis->('Connectiong to mysql');
@@ -296,8 +302,6 @@
 }
 $logthis->('SQL connection is up');
 
-if ($drop) { &drop_tables(); $logthis->('dropped tables'); }
-
 if (-s $gz_sql_filename) {
     my $backup_modification_time = (stat($gz_sql_filename))[9];
     $logthis->($gz_sql_filename.' was last modified '.
@@ -382,6 +386,7 @@
         &backup_tables($gz_sql_filename);
         $time_this->('write backup tables');
     }
+    if ($drop_when_done) { &drop_tables(); $logthis->('dropped tables'); }
 }
 close($error_fh);
 
@@ -405,6 +410,13 @@
     close LOGFILE;
 }
 
+foreach my $file ($lockfilename, $error_filename,$logfile) {
+    if (-z $file) { 
+        unlink($file); 
+    }
+}
+
+
 exit 0;   # Everything is okay, so end here before it gets worse.
 
 ########################################################
@@ -637,10 +649,10 @@
 ##
 sub backup_tables {
     my ($gz_sql_filename) = @_;
-    my $command = qq{mysqldump --opt loncapa };
-                             
+    my $command = qq{mysqldump --quote-names --opt loncapa };
     foreach my $table (@ID_Tables,@Activity_Table) {
         my $tablename = $table->{'id'};
+        $tablename =~ s/\`//g;
         $command .= $tablename.' ';
     }
     $command .= '| gzip >'.$gz_sql_filename;
@@ -680,11 +692,10 @@
 sub create_tables {
     foreach my $table (@ID_Tables,@Activity_Table) {
         my $table_id = &Apache::lonmysql::create_table($table);
-#        print STDERR "Unable to create table ".$table->{'id'}.$/;
-#        print STDERR join($/,&Apache::lonmysql::build_table_creation_request($table)).$/;
         if (! defined($table_id)) {
             warn "Unable to create table ".$table->{'id'}.$/;
-            warn join($/,&Apache::lonmysql::build_table_creation_request($table)).$/;
+            $logthis->('Unable to create table '.$table->{'id'});
+            $logthis->(join($/,&Apache::lonmysql::build_table_creation_request($table)));
             return 0;
         }
     }