--- loncom/metadata_database/parse_activity_log.pl 2005/02/09 21:24:33 1.12 +++ loncom/metadata_database/parse_activity_log.pl 2005/09/19 18:31:57 1.16 @@ -2,7 +2,7 @@ # # The LearningOnline Network # -# $Id: parse_activity_log.pl,v 1.12 2005/02/09 21:24:33 matthew Exp $ +# $Id: parse_activity_log.pl,v 1.16 2005/09/19 18:31:57 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -61,16 +61,18 @@ use Getopt::Long(); use IO::File; use File::Copy; use Fcntl qw(:flock); +use HTML::TokeParser; # # Determine parameters -my ($help,$course,$domain,$drop_when_done,$srcfile,$logfile,$time_run,$nocleanup,$log,$backup); +my ($help,$course,$domain,$drop_when_done,$srcfile,$logfile,$time_run,$nocleanup,$log,$backup,$xmlfile); &Getopt::Long::GetOptions( "course=s" => \$course, "domain=s" => \$domain, "backup" => \$backup, "help" => \$help, "logfile=s" => \$logfile, "srcfile=s" => \$srcfile, + "justloadxml=s" => \$xmlfile, "timerun" => \$time_run, "nocleanup" => \$nocleanup, "dropwhendone" => \$drop_when_done, @@ -129,7 +131,7 @@ if ($log) { print STDERR "$0: logging to $logfile".$/; if (! open(LOGFILE,">$logfile")) { warn("Unable to open $logfile for writing. Run aborted."); - exit 5; + &cleanup_and_exit(5); } else { $logthis = \&log_to_file; } @@ -141,7 +143,6 @@ if ($log) { ## my $sourcefilename; # activity log data 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 ($srcfile) { $sourcefilename = $srcfile; @@ -150,52 +151,54 @@ if ($srcfile) { } my $sql_filename = $sourcefilename; $sql_filename =~ s|[^/]*$|activity.log.sql|; -$gz_sql_filename = $sql_filename.'.gz'; +my $gz_sql_filename = $sql_filename.'.gz'; +# +my $xml_filename = $sourcefilename; +my $gz_xml_filename = $xml_filename.'.gz'; +if (defined($xmlfile)) { + $xml_filename = $xmlfile; + if ($xml_filename =~ /\.gz$/) { + $gz_xml_filename = $xml_filename; + } else { + $gz_xml_filename = $xml_filename.'.gz'; + } +} else { + my $xml_filename = $sourcefilename; + $xml_filename =~ s|[^/]*$|activity.log.xml|; + $gz_xml_filename = $xml_filename.'.gz'; +} +# $error_filename = $sourcefilename; $error_filename =~ s|[^/]*$|activity.log.errors|; $logthis->('Beginning logging '.time); - # # Wait for a lock on the lockfile to avoid collisions my $lockfilename = $sourcefilename.'.lock'; -open(LOCKFILE,'>'.$lockfilename); -if (!flock(LOCKFILE,LOCK_EX)) { - warn("Unable to lock $lockfilename. Aborting".$/); - exit 6; -} +$newfilename = $sourcefilename.'.processing'; +if (! defined($xmlfile)) { + open(LOCKFILE,'>'.$lockfilename); + if (!flock(LOCKFILE,LOCK_EX|LOCK_NB)) { + warn("Unable to lock $lockfilename. Aborting".$/); + &clean_up_and_exit(6); + } -## -## There will only be a $newfilename file if a copy of this program is already -## running. -my $newfilename = $sourcefilename.'.processing'; -if (-e $newfilename) { - warn "$newfilename exists"; - $logthis->($newfilename.' exists, so I cannot work on it.'); - exit 2; -} - -if (-e $sourcefilename) { - $logthis->('renaming '.$sourcefilename.' to '.$newfilename); - rename($sourcefilename,$newfilename); - Copy($newfilename,$newfilename.'.'.time) if ($backup); - $logthis->("renamed $sourcefilename to $newfilename"); -} else { - my $command = 'touch '.$newfilename; - $logthis->($command); - system($command); - $logthis->('touch was completed'); + if (! -e $newfilename && -e $sourcefilename) { + $logthis->('renaming '.$sourcefilename.' to '.$newfilename); + rename($sourcefilename,$newfilename); + Copy($newfilename,$newfilename.'.'.time) if ($backup); + $logthis->("renamed $sourcefilename to $newfilename"); + } elsif (! -e $newfilename) { + utime(undef,undef,$newfilename); + } } -close(LOCKFILE); - ## ## Table definitions ## -my $prefix = $course.'_'.$domain.'_'; -my $student_table = &Apache::lonmysql::fix_table_name($prefix.'students'); +my %tables = &table_names($course,$domain); my $student_table_def = -{ id => $student_table, +{ id => $tables{'student'}, permanent => 'no', columns => [ { name => 'student_id', @@ -209,9 +212,8 @@ my $student_table_def = 'PRIMARY KEY' => ['student_id',], }; -my $res_table = &Apache::lonmysql::fix_table_name($prefix.'resource'); my $res_table_def = -{ id => $res_table, +{ id => $tables{'res'}, permanent => 'no', columns => [{ name => 'res_id', type => 'MEDIUMINT UNSIGNED', @@ -224,7 +226,6 @@ my $res_table_def = 'PRIMARY KEY' => ['res_id'], }; -#my $action_table = &Apache::lonmysql::fix_table_name($prefix.'actions'); #my $action_table_def = #{ id => $action_table, # permanent => 'no', @@ -239,9 +240,8 @@ my $res_table_def = # 'PRIMARY KEY' => ['action_id',], #}; -my $machine_table = &Apache::lonmysql::fix_table_name($prefix.'machine_table'); my $machine_table_def = -{ id => $machine_table, +{ id => $tables{'machine'}, permanent => 'no', columns => [{ name => 'machine_id', type => 'MEDIUMINT UNSIGNED', @@ -254,9 +254,8 @@ my $machine_table_def = 'PRIMARY KEY' => ['machine_id',], }; -my $activity_table = &Apache::lonmysql::fix_table_name($prefix.'activity'); my $activity_table_def = -{ id => $activity_table, +{ id => $tables{'activity'}, permanent => 'no', columns => [ { name => 'res_id', @@ -288,9 +287,11 @@ my $activity_table_def = my @Activity_Table = ($activity_table_def); my @ID_Tables = ($student_table_def,$res_table_def,$machine_table_def); + ## -## End of table definitionsOB +## End of table definitions ## +$logthis->('tables = '.join(',',keys(%tables))); $logthis->('Connectiong to mysql'); &Apache::lonmysql::set_mysql_user_and_password('www', @@ -298,57 +299,54 @@ $logthis->('Connectiong to mysql'); if (!&Apache::lonmysql::verify_sql_connection()) { warn "Unable to connect to MySQL database."; $logthis->("Unable to connect to MySQL database."); - exit 3; + &clean_up_and_exit(3); } $logthis->('SQL connection is up'); -if (-s $gz_sql_filename) { +my $missing_table = &check_for_missing_tables(values(%tables)); +if (-s $gz_sql_filename && ! -s $gz_xml_filename) { 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) { - if ($tablename eq $table->{'id'}) { - $Found{$tablename}++; - } - } - } - $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'}}) { - $logthis->('Missing table '.$table->{'id'}); - $missing_a_table = 1; - last; + if ($missing_table) { + # If the backup happened prior to the last table modification, + # we need to save the tables. + if (&latest_table_modification_time() > $backup_modification_time) { + # Save the current tables in case we need them another time. + $logthis->('Backing existing tables up'); + &backup_tables_as_xml($gz_xml_filename.'.save_'.time,\%tables); } + $time_this->(); + &load_backup_sql_tables($gz_sql_filename); + &backup_tables_as_xml($gz_xml_filename,\%tables); + $time_this->('load backup tables'); } - if ($missing_a_table) { +} elsif (-s $gz_xml_filename) { + my $backup_modification_time = (stat($gz_xml_filename))[9]; + $logthis->($gz_xml_filename.' was last modified '. + localtime($backup_modification_time). + '('.$backup_modification_time.')'); + if ($missing_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) { + # we need to save the tables. + if (&latest_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); + $logthis->('Backing existing tables up'); + &backup_tables_as_xml($gz_xml_filename.'.save_'.time,\%tables); } $time_this->(); - &load_backup_tables($gz_sql_filename); + # We have to make our own tables for the xml format + &drop_tables(); + &create_tables(); + &load_backup_xml_tables($gz_xml_filename,\%tables); $time_this->('load backup tables'); - } + } +} + +if (defined($xmlfile)) { + &clean_up_and_exit(0); } ## @@ -358,7 +356,7 @@ $logthis->('creating tables'); if (! &create_tables()) { warn "Unable to create tables"; $logthis->('Unable to create tables'); - exit 4; + &clean_up_and_exit(4); } ## @@ -375,15 +373,16 @@ my $error_fh = IO::File->new(">>$error_f ## Parse the course log $logthis->('processing course log'); if (-s $newfilename) { - my $result = &process_courselog($newfilename,$error_fh); + my $result = &process_courselog($newfilename,$error_fh,\%tables); if (! defined($result)) { # Something went wrong along the way... $logthis->('process_courselog returned undef'); - exit 5; + &clean_up_and_exit(5); } elsif ($result > 0) { $time_this->(); - $logthis->('process_courselog returned '.$result.' backing up tables'); - &backup_tables($gz_sql_filename); + $logthis->('process_courselog returned '.$result.'.'.$/. + 'Backing up tables'); + &backup_tables_as_xml($gz_xml_filename,\%tables); $time_this->('write backup tables'); } if ($drop_when_done) { &drop_tables(); $logthis->('dropped tables'); } @@ -406,18 +405,40 @@ if ($time_run) { $logthis->(&outputtimes()); } -if ($log) { - close LOGFILE; -} +&clean_up_and_exit(0); -foreach my $file ($lockfilename, $error_filename,$logfile) { - if (-z $file) { - unlink($file); +######################################################## +######################################################## + +sub clean_up_and_exit { + my ($exit_code) = @_; + # Close files + close(LOCKFILE); + close(LOGFILE); + # Remove zero length files + foreach my $file ($lockfilename, $error_filename,$logfile) { + if (defined($file) && -z $file) { + unlink($file); + } } -} + exit $exit_code; +} -exit 0; # Everything is okay, so end here before it gets worse. +######################################################## +######################################################## +sub table_names { + my ($course,$domain) = @_; + my $prefix = $course.'_'.$domain.'_'; + # + my %tables = + ( student =>&Apache::lonmysql::fix_table_name($prefix.'students'), + res =>&Apache::lonmysql::fix_table_name($prefix.'resource'), + machine =>&Apache::lonmysql::fix_table_name($prefix.'machine_table'), + activity=>&Apache::lonmysql::fix_table_name($prefix.'activity'), + ); + return %tables; +} ######################################################## ######################################################## @@ -429,7 +450,7 @@ exit 0; # Everything is okay, so end h # # Returns the number of lines in the activity.log file that were processed. sub process_courselog { - my ($inputfile,$error_fh) = @_; + my ($inputfile,$error_fh,$tables) = @_; if (! open(IN,$inputfile)) { warn "Unable to open '$inputfile' for reading"; $logthis->("Unable to open '$inputfile' for reading"); @@ -438,8 +459,7 @@ sub process_courselog { my ($linecount,$insertcount); my $dbh = &Apache::lonmysql::get_dbh(); # - # Timing variables - my @RowData; + &store_entry(); while (my $line=){ # last if ($linecount > 1000); # @@ -449,7 +469,6 @@ sub process_courselog { $linecount++; # print $linecount++.$/; my ($timestamp,$host,$log)=split(/\:/,$line,3); - $time_this->('splitline'); # # $log has the actual log entries; currently still escaped, and # %26(timestamp)%3a(url)%3a(user)%3a(domain) @@ -461,16 +480,12 @@ sub process_courselog { # get delimiter between timestamped entries to be &&& $log=~s/\%26(\d{9,10})\%3a/\&\&\&$1\%3a/g; $log = &unescape($log); - $time_this->('translate_and_unescape'); # now go over all log entries if (! defined($host)) { $host = 'unknown'; } - my $machine_id = &get_id($machine_table,'machine',$host); my $prevchunk = 'none'; foreach my $chunk (split(/\&\&\&/,$log)) { my $warningflag = ''; - $time_this->(); my ($time,$res,$uname,$udom,$action,@values)= split(/:/,$chunk); - my $student = $uname.':'.$udom; if (! defined($res) || $res =~ /^\s*$/) { $res = '/adm/roles'; $action = 'LOGIN'; @@ -495,114 +510,44 @@ sub process_courselog { '@values = '.join('&',@values)); next; #skip it if we cannot understand what is happening. } - if (! defined($student) || $student eq ':') { - $student = 'unknown'; - $warningflag .= 'student'; - } - if (! defined($res) || $res =~ /^\s*$/) { - $res = 'unknown'; - $warningflag .= 'res'; - } - if (! defined($action) || $action =~ /^\s*$/) { - $action = 'unknown'; - $warningflag .= 'action'; - } - if (! defined($time) || $time !~ /^\d+$/) { - $time = 0; - $warningflag .= 'time'; - } - # - $time_this->('split_and_error_check'); - my $student_id = &get_id($student_table,'student',$student); - my $res_id = &get_id($res_table,'resource',$res); -# my $action_id = &get_id($action_table,'action',$action); - my $sql_time = &Apache::lonmysql::sqltime($time); - # - if (! defined($student_id) || $student_id eq '') { - $warningflag.='student_id'; - } - if (! defined($res_id) || $res_id eq '') { - $warningflag.='res_id'; - } -# if (! defined($action_id) || $action_id eq '') { -# $warningflag.='action_id'; -# } - if ($warningflag ne '') { - print $error_fh 'full log entry:'.$log.$/; - print $error_fh 'error on chunk:'.$chunk.$/; - $logthis->('warningflag ('.$warningflag.') on chunk '. - $/.$chunk.$/.'prevchunk = '.$/.$prevchunk); - $prevchunk .= $chunk; - next; # skip this chunk - } # - my $store_values; + my %data = (student => $uname.':'.$udom, + resource => $res, + machine => $host, + action => $action, + time => &Apache::lonmysql::sqltime($time)); if ($action eq 'POST') { - $store_values = + $data{'action_values'} = $dbh->quote(join('&',map { &escape($_); } @values)); } else { - $store_values = $dbh->quote(join('&',@values)); + $data{'action_values'} = $dbh->quote(join('&',@values)); } - $time_this->('get_ids'); - # - my $row = [$res_id, - qq{'$sql_time'}, - $student_id, - "'".$action."'", -# $action_id, - qq{''}, # idx - $machine_id, - $store_values]; - push(@RowData,$row); - $time_this->('push_row'); - $prevchunk = $chunk; - # - } - $time_this->(); - if ((scalar(@RowData) > 0) && ($linecount % 100 == 0)) { - my $result = &Apache::lonmysql::bulk_store_rows($activity_table, - undef, - \@RowData); - # $logthis->('result = '.$result); - $time_this->('bulk_store_rows'); - if (! defined($result)) { - my $error = &Apache::lonmysql::get_error(); - warn "Error occured during insert.".$error; - $logthis->('error = '.$error); + my $error = &store_entry($dbh,$tables,\%data); + if ($error) { + $logthis->('error store_entry:'.$error." on %data"); } - undef(@RowData); + $prevchunk = $chunk; } } - if (@RowData) { - $time_this->(); - $logthis->('storing '.$linecount); - my $result = &Apache::lonmysql::bulk_store_rows($activity_table, - undef, - \@RowData); - $logthis->('result = '.$result); - $time_this->('bulk_store_rows'); - if (! defined($result)) { - my $error = &Apache::lonmysql::get_error(); - warn "Error occured during insert.".$error; - $logthis->('error = '.$error); - } - undef(@RowData); + my $result = &store_entry($dbh,$tables); + if (! defined($result)) { + my $error = &Apache::lonmysql::get_error(); + warn "Error occured during insert.".$error; + $logthis->('error = '.$error); } close IN; -# print "Number of lines: ".$linecount.$/; -# print "Number of inserts: ".$insertcount.$/; return $linecount; } ## -## Somtimes, instead of doing something, doing nothing is appropriate. +## default value for $logthis and $time_this sub nothing { return; } ## -## Logging routine +## Logging routine (look for $log) ## sub log_to_file { my ($input)=@_; @@ -643,11 +588,47 @@ sub outputtimes { } +sub latest_table_modification_time { + my $latest_time; + 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 (! defined($latest_time) || + $latest_time < $tabledata{'Update_time'}) { + $latest_time = $tabledata{'Update_time'}; + } + } + return $latest_time; +} + +sub check_for_missing_tables { + my @wanted_tables = @_; + # Check for missing tables + my @Current_Tables = &Apache::lonmysql::tables_in_db(); + my %Found; + foreach my $tablename (@Current_Tables) { + foreach my $table (@wanted_tables) { + if ($tablename eq $table) { + $Found{$tablename}++; + } + } + } + $logthis->('Found tables '.join(',',keys(%Found))); + my $missing_a_table = 0; + foreach my $table (@wanted_tables) { + if (! $Found{$table}) { + $logthis->('Missing table '.$table); + $missing_a_table = 1; + last; + } + } + return $missing_a_table; +} ## ## Use mysqldump to store backups of the tables ## -sub backup_tables { +sub backup_tables_as_sql { my ($gz_sql_filename) = @_; my $command = qq{mysqldump --quote-names --opt loncapa }; foreach my $table (@ID_Tables,@Activity_Table) { @@ -663,7 +644,7 @@ sub backup_tables { ## ## Load in mysqldumped files ## -sub load_backup_tables { +sub load_backup_sql_tables { my ($gz_sql_filename) = @_; if (-s $gz_sql_filename) { $logthis->('loading data from gzipped sql file'); @@ -732,21 +713,20 @@ sub read_id_tables { sub get_id { my ($table,$fieldname,$value) = @_; - if (exists($IDs{$table}->{$value})) { + if (exists($IDs{$table}->{$value}) && $IDs{$table}->{$value} =~ /^\d+$/) { return $IDs{$table}->{$value}; } else { # insert into the table - if the item already exists, that is # okay. my $result = &Apache::lonmysql::store_row($table,[undef,$value]); if (! defined($result)) { - warn("Got error on id insert for $value\n".&Apache::lonmysql::get_error()); + warn("Got error on id insert for $value\n". + &Apache::lonmysql::get_error()); } # get the id - my @Data = - &Apache::lonmysql::get_rows($table,qq{$fieldname='$value'}); - if (@Data) { - $IDs{$table}->{$value}=$Data[0]->[0]; - return $IDs{$table}->{$value}; + my $id = &Apache::lonmysql::get_dbh()->{'mysql_insertid'}; + if (defined($id)) { + $IDs{$table}->{$value}=$id; } else { $logthis->("Unable to retrieve id for $table $fieldname $value"); return undef; @@ -756,6 +736,235 @@ sub get_id { } # End of ID scoping +############################################################### +############################################################### +## +## Save as XML +## +############################################################### +############################################################### +sub backup_tables_as_xml { + my ($filename,$tables) = @_; + open(XMLFILE,"|gzip - > $filename") || return ('error:unable to write '.$filename); + my $query = qq{ + SELECT B.resource, + A.time, + A.idx, + C.student, + A.action, + E.machine, + A.action_values + FROM $tables->{'activity'} AS A + LEFT JOIN $tables->{'res'} AS B ON B.res_id=A.res_id + LEFT JOIN $tables->{'student'} AS C ON C.student_id=A.student_id + LEFT JOIN $tables->{'machine'} AS E ON E.machine_id=A.machine_id + ORDER BY A.time DESC + }; + $query =~ s/\s+/ /g; + my $dbh = &Apache::lonmysql::get_dbh(); + my $sth = $dbh->prepare($query); + if (! $sth->execute()) { + $logthis->(''. + 'WARNING: Could not retrieve from database:'. + $sth->errstr().''); + return undef; + } else { + my ($res,$sqltime,$idx,$student,$action,$machine,$action_values); + if ($sth->bind_columns(\$res,\$sqltime,\$idx,\$student,\$action, + \$machine,\$action_values)) { + + while ($sth->fetch) { + print XMLFILE ''. + qq{$res}. + qq{}. + qq{$idx}. + qq{$student}. + qq{$action}. + qq{$machine}. + qq{$action_values}. + ''.$/; + } + } else { + warn "Unable to bind to columns.\n"; + return undef; + } + } + close XMLFILE; + return; +} + +############################################################### +############################################################### +## +## load as xml +## +############################################################### +############################################################### +{ + my @fields = ('resource','time', + 'student','action','idx','machine','action_values'); + my %ids = (); +sub load_backup_xml_tables { + my ($filename,$tables) = @_; + my $dbh = &Apache::lonmysql::get_dbh(); + my $xmlfh; + open($xmlfh,"cat $filename | gzip -d - |"); + if (! defined($xmlfh)) { + return ('error:unable to read '.$filename); + } + # + %ids = (resource=> {"\0count"=>1}, + student=> {"\0count"=>1}, + machine=> {"\0count"=>1}); + # + my %data; + while (my $inputline = <$xmlfh>) { + my ($resource,$time,undef,$student,$action,$machine,$action_values) = + ($inputline =~ m{ + (.*) + + (.*) + (.*) + (.*) + (.*) + (.*) + $ + }x + ); + my $resource_id = &xml_get_id('resource',$resource); + my $student_id = &xml_get_id('student',$student); + my $machine_id = &xml_get_id('machine',$machine); + &xml_store_activity_row(map { defined($_)?$dbh->quote($_):'' + } ($resource_id, + $time, + $student_id, + $action, + 'NULL', + $machine_id, + $action_values)); + } + &xml_store_activity_row(); + close($xmlfh); + # Store id tables + while (my ($id_name,$id_data) = each(%ids)) { + if ($id_name eq 'resource') { $id_name = 'res'; } + delete($id_data->{"\0count"}); + &xml_store_id_table($id_name,$id_data); + } + return; +} + +sub xml_get_id { + my ($table,$element) = @_; + if (! exists($ids{$table}->{$element})) { + $ids{$table}->{$element} = $ids{$table}->{"\0count"}++; + } + return $ids{$table}->{$element}; +} + +{ + my @data_rows; +sub xml_store_activity_row { + my @data = @_; + if (scalar(@data)) { + push(@data_rows,[@data]); + } + if (! scalar(@data) || scalar(@data_rows) > 500) { + if (! &Apache::lonmysql::bulk_store_rows($tables{'activity'}, + scalar(@{$data_rows[0]}), + \@data_rows)) { + $logthis->("Error:".&Apache::lonmysql::get_error()); + warn("Error:".&Apache::lonmysql::get_error()); + } else { + undef(@data_rows); + } + } + return; +} + +} + +sub xml_store_id_table { + my ($table,$tabledata) =@_; + my $dbh = &Apache::lonmysql::get_dbh(); + if (! &Apache::lonmysql::bulk_store_rows + ($tables{$table},2, + [map{[$tabledata->{$_},$dbh->quote($_)]} keys(%$tabledata)])) { + $logthis->("Error:".&Apache::lonmysql::get_error()); + warn "Error:".&Apache::lonmysql::get_error().$/; + } +} + +} # End of load xml scoping + +####################################################################### +####################################################################### +## +## store_entry - accumulate data to be inserted into the database +## +## Pass no values in to clear accumulator +## Pass ($dbh,\%tables) to initiate storage of values +## Pass ($dbh,\%tables,\%data) to use normally +## +####################################################################### +####################################################################### +{ + my @rows; + my $max_row_count = 100; + +sub store_entry { + if (! @_) { + undef(@rows); + return ''; + } + my ($dbh,$tables,$data) = @_; + return if (! defined($tables)); + if (defined($data)) { + my $error; + foreach my $field ('student','resource','action','time') { + if (! defined($data->{$field}) || $data->{$field} eq ':' || + $data->{$field}=~ /^\s*$/) { + $error.=$field.','; + } + } + if ($error) { $error=~s/,$//; return $error; } + # + my $student_id = &get_id($tables->{'student'},'student', + $data->{'student'}); + my $res_id = &get_id($tables->{'res'}, + 'resource',$data->{'resource'}); + my $machine_id = &get_id($tables->{'machine'}, + 'machine',$data->{'machine'}); + my $idx = $data->{'idx'}; if (! $idx) { $idx = "''"; } + # + push(@rows,[$res_id, + qq{'$data->{'time'}'}, + $student_id, + qq{'$data->{'action'}'}, + $idx, + $machine_id, + $data->{'action_values'}]); + } + if (defined($tables) && + ( (! defined($data) && scalar(@rows)) || scalar(@rows)>$max_row_count) + ){ + # Store the rows + my $result = + &Apache::lonmysql::bulk_store_rows($tables->{'activity'}, + undef, + \@rows); + if (! defined($result)) { + my $error = &Apache::lonmysql::get_error(); + warn "Error occured during insert.".$error; + return $error; + } + undef(@rows); + return $result if (! defined($data)); + } + return ''; +} + +} # end of scope for &store_entry ############################################################### ###############################################################