Annotation of loncom/metadata_database/parse_activity_log.pl, revision 1.20
1.1 matthew 1: #!/usr/bin/perl
2: #
3: # The LearningOnline Network
4: #
1.20 ! matthew 5: # $Id: parse_activity_log.pl,v 1.19 2005/09/20 17:49:51 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.17 albertel 134: &clean_up_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.19 matthew 147: my $chunk_filename; # where we save data we are not going to write to db
1.12 matthew 148: if ($srcfile) {
149: $sourcefilename = $srcfile;
1.1 matthew 150: } else {
151: $sourcefilename = &get_filename($course,$domain);
152: }
1.6 matthew 153: my $sql_filename = $sourcefilename;
1.2 matthew 154: $sql_filename =~ s|[^/]*$|activity.log.sql|;
1.14 matthew 155: my $gz_sql_filename = $sql_filename.'.gz';
156: #
1.19 matthew 157: $chunk_filename = $sourcefilename.".unprocessed_chunks";
158: #
1.14 matthew 159: my $xml_filename = $sourcefilename;
160: my $gz_xml_filename = $xml_filename.'.gz';
1.15 matthew 161: if (defined($xmlfile)) {
162: $xml_filename = $xmlfile;
163: if ($xml_filename =~ /\.gz$/) {
164: $gz_xml_filename = $xml_filename;
165: } else {
166: $gz_xml_filename = $xml_filename.'.gz';
167: }
168: } else {
169: my $xml_filename = $sourcefilename;
170: $xml_filename =~ s|[^/]*$|activity.log.xml|;
171: $gz_xml_filename = $xml_filename.'.gz';
172: }
1.14 matthew 173: #
1.3 matthew 174: $error_filename = $sourcefilename;
175: $error_filename =~ s|[^/]*$|activity.log.errors|;
176: $logthis->('Beginning logging '.time);
1.1 matthew 177:
1.7 matthew 178: #
179: # Wait for a lock on the lockfile to avoid collisions
180: my $lockfilename = $sourcefilename.'.lock';
1.16 matthew 181: $newfilename = $sourcefilename.'.processing';
1.15 matthew 182: if (! defined($xmlfile)) {
1.16 matthew 183: open(LOCKFILE,'>'.$lockfilename);
184: if (!flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
185: warn("Unable to lock $lockfilename. Aborting".$/);
1.17 albertel 186: # don't call clean_up_and_exit another instance is running and
187: # we don't want to 'cleanup' there files
188: exit 6;
1.16 matthew 189: }
1.7 matthew 190:
1.16 matthew 191: if (! -e $newfilename && -e $sourcefilename) {
192: $logthis->('renaming '.$sourcefilename.' to '.$newfilename);
193: rename($sourcefilename,$newfilename);
194: Copy($newfilename,$newfilename.'.'.time) if ($backup);
195: $logthis->("renamed $sourcefilename to $newfilename");
196: } elsif (! -e $newfilename) {
197: utime(undef,undef,$newfilename);
198: }
1.1 matthew 199: }
200:
201: ##
202: ## Table definitions
203: ##
1.14 matthew 204: my %tables = &table_names($course,$domain);
1.1 matthew 205: my $student_table_def =
1.14 matthew 206: { id => $tables{'student'},
1.1 matthew 207: permanent => 'no',
208: columns => [
209: { name => 'student_id',
210: type => 'MEDIUMINT UNSIGNED',
211: restrictions => 'NOT NULL',
212: auto_inc => 'yes', },
213: { name => 'student',
214: type => 'VARCHAR(100) BINARY',
215: restrictions => 'NOT NULL', },
216: ],
217: 'PRIMARY KEY' => ['student_id',],
218: };
219:
220: my $res_table_def =
1.14 matthew 221: { id => $tables{'res'},
1.1 matthew 222: permanent => 'no',
223: columns => [{ name => 'res_id',
224: type => 'MEDIUMINT UNSIGNED',
225: restrictions => 'NOT NULL',
226: auto_inc => 'yes', },
227: { name => 'resource',
228: type => 'MEDIUMTEXT',
229: restrictions => 'NOT NULL'},
230: ],
231: 'PRIMARY KEY' => ['res_id'],
232: };
233:
1.4 matthew 234: #my $action_table_def =
235: #{ id => $action_table,
236: # permanent => 'no',
237: # columns => [{ name => 'action_id',
238: # type => 'MEDIUMINT UNSIGNED',
239: # restrictions => 'NOT NULL',
240: # auto_inc => 'yes', },
241: # { name => 'action',
242: # type => 'VARCHAR(100)',
243: # restrictions => 'NOT NULL'},
244: # ],
245: # 'PRIMARY KEY' => ['action_id',],
246: #};
1.1 matthew 247:
248: my $machine_table_def =
1.14 matthew 249: { id => $tables{'machine'},
1.1 matthew 250: permanent => 'no',
251: columns => [{ name => 'machine_id',
252: type => 'MEDIUMINT UNSIGNED',
253: restrictions => 'NOT NULL',
254: auto_inc => 'yes', },
255: { name => 'machine',
256: type => 'VARCHAR(100)',
257: restrictions => 'NOT NULL'},
258: ],
259: 'PRIMARY KEY' => ['machine_id',],
260: };
261:
262: my $activity_table_def =
1.14 matthew 263: { id => $tables{'activity'},
1.1 matthew 264: permanent => 'no',
265: columns => [
266: { name => 'res_id',
267: type => 'MEDIUMINT UNSIGNED',
268: restrictions => 'NOT NULL',},
269: { name => 'time',
270: type => 'DATETIME',
271: restrictions => 'NOT NULL',},
272: { name => 'student_id',
1.2 matthew 273: type => 'MEDIUMINT UNSIGNED',
1.1 matthew 274: restrictions => 'NOT NULL',},
1.4 matthew 275: { name => 'action',
276: type => 'VARCHAR(10)',
1.1 matthew 277: restrictions => 'NOT NULL',},
278: { name => 'idx', # This is here in case a student
279: type => 'MEDIUMINT UNSIGNED', # has multiple submissions during
280: restrictions => 'NOT NULL', # one second. It happens, trust
281: auto_inc => 'yes', }, # me.
282: { name => 'machine_id',
1.2 matthew 283: type => 'MEDIUMINT UNSIGNED',
1.1 matthew 284: restrictions => 'NOT NULL',},
285: { name => 'action_values',
286: type => 'MEDIUMTEXT', },
287: ],
1.4 matthew 288: 'PRIMARY KEY' => ['time','student_id','res_id','idx'],
289: 'KEY' => [{columns => ['student_id']},
290: {columns => ['time']},],
1.1 matthew 291: };
1.4 matthew 292:
1.8 matthew 293: my @Activity_Table = ($activity_table_def);
1.4 matthew 294: my @ID_Tables = ($student_table_def,$res_table_def,$machine_table_def);
1.14 matthew 295:
1.1 matthew 296: ##
1.13 matthew 297: ## End of table definitions
1.1 matthew 298: ##
1.14 matthew 299: $logthis->('tables = '.join(',',keys(%tables)));
1.1 matthew 300:
1.3 matthew 301: $logthis->('Connectiong to mysql');
1.8 matthew 302: &Apache::lonmysql::set_mysql_user_and_password('www',
1.1 matthew 303: $perlvar{'lonSqlAccess'});
304: if (!&Apache::lonmysql::verify_sql_connection()) {
305: warn "Unable to connect to MySQL database.";
1.2 matthew 306: $logthis->("Unable to connect to MySQL database.");
1.16 matthew 307: &clean_up_and_exit(3);
1.1 matthew 308: }
1.3 matthew 309: $logthis->('SQL connection is up');
310:
1.14 matthew 311: my $missing_table = &check_for_missing_tables(values(%tables));
312: if (-s $gz_sql_filename && ! -s $gz_xml_filename) {
1.8 matthew 313: my $backup_modification_time = (stat($gz_sql_filename))[9];
314: $logthis->($gz_sql_filename.' was last modified '.
315: localtime($backup_modification_time).
316: '('.$backup_modification_time.')');
1.14 matthew 317: if ($missing_table) {
318: # If the backup happened prior to the last table modification,
319: # we need to save the tables.
320: if (&latest_table_modification_time() > $backup_modification_time) {
321: # Save the current tables in case we need them another time.
322: $logthis->('Backing existing tables up');
323: &backup_tables_as_xml($gz_xml_filename.'.save_'.time,\%tables);
1.1 matthew 324: }
1.14 matthew 325: $time_this->();
326: &load_backup_sql_tables($gz_sql_filename);
327: &backup_tables_as_xml($gz_xml_filename,\%tables);
328: $time_this->('load backup tables');
1.1 matthew 329: }
1.14 matthew 330: } elsif (-s $gz_xml_filename) {
331: my $backup_modification_time = (stat($gz_xml_filename))[9];
332: $logthis->($gz_xml_filename.' was last modified '.
333: localtime($backup_modification_time).
334: '('.$backup_modification_time.')');
335: if ($missing_table) {
1.8 matthew 336: my $table_modification_time = $backup_modification_time;
337: # If the backup happened prior to the last table modification,
1.14 matthew 338: # we need to save the tables.
339: if (&latest_table_modification_time() > $backup_modification_time) {
1.8 matthew 340: # Save the current tables in case we need them another time.
1.14 matthew 341: $logthis->('Backing existing tables up');
342: &backup_tables_as_xml($gz_xml_filename.'.save_'.time,\%tables);
1.8 matthew 343: }
344: $time_this->();
1.14 matthew 345: # We have to make our own tables for the xml format
346: &drop_tables();
347: &create_tables();
348: &load_backup_xml_tables($gz_xml_filename,\%tables);
1.8 matthew 349: $time_this->('load backup tables');
1.14 matthew 350: }
1.1 matthew 351: }
352:
1.15 matthew 353: if (defined($xmlfile)) {
1.16 matthew 354: &clean_up_and_exit(0);
1.15 matthew 355: }
356:
1.3 matthew 357: ##
358: ## Ensure the tables we need exist
1.1 matthew 359: # create_tables does not complain if the tables already exist
1.3 matthew 360: $logthis->('creating tables');
1.1 matthew 361: if (! &create_tables()) {
362: warn "Unable to create tables";
1.2 matthew 363: $logthis->('Unable to create tables');
1.16 matthew 364: &clean_up_and_exit(4);
1.1 matthew 365: }
366:
1.3 matthew 367: ##
368: ## Read the ids used for various tables
1.2 matthew 369: $logthis->('reading id tables');
1.1 matthew 370: &read_id_tables();
1.2 matthew 371: $logthis->('finished reading id tables');
1.1 matthew 372:
373: ##
1.3 matthew 374: ## Set up the errors file
375: my $error_fh = IO::File->new(">>$error_filename");
376:
377: ##
378: ## Parse the course log
379: $logthis->('processing course log');
380: if (-s $newfilename) {
1.14 matthew 381: my $result = &process_courselog($newfilename,$error_fh,\%tables);
1.1 matthew 382: if (! defined($result)) {
383: # Something went wrong along the way...
1.2 matthew 384: $logthis->('process_courselog returned undef');
1.16 matthew 385: &clean_up_and_exit(5);
1.1 matthew 386: } elsif ($result > 0) {
387: $time_this->();
1.15 matthew 388: $logthis->('process_courselog returned '.$result.'.'.$/.
389: 'Backing up tables');
1.14 matthew 390: &backup_tables_as_xml($gz_xml_filename,\%tables);
1.1 matthew 391: $time_this->('write backup tables');
392: }
1.12 matthew 393: if ($drop_when_done) { &drop_tables(); $logthis->('dropped tables'); }
1.1 matthew 394: }
1.3 matthew 395: close($error_fh);
1.1 matthew 396:
397: ##
398: ## Clean up the filesystem
399: &Apache::lonmysql::disconnect_from_db();
1.3 matthew 400: unlink($newfilename) if (-e $newfilename && ! $nocleanup);
1.1 matthew 401:
1.3 matthew 402: ##
403: ## Print timing data
404: $logthis->('printing timing data');
1.1 matthew 405: if ($time_run) {
1.2 matthew 406: my $elapsed_time = Time::HiRes::time - $initial_time;
407: print "Overall time: ".$elapsed_time.$/;
1.1 matthew 408: print &outputtimes();
1.2 matthew 409: $logthis->("Overall time: ".$elapsed_time);
410: $logthis->(&outputtimes());
411: }
412:
1.16 matthew 413: &clean_up_and_exit(0);
414:
415: ########################################################
416: ########################################################
1.1 matthew 417:
1.16 matthew 418: sub clean_up_and_exit {
419: my ($exit_code) = @_;
420: # Close files
421: close(LOCKFILE);
422: close(LOGFILE);
423: # Remove zero length files
424: foreach my $file ($lockfilename, $error_filename,$logfile) {
425: if (defined($file) && -z $file) {
426: unlink($file);
427: }
1.12 matthew 428: }
1.16 matthew 429:
430: exit $exit_code;
1.12 matthew 431: }
432:
1.14 matthew 433: ########################################################
434: ########################################################
435: sub table_names {
436: my ($course,$domain) = @_;
437: my $prefix = $course.'_'.$domain.'_';
438: #
439: my %tables =
440: ( student =>&Apache::lonmysql::fix_table_name($prefix.'students'),
441: res =>&Apache::lonmysql::fix_table_name($prefix.'resource'),
442: machine =>&Apache::lonmysql::fix_table_name($prefix.'machine_table'),
443: activity=>&Apache::lonmysql::fix_table_name($prefix.'activity'),
444: );
445: return %tables;
446: }
1.1 matthew 447:
448: ########################################################
449: ########################################################
450: ##
451: ## Process Course Log
452: ##
453: ########################################################
454: ########################################################
455: #
456: # Returns the number of lines in the activity.log file that were processed.
457: sub process_courselog {
1.14 matthew 458: my ($inputfile,$error_fh,$tables) = @_;
1.1 matthew 459: if (! open(IN,$inputfile)) {
460: warn "Unable to open '$inputfile' for reading";
1.2 matthew 461: $logthis->("Unable to open '$inputfile' for reading");
1.1 matthew 462: return undef;
463: }
464: my ($linecount,$insertcount);
465: my $dbh = &Apache::lonmysql::get_dbh();
466: #
1.14 matthew 467: &store_entry();
1.1 matthew 468: while (my $line=<IN>){
469: # last if ($linecount > 1000);
470: #
471: # Bulk storage variables
472: $time_this->();
473: chomp($line);
474: $linecount++;
475: # print $linecount++.$/;
476: my ($timestamp,$host,$log)=split(/\:/,$line,3);
477: #
478: # $log has the actual log entries; currently still escaped, and
479: # %26(timestamp)%3a(url)%3a(user)%3a(domain)
480: # then additionally
481: # %3aPOST%3a(name)%3d(value)%3a(name)%3d(value)
482: # or
483: # %3aCSTORE%3a(name)%3d(value)%26(name)%3d(value)
484: #
485: # get delimiter between timestamped entries to be &&&
486: $log=~s/\%26(\d{9,10})\%3a/\&\&\&$1\%3a/g;
487: $log = &unescape($log);
488: # now go over all log entries
1.2 matthew 489: if (! defined($host)) { $host = 'unknown'; }
490: my $prevchunk = 'none';
491: foreach my $chunk (split(/\&\&\&/,$log)) {
1.19 matthew 492: if (length($chunk) > 20000) {
493: # avoid putting too much data into the database
494: # (usually an uploaded file or something similar)
1.20 ! matthew 495: if (! &savechunk(\$chunk,$timestamp,$host)) {
1.19 matthew 496: close(IN);
497: return undef;
498: }
499: next;
500: }
1.2 matthew 501: my $warningflag = '';
502: my ($time,$res,$uname,$udom,$action,@values)= split(/:/,$chunk);
1.18 matthew 503: #
1.1 matthew 504: if (! defined($res) || $res =~ /^\s*$/) {
505: $res = '/adm/roles';
1.2 matthew 506: $action = 'LOGIN';
1.1 matthew 507: }
508: if ($res =~ m|^/prtspool/|) {
509: $res = '/prtspool/';
510: }
511: if (! defined($action) || $action eq '') {
1.2 matthew 512: $action = 'VIEW';
1.1 matthew 513: }
1.2 matthew 514: if ($action !~ /^(LOGIN|VIEW|POST|CSTORE|STORE)$/) {
515: $warningflag .= 'action';
1.3 matthew 516: print $error_fh 'full log entry:'.$log.$/;
1.20 ! matthew 517: print $error_fh 'error on chunk (saving)'.$/;
! 518: if (! &savechunk(\$chunk,$timestamp,$host)) {
! 519: close(IN);
! 520: return undef;
! 521: }
! 522: $logthis->('(action) Unable to parse chunk'.$/.
1.3 matthew 523: 'got '.
524: 'time = '.$time.$/.
525: 'res = '.$res.$/.
526: 'uname= '.$uname.$/.
527: 'udom = '.$udom.$/.
528: 'action='.$action.$/.
1.11 matthew 529: '@values = '.join('&',@values));
1.3 matthew 530: next; #skip it if we cannot understand what is happening.
1.2 matthew 531: }
532: #
1.14 matthew 533: my %data = (student => $uname.':'.$udom,
534: resource => $res,
535: machine => $host,
536: action => $action,
537: time => &Apache::lonmysql::sqltime($time));
1.11 matthew 538: if ($action eq 'POST') {
1.14 matthew 539: $data{'action_values'} =
1.11 matthew 540: $dbh->quote(join('&',map { &escape($_); } @values));
541: } else {
1.14 matthew 542: $data{'action_values'} = $dbh->quote(join('&',@values));
543: }
544: my $error = &store_entry($dbh,$tables,\%data);
545: if ($error) {
546: $logthis->('error store_entry:'.$error." on %data");
1.11 matthew 547: }
1.2 matthew 548: $prevchunk = $chunk;
1.1 matthew 549: }
550: }
1.14 matthew 551: my $result = &store_entry($dbh,$tables);
552: if (! defined($result)) {
553: my $error = &Apache::lonmysql::get_error();
554: warn "Error occured during insert.".$error;
555: $logthis->('error = '.$error);
1.1 matthew 556: }
557: close IN;
558: return $linecount;
1.20 ! matthew 559: ##
! 560: ##
! 561: sub savechunk {
! 562: my ($chunkref,$timestamp,$host) = @_;
! 563: my $chunk = &escape(${$chunkref});
! 564: if (! open(CHUNKFILE,">>$chunk_filename") ||
! 565: ! print CHUNKFILE $timestamp.':'.$host.':'.$chunk.$/) {
! 566: # abort
! 567: close(CHUNKFILE);
! 568: return 0;
! 569: }
! 570: close(CHUNKFILE);
! 571: return 1;
! 572: }
1.1 matthew 573: }
574:
1.2 matthew 575:
576: ##
1.14 matthew 577: ## default value for $logthis and $time_this
1.2 matthew 578: sub nothing {
579: return;
580: }
581:
582: ##
1.14 matthew 583: ## Logging routine (look for $log)
1.2 matthew 584: ##
585: sub log_to_file {
586: my ($input)=@_;
587: print LOGFILE $input.$/;
588: }
589:
1.1 matthew 590: ##
591: ## Timing routines
592: ##
593: {
594: my %Timing;
595: my $starttime;
596:
597: sub time_action {
598: my ($key) = @_;
599: if (defined($key)) {
600: $Timing{$key}+=Time::HiRes::time-$starttime;
601: $Timing{'count_'.$key}++;
602: }
603: $starttime = Time::HiRes::time;
604: }
605:
606: sub outputtimes {
607: my $Str;
608: if ($time_run) {
609: $Str = "Timing Data:".$/;
610: while (my($k,$v) = each(%Timing)) {
611: next if ($k =~ /^count_/);
612: my $count = $Timing{'count_'.$k};
613: $Str .=
614: ' '.sprintf("%25.25s",$k).
615: ' '.sprintf('% 8d',$count).
616: ' '.sprintf('%12.5f',$v).$/;
617: }
618: }
619: return $Str;
620: }
621:
622: }
623:
1.14 matthew 624: sub latest_table_modification_time {
625: my $latest_time;
626: foreach my $table (@Activity_Table,@ID_Tables) {
627: my %tabledata = &Apache::lonmysql::table_information($table->{'id'});
628: next if (! scalar(keys(%tabledata))); # table does not exist
629: if (! defined($latest_time) ||
630: $latest_time < $tabledata{'Update_time'}) {
631: $latest_time = $tabledata{'Update_time'};
632: }
633: }
634: return $latest_time;
635: }
636:
637: sub check_for_missing_tables {
638: my @wanted_tables = @_;
639: # Check for missing tables
640: my @Current_Tables = &Apache::lonmysql::tables_in_db();
641: my %Found;
642: foreach my $tablename (@Current_Tables) {
643: foreach my $table (@wanted_tables) {
644: if ($tablename eq $table) {
645: $Found{$tablename}++;
646: }
647: }
648: }
649: $logthis->('Found tables '.join(',',keys(%Found)));
650: my $missing_a_table = 0;
651: foreach my $table (@wanted_tables) {
652: if (! $Found{$table}) {
653: $logthis->('Missing table '.$table);
654: $missing_a_table = 1;
655: last;
656: }
657: }
658: return $missing_a_table;
659: }
1.1 matthew 660:
661: ##
662: ## Use mysqldump to store backups of the tables
663: ##
1.14 matthew 664: sub backup_tables_as_sql {
1.6 matthew 665: my ($gz_sql_filename) = @_;
1.12 matthew 666: my $command = qq{mysqldump --quote-names --opt loncapa };
1.3 matthew 667: foreach my $table (@ID_Tables,@Activity_Table) {
1.1 matthew 668: my $tablename = $table->{'id'};
1.12 matthew 669: $tablename =~ s/\`//g;
1.1 matthew 670: $command .= $tablename.' ';
671: }
1.6 matthew 672: $command .= '| gzip >'.$gz_sql_filename;
1.2 matthew 673: $logthis->($command);
1.1 matthew 674: system($command);
675: }
676:
677: ##
678: ## Load in mysqldumped files
679: ##
1.14 matthew 680: sub load_backup_sql_tables {
1.6 matthew 681: my ($gz_sql_filename) = @_;
682: if (-s $gz_sql_filename) {
1.8 matthew 683: $logthis->('loading data from gzipped sql file');
684: my $command='gzip -dc '.$gz_sql_filename.' | mysql --database=loncapa';
1.6 matthew 685: system($command);
686: $logthis->('finished loading gzipped data');;
687: } else {
688: return undef;
689: }
1.1 matthew 690: }
691:
692: ##
693: ##
694: ##
695: sub update_process_name {
696: my ($text) = @_;
697: $0 = 'parse_activity_log.pl: '.$text;
698: }
699:
700: sub get_filename {
701: my ($course,$domain) = @_;
702: my ($a,$b,$c,undef) = split('',$course,4);
703: return "$perlvar{'lonUsersDir'}/$domain/$a/$b/$c/$course/activity.log";
704: }
705:
706: sub create_tables {
1.3 matthew 707: foreach my $table (@ID_Tables,@Activity_Table) {
1.1 matthew 708: my $table_id = &Apache::lonmysql::create_table($table);
709: if (! defined($table_id)) {
710: warn "Unable to create table ".$table->{'id'}.$/;
1.12 matthew 711: $logthis->('Unable to create table '.$table->{'id'});
712: $logthis->(join($/,&Apache::lonmysql::build_table_creation_request($table)));
1.1 matthew 713: return 0;
714: }
715: }
716: return 1;
717: }
718:
719: sub drop_tables {
1.3 matthew 720: foreach my $table (@ID_Tables,@Activity_Table) {
1.1 matthew 721: my $table_id = $table->{'id'};
722: &Apache::lonmysql::drop_table($table_id);
723: }
724: }
725:
726: #################################################################
727: #################################################################
728: ##
729: ## Database item id code
730: ##
731: #################################################################
732: #################################################################
733: { # Scoping for ID lookup code
734: my %IDs;
735:
736: sub read_id_tables {
1.3 matthew 737: foreach my $table (@ID_Tables) {
1.1 matthew 738: my @Data = &Apache::lonmysql::get_rows($table->{'id'});
1.3 matthew 739: my $count = 0;
1.1 matthew 740: foreach my $row (@Data) {
741: $IDs{$table->{'id'}}->{$row->[1]} = $row->[0];
742: }
743: }
1.3 matthew 744: return;
1.1 matthew 745: }
746:
747: sub get_id {
748: my ($table,$fieldname,$value) = @_;
1.16 matthew 749: if (exists($IDs{$table}->{$value}) && $IDs{$table}->{$value} =~ /^\d+$/) {
1.1 matthew 750: return $IDs{$table}->{$value};
751: } else {
752: # insert into the table - if the item already exists, that is
753: # okay.
754: my $result = &Apache::lonmysql::store_row($table,[undef,$value]);
755: if (! defined($result)) {
1.16 matthew 756: warn("Got error on id insert for $value\n".
757: &Apache::lonmysql::get_error());
1.1 matthew 758: }
759: # get the id
1.16 matthew 760: my $id = &Apache::lonmysql::get_dbh()->{'mysql_insertid'};
761: if (defined($id)) {
762: $IDs{$table}->{$value}=$id;
1.1 matthew 763: } else {
1.2 matthew 764: $logthis->("Unable to retrieve id for $table $fieldname $value");
1.1 matthew 765: return undef;
766: }
767: }
768: }
769:
770: } # End of ID scoping
771:
1.14 matthew 772: ###############################################################
773: ###############################################################
774: ##
775: ## Save as XML
776: ##
777: ###############################################################
778: ###############################################################
779: sub backup_tables_as_xml {
780: my ($filename,$tables) = @_;
781: open(XMLFILE,"|gzip - > $filename") || return ('error:unable to write '.$filename);
782: my $query = qq{
783: SELECT B.resource,
784: A.time,
785: A.idx,
786: C.student,
787: A.action,
788: E.machine,
789: A.action_values
790: FROM $tables->{'activity'} AS A
791: LEFT JOIN $tables->{'res'} AS B ON B.res_id=A.res_id
792: LEFT JOIN $tables->{'student'} AS C ON C.student_id=A.student_id
793: LEFT JOIN $tables->{'machine'} AS E ON E.machine_id=A.machine_id
794: ORDER BY A.time DESC
795: };
796: $query =~ s/\s+/ /g;
797: my $dbh = &Apache::lonmysql::get_dbh();
798: my $sth = $dbh->prepare($query);
799: if (! $sth->execute()) {
800: $logthis->('<font color="blue">'.
801: 'WARNING: Could not retrieve from database:'.
802: $sth->errstr().'</font>');
803: return undef;
804: } else {
805: my ($res,$sqltime,$idx,$student,$action,$machine,$action_values);
806: if ($sth->bind_columns(\$res,\$sqltime,\$idx,\$student,\$action,
807: \$machine,\$action_values)) {
808:
809: while ($sth->fetch) {
810: print XMLFILE '<row>'.
811: qq{<resource>$res</resource>}.
812: qq{<time>$sqltime</time>}.
813: qq{<idx>$idx</idx>}.
814: qq{<student>$student</student>}.
815: qq{<action>$action</action>}.
816: qq{<machine>$machine</machine>}.
817: qq{<action_values>$action_values</action_values>}.
818: '</row>'.$/;
819: }
820: } else {
821: warn "Unable to bind to columns.\n";
822: return undef;
823: }
824: }
825: close XMLFILE;
826: return;
827: }
828:
829: ###############################################################
830: ###############################################################
831: ##
832: ## load as xml
833: ##
834: ###############################################################
835: ###############################################################
1.15 matthew 836: {
837: my @fields = ('resource','time',
838: 'student','action','idx','machine','action_values');
839: my %ids = ();
1.14 matthew 840: sub load_backup_xml_tables {
841: my ($filename,$tables) = @_;
1.16 matthew 842: my $dbh = &Apache::lonmysql::get_dbh();
1.14 matthew 843: my $xmlfh;
844: open($xmlfh,"cat $filename | gzip -d - |");
845: if (! defined($xmlfh)) {
846: return ('error:unable to read '.$filename);
847: }
1.15 matthew 848: #
849: %ids = (resource=> {"\0count"=>1},
850: student=> {"\0count"=>1},
851: machine=> {"\0count"=>1});
852: #
1.14 matthew 853: my %data;
1.15 matthew 854: while (my $inputline = <$xmlfh>) {
855: my ($resource,$time,undef,$student,$action,$machine,$action_values) =
856: ($inputline =~ m{<row>
857: <resource>(.*)</resource>
858: <time>(.*)</time>
859: <idx>(.*)</idx>
860: <student>(.*)</student>
861: <action>(.*)</action>
862: <machine>(.*)</machine>
863: <action_values>(.*)</action_values>
864: </row>$
865: }x
866: );
867: my $resource_id = &xml_get_id('resource',$resource);
868: my $student_id = &xml_get_id('student',$student);
869: my $machine_id = &xml_get_id('machine',$machine);
1.16 matthew 870: &xml_store_activity_row(map { defined($_)?$dbh->quote($_):''
1.15 matthew 871: } ($resource_id,
872: $time,
873: $student_id,
874: $action,
875: 'NULL',
876: $machine_id,
877: $action_values));
878: }
879: &xml_store_activity_row();
880: close($xmlfh);
881: # Store id tables
882: while (my ($id_name,$id_data) = each(%ids)) {
883: if ($id_name eq 'resource') { $id_name = 'res'; }
884: delete($id_data->{"\0count"});
885: &xml_store_id_table($id_name,$id_data);
886: }
887: return;
888: }
889:
890: sub xml_get_id {
891: my ($table,$element) = @_;
892: if (! exists($ids{$table}->{$element})) {
893: $ids{$table}->{$element} = $ids{$table}->{"\0count"}++;
894: }
895: return $ids{$table}->{$element};
896: }
897:
898: {
899: my @data_rows;
900: sub xml_store_activity_row {
901: my @data = @_;
902: if (scalar(@data)) {
903: push(@data_rows,[@data]);
904: }
905: if (! scalar(@data) || scalar(@data_rows) > 500) {
906: if (! &Apache::lonmysql::bulk_store_rows($tables{'activity'},
907: scalar(@{$data_rows[0]}),
908: \@data_rows)) {
909: $logthis->("Error:".&Apache::lonmysql::get_error());
910: warn("Error:".&Apache::lonmysql::get_error());
911: } else {
912: undef(@data_rows);
1.14 matthew 913: }
914: }
915: return;
916: }
917:
1.15 matthew 918: }
919:
920: sub xml_store_id_table {
921: my ($table,$tabledata) =@_;
1.16 matthew 922: my $dbh = &Apache::lonmysql::get_dbh();
1.15 matthew 923: if (! &Apache::lonmysql::bulk_store_rows
924: ($tables{$table},2,
1.16 matthew 925: [map{[$tabledata->{$_},$dbh->quote($_)]} keys(%$tabledata)])) {
1.15 matthew 926: $logthis->("Error:".&Apache::lonmysql::get_error());
927: warn "Error:".&Apache::lonmysql::get_error().$/;
928: }
929: }
930:
931: } # End of load xml scoping
1.14 matthew 932:
933: #######################################################################
934: #######################################################################
935: ##
936: ## store_entry - accumulate data to be inserted into the database
1.15 matthew 937: ##
1.14 matthew 938: ## Pass no values in to clear accumulator
939: ## Pass ($dbh,\%tables) to initiate storage of values
940: ## Pass ($dbh,\%tables,\%data) to use normally
941: ##
942: #######################################################################
943: #######################################################################
944: {
945: my @rows;
946: my $max_row_count = 100;
947:
948: sub store_entry {
949: if (! @_) {
950: undef(@rows);
951: return '';
952: }
953: my ($dbh,$tables,$data) = @_;
954: return if (! defined($tables));
955: if (defined($data)) {
956: my $error;
957: foreach my $field ('student','resource','action','time') {
958: if (! defined($data->{$field}) || $data->{$field} eq ':' ||
959: $data->{$field}=~ /^\s*$/) {
960: $error.=$field.',';
961: }
962: }
963: if ($error) { $error=~s/,$//; return $error; }
964: #
965: my $student_id = &get_id($tables->{'student'},'student',
966: $data->{'student'});
967: my $res_id = &get_id($tables->{'res'},
968: 'resource',$data->{'resource'});
969: my $machine_id = &get_id($tables->{'machine'},
970: 'machine',$data->{'machine'});
971: my $idx = $data->{'idx'}; if (! $idx) { $idx = "''"; }
972: #
973: push(@rows,[$res_id,
974: qq{'$data->{'time'}'},
975: $student_id,
976: qq{'$data->{'action'}'},
977: $idx,
978: $machine_id,
979: $data->{'action_values'}]);
980: }
1.15 matthew 981: if (defined($tables) &&
1.14 matthew 982: ( (! defined($data) && scalar(@rows)) || scalar(@rows)>$max_row_count)
983: ){
984: # Store the rows
1.15 matthew 985: my $result =
1.14 matthew 986: &Apache::lonmysql::bulk_store_rows($tables->{'activity'},
987: undef,
988: \@rows);
989: if (! defined($result)) {
990: my $error = &Apache::lonmysql::get_error();
991: warn "Error occured during insert.".$error;
992: return $error;
993: }
994: undef(@rows);
995: return $result if (! defined($data));
996: }
997: return '';
998: }
999:
1000: } # end of scope for &store_entry
1.1 matthew 1001:
1002: ###############################################################
1003: ###############################################################
1004: ##
1005: ## The usual suspects
1006: ##
1007: ###############################################################
1008: ###############################################################
1009: sub escape {
1010: my $str=shift;
1011: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
1012: return $str;
1013: }
1014:
1015: sub unescape {
1016: my $str=shift;
1017: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1018: return $str;
1019: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>