Annotation of loncom/metadata_database/parse_activity_log.pl, revision 1.16
1.1 matthew 1: #!/usr/bin/perl
2: #
3: # The LearningOnline Network
4: #
1.16 ! matthew 5: # $Id: parse_activity_log.pl,v 1.15 2005/07/11 05:16:19 matthew Exp $
1.1 matthew 6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
1.3 matthew 29: #--------------------------------------------------------------------
1.1 matthew 30: #
31: # Exit codes
32: # 0 Everything is okay
33: # 1 Another copy is running on this course
34: # 2 Activity log does not exist
35: # 3 Unable to connect to database
36: # 4 Unable to create database tables
1.9 matthew 37: # 5 Unable to open log file
38: # 6 Unable to get lock on activity log
1.1 matthew 39: #
40:
1.8 matthew 41: #
42: # Notes:
43: #
44: # Logging is done via the $logthis variable, which may be the result of
45: # overcleverness. log via $logthis->('logtext'); Those are parentheses,
46: # not curly braces. If the -log command line parameter is set, the $logthis
47: # routine is set to a routine which writes to a file. If the command line
48: # parameter is not set $logthis is set to ¬hing, which does what you
49: # would expect.
50: #
51:
1.1 matthew 52: use strict;
53: use DBI;
1.9 matthew 54: use lib '/home/httpd/lib/perl/Apache';
1.8 matthew 55: use lib '/home/httpd/lib/perl/';
56: use LONCAPA::Configuration();
57: use Apache::lonmysql();
1.1 matthew 58: use lonmysql();
59: use Time::HiRes();
60: use Getopt::Long();
1.3 matthew 61: use IO::File;
1.5 matthew 62: use File::Copy;
1.7 matthew 63: use Fcntl qw(:flock);
1.14 matthew 64: use HTML::TokeParser;
1.7 matthew 65:
1.1 matthew 66: #
67: # Determine parameters
1.15 matthew 68: my ($help,$course,$domain,$drop_when_done,$srcfile,$logfile,$time_run,$nocleanup,$log,$backup,$xmlfile);
1.1 matthew 69: &Getopt::Long::GetOptions( "course=s" => \$course,
70: "domain=s" => \$domain,
1.5 matthew 71: "backup" => \$backup,
1.1 matthew 72: "help" => \$help,
1.12 matthew 73: "logfile=s" => \$logfile,
74: "srcfile=s" => \$srcfile,
1.15 matthew 75: "justloadxml=s" => \$xmlfile,
1.1 matthew 76: "timerun" => \$time_run,
77: "nocleanup" => \$nocleanup,
1.12 matthew 78: "dropwhendone" => \$drop_when_done,
1.2 matthew 79: "log" => \$log);
1.1 matthew 80: if (! defined($course) || $help) {
81: print<<USAGE;
82: parse_activity_log.pl
83:
84: Process a lon-capa activity log into a database.
85: Parameters:
86: course Required
1.12 matthew 87: domain optional
1.5 matthew 88: backup optional if present, backup the activity log file
89: before processing it
1.12 matthew 90: dropwhendone optional if present, drop all course
91: specific activity log tables after processing.
92: srcfile optional Specify the file to parse, including path
1.1 matthew 93: time optional if present, print out timing data
94: nocleanup optional if present, do not remove old files
1.2 matthew 95: log optional if present, prepare log file of activity
1.12 matthew 96: logfile optional specifies the logfile to use
1.1 matthew 97: Examples:
98: $0 -course=123456abcdef -domain=msu
1.12 matthew 99: $0 -course=123456abcdef -srcfile=activity.log
100: $0 -course-123456abcdef -log -logfile=/tmp/logfile -dropwhendone
1.1 matthew 101: USAGE
102: exit;
103: }
104:
105: ##
106: ## Set up timing code
107: my $time_this = \¬hing;
108: if ($time_run) {
109: $time_this = \&time_action;
110: }
111: my $initial_time = Time::HiRes::time;
112:
113: ##
1.3 matthew 114: ## Read in configuration parameters
115: ##
1.8 matthew 116: my %perlvar = %{&LONCAPA::Configuration::read_conf('loncapa.conf')};
117:
1.3 matthew 118: if (! defined($domain) || $domain eq '') {
119: $domain = $perlvar{'lonDefDomain'};
120: }
121: &update_process_name($course.'@'.$domain);
122:
123: ##
1.2 matthew 124: ## Set up logging code
125: my $logthis = \¬hing;
1.12 matthew 126:
1.2 matthew 127: if ($log) {
1.12 matthew 128: if (! $logfile) {
129: $logfile = $perlvar{'lonDaemons'}.'/tmp/parse_activity_log.log.'.time;
130: }
1.2 matthew 131: print STDERR "$0: logging to $logfile".$/;
132: if (! open(LOGFILE,">$logfile")) {
1.9 matthew 133: warn("Unable to open $logfile for writing. Run aborted.");
1.16 ! matthew 134: &cleanup_and_exit(5);
1.2 matthew 135: } else {
136: $logthis = \&log_to_file;
137: }
138: }
1.3 matthew 139:
1.1 matthew 140:
141: ##
142: ## Determine filenames
143: ##
144: my $sourcefilename; # activity log data
145: my $newfilename; # $sourcefilename will be renamed to this
1.3 matthew 146: my $error_filename; # Errors in parsing the activity log will be written here
1.12 matthew 147: if ($srcfile) {
148: $sourcefilename = $srcfile;
1.1 matthew 149: } else {
150: $sourcefilename = &get_filename($course,$domain);
151: }
1.6 matthew 152: my $sql_filename = $sourcefilename;
1.2 matthew 153: $sql_filename =~ s|[^/]*$|activity.log.sql|;
1.14 matthew 154: my $gz_sql_filename = $sql_filename.'.gz';
155: #
156: my $xml_filename = $sourcefilename;
157: my $gz_xml_filename = $xml_filename.'.gz';
1.15 matthew 158: if (defined($xmlfile)) {
159: $xml_filename = $xmlfile;
160: if ($xml_filename =~ /\.gz$/) {
161: $gz_xml_filename = $xml_filename;
162: } else {
163: $gz_xml_filename = $xml_filename.'.gz';
164: }
165: } else {
166: my $xml_filename = $sourcefilename;
167: $xml_filename =~ s|[^/]*$|activity.log.xml|;
168: $gz_xml_filename = $xml_filename.'.gz';
169: }
1.14 matthew 170: #
1.3 matthew 171: $error_filename = $sourcefilename;
172: $error_filename =~ s|[^/]*$|activity.log.errors|;
173: $logthis->('Beginning logging '.time);
1.1 matthew 174:
1.7 matthew 175: #
176: # Wait for a lock on the lockfile to avoid collisions
177: my $lockfilename = $sourcefilename.'.lock';
1.16 ! matthew 178: $newfilename = $sourcefilename.'.processing';
1.15 matthew 179: if (! defined($xmlfile)) {
1.16 ! matthew 180: open(LOCKFILE,'>'.$lockfilename);
! 181: if (!flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
! 182: warn("Unable to lock $lockfilename. Aborting".$/);
! 183: &clean_up_and_exit(6);
! 184: }
1.7 matthew 185:
1.16 ! matthew 186: if (! -e $newfilename && -e $sourcefilename) {
! 187: $logthis->('renaming '.$sourcefilename.' to '.$newfilename);
! 188: rename($sourcefilename,$newfilename);
! 189: Copy($newfilename,$newfilename.'.'.time) if ($backup);
! 190: $logthis->("renamed $sourcefilename to $newfilename");
! 191: } elsif (! -e $newfilename) {
! 192: utime(undef,undef,$newfilename);
! 193: }
1.1 matthew 194: }
195:
196: ##
197: ## Table definitions
198: ##
1.14 matthew 199: my %tables = &table_names($course,$domain);
1.1 matthew 200: my $student_table_def =
1.14 matthew 201: { id => $tables{'student'},
1.1 matthew 202: permanent => 'no',
203: columns => [
204: { name => 'student_id',
205: type => 'MEDIUMINT UNSIGNED',
206: restrictions => 'NOT NULL',
207: auto_inc => 'yes', },
208: { name => 'student',
209: type => 'VARCHAR(100) BINARY',
210: restrictions => 'NOT NULL', },
211: ],
212: 'PRIMARY KEY' => ['student_id',],
213: };
214:
215: my $res_table_def =
1.14 matthew 216: { id => $tables{'res'},
1.1 matthew 217: permanent => 'no',
218: columns => [{ name => 'res_id',
219: type => 'MEDIUMINT UNSIGNED',
220: restrictions => 'NOT NULL',
221: auto_inc => 'yes', },
222: { name => 'resource',
223: type => 'MEDIUMTEXT',
224: restrictions => 'NOT NULL'},
225: ],
226: 'PRIMARY KEY' => ['res_id'],
227: };
228:
1.4 matthew 229: #my $action_table_def =
230: #{ id => $action_table,
231: # permanent => 'no',
232: # columns => [{ name => 'action_id',
233: # type => 'MEDIUMINT UNSIGNED',
234: # restrictions => 'NOT NULL',
235: # auto_inc => 'yes', },
236: # { name => 'action',
237: # type => 'VARCHAR(100)',
238: # restrictions => 'NOT NULL'},
239: # ],
240: # 'PRIMARY KEY' => ['action_id',],
241: #};
1.1 matthew 242:
243: my $machine_table_def =
1.14 matthew 244: { id => $tables{'machine'},
1.1 matthew 245: permanent => 'no',
246: columns => [{ name => 'machine_id',
247: type => 'MEDIUMINT UNSIGNED',
248: restrictions => 'NOT NULL',
249: auto_inc => 'yes', },
250: { name => 'machine',
251: type => 'VARCHAR(100)',
252: restrictions => 'NOT NULL'},
253: ],
254: 'PRIMARY KEY' => ['machine_id',],
255: };
256:
257: my $activity_table_def =
1.14 matthew 258: { id => $tables{'activity'},
1.1 matthew 259: permanent => 'no',
260: columns => [
261: { name => 'res_id',
262: type => 'MEDIUMINT UNSIGNED',
263: restrictions => 'NOT NULL',},
264: { name => 'time',
265: type => 'DATETIME',
266: restrictions => 'NOT NULL',},
267: { name => 'student_id',
1.2 matthew 268: type => 'MEDIUMINT UNSIGNED',
1.1 matthew 269: restrictions => 'NOT NULL',},
1.4 matthew 270: { name => 'action',
271: type => 'VARCHAR(10)',
1.1 matthew 272: restrictions => 'NOT NULL',},
273: { name => 'idx', # This is here in case a student
274: type => 'MEDIUMINT UNSIGNED', # has multiple submissions during
275: restrictions => 'NOT NULL', # one second. It happens, trust
276: auto_inc => 'yes', }, # me.
277: { name => 'machine_id',
1.2 matthew 278: type => 'MEDIUMINT UNSIGNED',
1.1 matthew 279: restrictions => 'NOT NULL',},
280: { name => 'action_values',
281: type => 'MEDIUMTEXT', },
282: ],
1.4 matthew 283: 'PRIMARY KEY' => ['time','student_id','res_id','idx'],
284: 'KEY' => [{columns => ['student_id']},
285: {columns => ['time']},],
1.1 matthew 286: };
1.4 matthew 287:
1.8 matthew 288: my @Activity_Table = ($activity_table_def);
1.4 matthew 289: my @ID_Tables = ($student_table_def,$res_table_def,$machine_table_def);
1.14 matthew 290:
1.1 matthew 291: ##
1.13 matthew 292: ## End of table definitions
1.1 matthew 293: ##
1.14 matthew 294: $logthis->('tables = '.join(',',keys(%tables)));
1.1 matthew 295:
1.3 matthew 296: $logthis->('Connectiong to mysql');
1.8 matthew 297: &Apache::lonmysql::set_mysql_user_and_password('www',
1.1 matthew 298: $perlvar{'lonSqlAccess'});
299: if (!&Apache::lonmysql::verify_sql_connection()) {
300: warn "Unable to connect to MySQL database.";
1.2 matthew 301: $logthis->("Unable to connect to MySQL database.");
1.16 ! matthew 302: &clean_up_and_exit(3);
1.1 matthew 303: }
1.3 matthew 304: $logthis->('SQL connection is up');
305:
1.14 matthew 306: my $missing_table = &check_for_missing_tables(values(%tables));
307: if (-s $gz_sql_filename && ! -s $gz_xml_filename) {
1.8 matthew 308: my $backup_modification_time = (stat($gz_sql_filename))[9];
309: $logthis->($gz_sql_filename.' was last modified '.
310: localtime($backup_modification_time).
311: '('.$backup_modification_time.')');
1.14 matthew 312: if ($missing_table) {
313: # If the backup happened prior to the last table modification,
314: # we need to save the tables.
315: if (&latest_table_modification_time() > $backup_modification_time) {
316: # Save the current tables in case we need them another time.
317: $logthis->('Backing existing tables up');
318: &backup_tables_as_xml($gz_xml_filename.'.save_'.time,\%tables);
1.1 matthew 319: }
1.14 matthew 320: $time_this->();
321: &load_backup_sql_tables($gz_sql_filename);
322: &backup_tables_as_xml($gz_xml_filename,\%tables);
323: $time_this->('load backup tables');
1.1 matthew 324: }
1.14 matthew 325: } elsif (-s $gz_xml_filename) {
326: my $backup_modification_time = (stat($gz_xml_filename))[9];
327: $logthis->($gz_xml_filename.' was last modified '.
328: localtime($backup_modification_time).
329: '('.$backup_modification_time.')');
330: if ($missing_table) {
1.8 matthew 331: my $table_modification_time = $backup_modification_time;
332: # If the backup happened prior to the last table modification,
1.14 matthew 333: # we need to save the tables.
334: if (&latest_table_modification_time() > $backup_modification_time) {
1.8 matthew 335: # Save the current tables in case we need them another time.
1.14 matthew 336: $logthis->('Backing existing tables up');
337: &backup_tables_as_xml($gz_xml_filename.'.save_'.time,\%tables);
1.8 matthew 338: }
339: $time_this->();
1.14 matthew 340: # We have to make our own tables for the xml format
341: &drop_tables();
342: &create_tables();
343: &load_backup_xml_tables($gz_xml_filename,\%tables);
1.8 matthew 344: $time_this->('load backup tables');
1.14 matthew 345: }
1.1 matthew 346: }
347:
1.15 matthew 348: if (defined($xmlfile)) {
1.16 ! matthew 349: &clean_up_and_exit(0);
1.15 matthew 350: }
351:
1.3 matthew 352: ##
353: ## Ensure the tables we need exist
1.1 matthew 354: # create_tables does not complain if the tables already exist
1.3 matthew 355: $logthis->('creating tables');
1.1 matthew 356: if (! &create_tables()) {
357: warn "Unable to create tables";
1.2 matthew 358: $logthis->('Unable to create tables');
1.16 ! matthew 359: &clean_up_and_exit(4);
1.1 matthew 360: }
361:
1.3 matthew 362: ##
363: ## Read the ids used for various tables
1.2 matthew 364: $logthis->('reading id tables');
1.1 matthew 365: &read_id_tables();
1.2 matthew 366: $logthis->('finished reading id tables');
1.1 matthew 367:
368: ##
1.3 matthew 369: ## Set up the errors file
370: my $error_fh = IO::File->new(">>$error_filename");
371:
372: ##
373: ## Parse the course log
374: $logthis->('processing course log');
375: if (-s $newfilename) {
1.14 matthew 376: my $result = &process_courselog($newfilename,$error_fh,\%tables);
1.1 matthew 377: if (! defined($result)) {
378: # Something went wrong along the way...
1.2 matthew 379: $logthis->('process_courselog returned undef');
1.16 ! matthew 380: &clean_up_and_exit(5);
1.1 matthew 381: } elsif ($result > 0) {
382: $time_this->();
1.15 matthew 383: $logthis->('process_courselog returned '.$result.'.'.$/.
384: 'Backing up tables');
1.14 matthew 385: &backup_tables_as_xml($gz_xml_filename,\%tables);
1.1 matthew 386: $time_this->('write backup tables');
387: }
1.12 matthew 388: if ($drop_when_done) { &drop_tables(); $logthis->('dropped tables'); }
1.1 matthew 389: }
1.3 matthew 390: close($error_fh);
1.1 matthew 391:
392: ##
393: ## Clean up the filesystem
394: &Apache::lonmysql::disconnect_from_db();
1.3 matthew 395: unlink($newfilename) if (-e $newfilename && ! $nocleanup);
1.1 matthew 396:
1.3 matthew 397: ##
398: ## Print timing data
399: $logthis->('printing timing data');
1.1 matthew 400: if ($time_run) {
1.2 matthew 401: my $elapsed_time = Time::HiRes::time - $initial_time;
402: print "Overall time: ".$elapsed_time.$/;
1.1 matthew 403: print &outputtimes();
1.2 matthew 404: $logthis->("Overall time: ".$elapsed_time);
405: $logthis->(&outputtimes());
406: }
407:
1.16 ! matthew 408: &clean_up_and_exit(0);
! 409:
! 410: ########################################################
! 411: ########################################################
1.1 matthew 412:
1.16 ! matthew 413: sub clean_up_and_exit {
! 414: my ($exit_code) = @_;
! 415: # Close files
! 416: close(LOCKFILE);
! 417: close(LOGFILE);
! 418: # Remove zero length files
! 419: foreach my $file ($lockfilename, $error_filename,$logfile) {
! 420: if (defined($file) && -z $file) {
! 421: unlink($file);
! 422: }
1.12 matthew 423: }
1.16 ! matthew 424:
! 425: exit $exit_code;
1.12 matthew 426: }
427:
1.14 matthew 428: ########################################################
429: ########################################################
430: sub table_names {
431: my ($course,$domain) = @_;
432: my $prefix = $course.'_'.$domain.'_';
433: #
434: my %tables =
435: ( student =>&Apache::lonmysql::fix_table_name($prefix.'students'),
436: res =>&Apache::lonmysql::fix_table_name($prefix.'resource'),
437: machine =>&Apache::lonmysql::fix_table_name($prefix.'machine_table'),
438: activity=>&Apache::lonmysql::fix_table_name($prefix.'activity'),
439: );
440: return %tables;
441: }
1.1 matthew 442:
443: ########################################################
444: ########################################################
445: ##
446: ## Process Course Log
447: ##
448: ########################################################
449: ########################################################
450: #
451: # Returns the number of lines in the activity.log file that were processed.
452: sub process_courselog {
1.14 matthew 453: my ($inputfile,$error_fh,$tables) = @_;
1.1 matthew 454: if (! open(IN,$inputfile)) {
455: warn "Unable to open '$inputfile' for reading";
1.2 matthew 456: $logthis->("Unable to open '$inputfile' for reading");
1.1 matthew 457: return undef;
458: }
459: my ($linecount,$insertcount);
460: my $dbh = &Apache::lonmysql::get_dbh();
461: #
1.14 matthew 462: &store_entry();
1.1 matthew 463: while (my $line=<IN>){
464: # last if ($linecount > 1000);
465: #
466: # Bulk storage variables
467: $time_this->();
468: chomp($line);
469: $linecount++;
470: # print $linecount++.$/;
471: my ($timestamp,$host,$log)=split(/\:/,$line,3);
472: #
473: # $log has the actual log entries; currently still escaped, and
474: # %26(timestamp)%3a(url)%3a(user)%3a(domain)
475: # then additionally
476: # %3aPOST%3a(name)%3d(value)%3a(name)%3d(value)
477: # or
478: # %3aCSTORE%3a(name)%3d(value)%26(name)%3d(value)
479: #
480: # get delimiter between timestamped entries to be &&&
481: $log=~s/\%26(\d{9,10})\%3a/\&\&\&$1\%3a/g;
482: $log = &unescape($log);
483: # now go over all log entries
1.2 matthew 484: if (! defined($host)) { $host = 'unknown'; }
485: my $prevchunk = 'none';
486: foreach my $chunk (split(/\&\&\&/,$log)) {
487: my $warningflag = '';
488: my ($time,$res,$uname,$udom,$action,@values)= split(/:/,$chunk);
1.1 matthew 489: if (! defined($res) || $res =~ /^\s*$/) {
490: $res = '/adm/roles';
1.2 matthew 491: $action = 'LOGIN';
1.1 matthew 492: }
493: if ($res =~ m|^/prtspool/|) {
494: $res = '/prtspool/';
495: }
496: if (! defined($action) || $action eq '') {
1.2 matthew 497: $action = 'VIEW';
1.1 matthew 498: }
1.2 matthew 499: if ($action !~ /^(LOGIN|VIEW|POST|CSTORE|STORE)$/) {
500: $warningflag .= 'action';
1.3 matthew 501: print $error_fh 'full log entry:'.$log.$/;
502: print $error_fh 'error on chunk:'.$chunk.$/;
503: $logthis->('(action) Unable to parse '.$/.$chunk.$/.
504: 'got '.
505: 'time = '.$time.$/.
506: 'res = '.$res.$/.
507: 'uname= '.$uname.$/.
508: 'udom = '.$udom.$/.
509: 'action='.$action.$/.
1.11 matthew 510: '@values = '.join('&',@values));
1.3 matthew 511: next; #skip it if we cannot understand what is happening.
1.2 matthew 512: }
513: #
1.14 matthew 514: my %data = (student => $uname.':'.$udom,
515: resource => $res,
516: machine => $host,
517: action => $action,
518: time => &Apache::lonmysql::sqltime($time));
1.11 matthew 519: if ($action eq 'POST') {
1.14 matthew 520: $data{'action_values'} =
1.11 matthew 521: $dbh->quote(join('&',map { &escape($_); } @values));
522: } else {
1.14 matthew 523: $data{'action_values'} = $dbh->quote(join('&',@values));
524: }
525: my $error = &store_entry($dbh,$tables,\%data);
526: if ($error) {
527: $logthis->('error store_entry:'.$error." on %data");
1.11 matthew 528: }
1.2 matthew 529: $prevchunk = $chunk;
1.1 matthew 530: }
531: }
1.14 matthew 532: my $result = &store_entry($dbh,$tables);
533: if (! defined($result)) {
534: my $error = &Apache::lonmysql::get_error();
535: warn "Error occured during insert.".$error;
536: $logthis->('error = '.$error);
1.1 matthew 537: }
538: close IN;
539: return $linecount;
540: }
541:
1.2 matthew 542:
543: ##
1.14 matthew 544: ## default value for $logthis and $time_this
1.2 matthew 545: sub nothing {
546: return;
547: }
548:
549: ##
1.14 matthew 550: ## Logging routine (look for $log)
1.2 matthew 551: ##
552: sub log_to_file {
553: my ($input)=@_;
554: print LOGFILE $input.$/;
555: }
556:
1.1 matthew 557: ##
558: ## Timing routines
559: ##
560: {
561: my %Timing;
562: my $starttime;
563:
564: sub time_action {
565: my ($key) = @_;
566: if (defined($key)) {
567: $Timing{$key}+=Time::HiRes::time-$starttime;
568: $Timing{'count_'.$key}++;
569: }
570: $starttime = Time::HiRes::time;
571: }
572:
573: sub outputtimes {
574: my $Str;
575: if ($time_run) {
576: $Str = "Timing Data:".$/;
577: while (my($k,$v) = each(%Timing)) {
578: next if ($k =~ /^count_/);
579: my $count = $Timing{'count_'.$k};
580: $Str .=
581: ' '.sprintf("%25.25s",$k).
582: ' '.sprintf('% 8d',$count).
583: ' '.sprintf('%12.5f',$v).$/;
584: }
585: }
586: return $Str;
587: }
588:
589: }
590:
1.14 matthew 591: sub latest_table_modification_time {
592: my $latest_time;
593: foreach my $table (@Activity_Table,@ID_Tables) {
594: my %tabledata = &Apache::lonmysql::table_information($table->{'id'});
595: next if (! scalar(keys(%tabledata))); # table does not exist
596: if (! defined($latest_time) ||
597: $latest_time < $tabledata{'Update_time'}) {
598: $latest_time = $tabledata{'Update_time'};
599: }
600: }
601: return $latest_time;
602: }
603:
604: sub check_for_missing_tables {
605: my @wanted_tables = @_;
606: # Check for missing tables
607: my @Current_Tables = &Apache::lonmysql::tables_in_db();
608: my %Found;
609: foreach my $tablename (@Current_Tables) {
610: foreach my $table (@wanted_tables) {
611: if ($tablename eq $table) {
612: $Found{$tablename}++;
613: }
614: }
615: }
616: $logthis->('Found tables '.join(',',keys(%Found)));
617: my $missing_a_table = 0;
618: foreach my $table (@wanted_tables) {
619: if (! $Found{$table}) {
620: $logthis->('Missing table '.$table);
621: $missing_a_table = 1;
622: last;
623: }
624: }
625: return $missing_a_table;
626: }
1.1 matthew 627:
628: ##
629: ## Use mysqldump to store backups of the tables
630: ##
1.14 matthew 631: sub backup_tables_as_sql {
1.6 matthew 632: my ($gz_sql_filename) = @_;
1.12 matthew 633: my $command = qq{mysqldump --quote-names --opt loncapa };
1.3 matthew 634: foreach my $table (@ID_Tables,@Activity_Table) {
1.1 matthew 635: my $tablename = $table->{'id'};
1.12 matthew 636: $tablename =~ s/\`//g;
1.1 matthew 637: $command .= $tablename.' ';
638: }
1.6 matthew 639: $command .= '| gzip >'.$gz_sql_filename;
1.2 matthew 640: $logthis->($command);
1.1 matthew 641: system($command);
642: }
643:
644: ##
645: ## Load in mysqldumped files
646: ##
1.14 matthew 647: sub load_backup_sql_tables {
1.6 matthew 648: my ($gz_sql_filename) = @_;
649: if (-s $gz_sql_filename) {
1.8 matthew 650: $logthis->('loading data from gzipped sql file');
651: my $command='gzip -dc '.$gz_sql_filename.' | mysql --database=loncapa';
1.6 matthew 652: system($command);
653: $logthis->('finished loading gzipped data');;
654: } else {
655: return undef;
656: }
1.1 matthew 657: }
658:
659: ##
660: ##
661: ##
662: sub update_process_name {
663: my ($text) = @_;
664: $0 = 'parse_activity_log.pl: '.$text;
665: }
666:
667: sub get_filename {
668: my ($course,$domain) = @_;
669: my ($a,$b,$c,undef) = split('',$course,4);
670: return "$perlvar{'lonUsersDir'}/$domain/$a/$b/$c/$course/activity.log";
671: }
672:
673: sub create_tables {
1.3 matthew 674: foreach my $table (@ID_Tables,@Activity_Table) {
1.1 matthew 675: my $table_id = &Apache::lonmysql::create_table($table);
676: if (! defined($table_id)) {
677: warn "Unable to create table ".$table->{'id'}.$/;
1.12 matthew 678: $logthis->('Unable to create table '.$table->{'id'});
679: $logthis->(join($/,&Apache::lonmysql::build_table_creation_request($table)));
1.1 matthew 680: return 0;
681: }
682: }
683: return 1;
684: }
685:
686: sub drop_tables {
1.3 matthew 687: foreach my $table (@ID_Tables,@Activity_Table) {
1.1 matthew 688: my $table_id = $table->{'id'};
689: &Apache::lonmysql::drop_table($table_id);
690: }
691: }
692:
693: #################################################################
694: #################################################################
695: ##
696: ## Database item id code
697: ##
698: #################################################################
699: #################################################################
700: { # Scoping for ID lookup code
701: my %IDs;
702:
703: sub read_id_tables {
1.3 matthew 704: foreach my $table (@ID_Tables) {
1.1 matthew 705: my @Data = &Apache::lonmysql::get_rows($table->{'id'});
1.3 matthew 706: my $count = 0;
1.1 matthew 707: foreach my $row (@Data) {
708: $IDs{$table->{'id'}}->{$row->[1]} = $row->[0];
709: }
710: }
1.3 matthew 711: return;
1.1 matthew 712: }
713:
714: sub get_id {
715: my ($table,$fieldname,$value) = @_;
1.16 ! matthew 716: if (exists($IDs{$table}->{$value}) && $IDs{$table}->{$value} =~ /^\d+$/) {
1.1 matthew 717: return $IDs{$table}->{$value};
718: } else {
719: # insert into the table - if the item already exists, that is
720: # okay.
721: my $result = &Apache::lonmysql::store_row($table,[undef,$value]);
722: if (! defined($result)) {
1.16 ! matthew 723: warn("Got error on id insert for $value\n".
! 724: &Apache::lonmysql::get_error());
1.1 matthew 725: }
726: # get the id
1.16 ! matthew 727: my $id = &Apache::lonmysql::get_dbh()->{'mysql_insertid'};
! 728: if (defined($id)) {
! 729: $IDs{$table}->{$value}=$id;
1.1 matthew 730: } else {
1.2 matthew 731: $logthis->("Unable to retrieve id for $table $fieldname $value");
1.1 matthew 732: return undef;
733: }
734: }
735: }
736:
737: } # End of ID scoping
738:
1.14 matthew 739: ###############################################################
740: ###############################################################
741: ##
742: ## Save as XML
743: ##
744: ###############################################################
745: ###############################################################
746: sub backup_tables_as_xml {
747: my ($filename,$tables) = @_;
748: open(XMLFILE,"|gzip - > $filename") || return ('error:unable to write '.$filename);
749: my $query = qq{
750: SELECT B.resource,
751: A.time,
752: A.idx,
753: C.student,
754: A.action,
755: E.machine,
756: A.action_values
757: FROM $tables->{'activity'} AS A
758: LEFT JOIN $tables->{'res'} AS B ON B.res_id=A.res_id
759: LEFT JOIN $tables->{'student'} AS C ON C.student_id=A.student_id
760: LEFT JOIN $tables->{'machine'} AS E ON E.machine_id=A.machine_id
761: ORDER BY A.time DESC
762: };
763: $query =~ s/\s+/ /g;
764: my $dbh = &Apache::lonmysql::get_dbh();
765: my $sth = $dbh->prepare($query);
766: if (! $sth->execute()) {
767: $logthis->('<font color="blue">'.
768: 'WARNING: Could not retrieve from database:'.
769: $sth->errstr().'</font>');
770: return undef;
771: } else {
772: my ($res,$sqltime,$idx,$student,$action,$machine,$action_values);
773: if ($sth->bind_columns(\$res,\$sqltime,\$idx,\$student,\$action,
774: \$machine,\$action_values)) {
775:
776: while ($sth->fetch) {
777: print XMLFILE '<row>'.
778: qq{<resource>$res</resource>}.
779: qq{<time>$sqltime</time>}.
780: qq{<idx>$idx</idx>}.
781: qq{<student>$student</student>}.
782: qq{<action>$action</action>}.
783: qq{<machine>$machine</machine>}.
784: qq{<action_values>$action_values</action_values>}.
785: '</row>'.$/;
786: }
787: } else {
788: warn "Unable to bind to columns.\n";
789: return undef;
790: }
791: }
792: close XMLFILE;
793: return;
794: }
795:
796: ###############################################################
797: ###############################################################
798: ##
799: ## load as xml
800: ##
801: ###############################################################
802: ###############################################################
1.15 matthew 803: {
804: my @fields = ('resource','time',
805: 'student','action','idx','machine','action_values');
806: my %ids = ();
1.14 matthew 807: sub load_backup_xml_tables {
808: my ($filename,$tables) = @_;
1.16 ! matthew 809: my $dbh = &Apache::lonmysql::get_dbh();
1.14 matthew 810: my $xmlfh;
811: open($xmlfh,"cat $filename | gzip -d - |");
812: if (! defined($xmlfh)) {
813: return ('error:unable to read '.$filename);
814: }
1.15 matthew 815: #
816: %ids = (resource=> {"\0count"=>1},
817: student=> {"\0count"=>1},
818: machine=> {"\0count"=>1});
819: #
1.14 matthew 820: my %data;
1.15 matthew 821: while (my $inputline = <$xmlfh>) {
822: my ($resource,$time,undef,$student,$action,$machine,$action_values) =
823: ($inputline =~ m{<row>
824: <resource>(.*)</resource>
825: <time>(.*)</time>
826: <idx>(.*)</idx>
827: <student>(.*)</student>
828: <action>(.*)</action>
829: <machine>(.*)</machine>
830: <action_values>(.*)</action_values>
831: </row>$
832: }x
833: );
834: my $resource_id = &xml_get_id('resource',$resource);
835: my $student_id = &xml_get_id('student',$student);
836: my $machine_id = &xml_get_id('machine',$machine);
1.16 ! matthew 837: &xml_store_activity_row(map { defined($_)?$dbh->quote($_):''
1.15 matthew 838: } ($resource_id,
839: $time,
840: $student_id,
841: $action,
842: 'NULL',
843: $machine_id,
844: $action_values));
845: }
846: &xml_store_activity_row();
847: close($xmlfh);
848: # Store id tables
849: while (my ($id_name,$id_data) = each(%ids)) {
850: if ($id_name eq 'resource') { $id_name = 'res'; }
851: delete($id_data->{"\0count"});
852: &xml_store_id_table($id_name,$id_data);
853: }
854: return;
855: }
856:
857: sub xml_get_id {
858: my ($table,$element) = @_;
859: if (! exists($ids{$table}->{$element})) {
860: $ids{$table}->{$element} = $ids{$table}->{"\0count"}++;
861: }
862: return $ids{$table}->{$element};
863: }
864:
865: {
866: my @data_rows;
867: sub xml_store_activity_row {
868: my @data = @_;
869: if (scalar(@data)) {
870: push(@data_rows,[@data]);
871: }
872: if (! scalar(@data) || scalar(@data_rows) > 500) {
873: if (! &Apache::lonmysql::bulk_store_rows($tables{'activity'},
874: scalar(@{$data_rows[0]}),
875: \@data_rows)) {
876: $logthis->("Error:".&Apache::lonmysql::get_error());
877: warn("Error:".&Apache::lonmysql::get_error());
878: } else {
879: undef(@data_rows);
1.14 matthew 880: }
881: }
882: return;
883: }
884:
1.15 matthew 885: }
886:
887: sub xml_store_id_table {
888: my ($table,$tabledata) =@_;
1.16 ! matthew 889: my $dbh = &Apache::lonmysql::get_dbh();
1.15 matthew 890: if (! &Apache::lonmysql::bulk_store_rows
891: ($tables{$table},2,
1.16 ! matthew 892: [map{[$tabledata->{$_},$dbh->quote($_)]} keys(%$tabledata)])) {
1.15 matthew 893: $logthis->("Error:".&Apache::lonmysql::get_error());
894: warn "Error:".&Apache::lonmysql::get_error().$/;
895: }
896: }
897:
898: } # End of load xml scoping
1.14 matthew 899:
900: #######################################################################
901: #######################################################################
902: ##
903: ## store_entry - accumulate data to be inserted into the database
1.15 matthew 904: ##
1.14 matthew 905: ## Pass no values in to clear accumulator
906: ## Pass ($dbh,\%tables) to initiate storage of values
907: ## Pass ($dbh,\%tables,\%data) to use normally
908: ##
909: #######################################################################
910: #######################################################################
911: {
912: my @rows;
913: my $max_row_count = 100;
914:
915: sub store_entry {
916: if (! @_) {
917: undef(@rows);
918: return '';
919: }
920: my ($dbh,$tables,$data) = @_;
921: return if (! defined($tables));
922: if (defined($data)) {
923: my $error;
924: foreach my $field ('student','resource','action','time') {
925: if (! defined($data->{$field}) || $data->{$field} eq ':' ||
926: $data->{$field}=~ /^\s*$/) {
927: $error.=$field.',';
928: }
929: }
930: if ($error) { $error=~s/,$//; return $error; }
931: #
932: my $student_id = &get_id($tables->{'student'},'student',
933: $data->{'student'});
934: my $res_id = &get_id($tables->{'res'},
935: 'resource',$data->{'resource'});
936: my $machine_id = &get_id($tables->{'machine'},
937: 'machine',$data->{'machine'});
938: my $idx = $data->{'idx'}; if (! $idx) { $idx = "''"; }
939: #
940: push(@rows,[$res_id,
941: qq{'$data->{'time'}'},
942: $student_id,
943: qq{'$data->{'action'}'},
944: $idx,
945: $machine_id,
946: $data->{'action_values'}]);
947: }
1.15 matthew 948: if (defined($tables) &&
1.14 matthew 949: ( (! defined($data) && scalar(@rows)) || scalar(@rows)>$max_row_count)
950: ){
951: # Store the rows
1.15 matthew 952: my $result =
1.14 matthew 953: &Apache::lonmysql::bulk_store_rows($tables->{'activity'},
954: undef,
955: \@rows);
956: if (! defined($result)) {
957: my $error = &Apache::lonmysql::get_error();
958: warn "Error occured during insert.".$error;
959: return $error;
960: }
961: undef(@rows);
962: return $result if (! defined($data));
963: }
964: return '';
965: }
966:
967: } # end of scope for &store_entry
1.1 matthew 968:
969: ###############################################################
970: ###############################################################
971: ##
972: ## The usual suspects
973: ##
974: ###############################################################
975: ###############################################################
976: sub escape {
977: my $str=shift;
978: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
979: return $str;
980: }
981:
982: sub unescape {
983: my $str=shift;
984: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
985: return $str;
986: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>