--- loncom/metadata_database/parse_activity_log.pl 2004/12/20 19:53:36 1.8 +++ loncom/metadata_database/parse_activity_log.pl 2005/02/09 21:54:15 1.13 @@ -2,7 +2,7 @@ # # The LearningOnline Network # -# $Id: parse_activity_log.pl,v 1.8 2004/12/20 19:53:36 matthew Exp $ +# $Id: parse_activity_log.pl,v 1.13 2005/02/09 21:54:15 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -34,7 +34,8 @@ # 2 Activity log does not exist # 3 Unable to connect to database # 4 Unable to create database tables -# 5 Unspecified error? +# 5 Unable to open log file +# 6 Unable to get lock on activity log # # @@ -50,6 +51,7 @@ use strict; use DBI; +use lib '/home/httpd/lib/perl/Apache'; use lib '/home/httpd/lib/perl/'; use LONCAPA::Configuration(); use Apache::lonmysql(); @@ -62,15 +64,16 @@ use Fcntl qw(:flock); # # 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<$logfile")) { - die "Unable to open $logfile for writing. Run aborted."; + warn("Unable to open $logfile for writing. Run aborted."); + exit 5; } else { $logthis = \&log_to_file; } @@ -134,8 +143,8 @@ my $sourcefilename; # activity log dat 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); } @@ -151,7 +160,10 @@ $logthis->('Beginning logging '.time); # Wait for a lock on the lockfile to avoid collisions my $lockfilename = $sourcefilename.'.lock'; open(LOCKFILE,'>'.$lockfilename); -flock(LOCKFILE,LOCK_EX) || die("Unable to lock $lockfilename. Aborting".$/); +if (!flock(LOCKFILE,LOCK_EX)) { + warn("Unable to lock $lockfilename. Aborting".$/); + exit 6; +} ## ## There will only be a $newfilename file if a copy of this program is already @@ -181,7 +193,7 @@ close(LOCKFILE); ## 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', @@ -197,7 +209,7 @@ my $student_table_def = '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', @@ -212,7 +224,7 @@ my $res_table_def = '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', @@ -227,7 +239,7 @@ my $res_table_def = # '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', @@ -242,7 +254,7 @@ my $machine_table_def = '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', @@ -290,8 +302,6 @@ if (!&Apache::lonmysql::verify_sql_conne } $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 '. @@ -376,6 +386,7 @@ if (-s $newfilename) { &backup_tables($gz_sql_filename); $time_this->('write backup tables'); } + if ($drop_when_done) { &drop_tables(); $logthis->('dropped tables'); } } close($error_fh); @@ -399,6 +410,13 @@ if ($log) { 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. ######################################################## @@ -474,7 +492,7 @@ sub process_courselog { 'uname= '.$uname.$/. 'udom = '.$udom.$/. 'action='.$action.$/. - '@values = '.join(':',@values)); + '@values = '.join('&',@values)); next; #skip it if we cannot understand what is happening. } if (! defined($student) || $student eq ':') { @@ -518,7 +536,13 @@ sub process_courselog { next; # skip this chunk } # - my $values = $dbh->quote(join('',@values)); + my $store_values; + if ($action eq 'POST') { + $store_values = + $dbh->quote(join('&',map { &escape($_); } @values)); + } else { + $store_values = $dbh->quote(join('&',@values)); + } $time_this->('get_ids'); # my $row = [$res_id, @@ -528,7 +552,7 @@ sub process_courselog { # $action_id, qq{''}, # idx $machine_id, - $values]; + $store_values]; push(@RowData,$row); $time_this->('push_row'); $prevchunk = $chunk; @@ -625,10 +649,10 @@ sub outputtimes { ## 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; @@ -668,11 +692,10 @@ sub get_filename { 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; } }