Annotation of loncom/metadata_database/parse_activity_log.pl, revision 1.26
1.1 matthew 1: #!/usr/bin/perl
2: #
3: # The LearningOnline Network
4: #
1.26 ! raeburn 5: # $Id: parse_activity_log.pl,v 1.25 2014/11/24 02:36:34 raeburn 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: #
1.1 matthew 51: use strict;
52: use DBI;
1.8 matthew 53: use lib '/home/httpd/lib/perl/';
54: use LONCAPA::Configuration();
55: use Apache::lonmysql();
1.1 matthew 56: use Time::HiRes();
57: use Getopt::Long();
1.3 matthew 58: use IO::File;
1.5 matthew 59: use File::Copy;
1.7 matthew 60: use Fcntl qw(:flock);
1.14 matthew 61: use HTML::TokeParser;
1.7 matthew 62:
1.1 matthew 63: #
64: # Determine parameters
1.15 matthew 65: my ($help,$course,$domain,$drop_when_done,$srcfile,$logfile,$time_run,$nocleanup,$log,$backup,$xmlfile);
1.1 matthew 66: &Getopt::Long::GetOptions( "course=s" => \$course,
67: "domain=s" => \$domain,
1.5 matthew 68: "backup" => \$backup,
1.1 matthew 69: "help" => \$help,
1.12 matthew 70: "logfile=s" => \$logfile,
71: "srcfile=s" => \$srcfile,
1.15 matthew 72: "justloadxml=s" => \$xmlfile,
1.1 matthew 73: "timerun" => \$time_run,
74: "nocleanup" => \$nocleanup,
1.12 matthew 75: "dropwhendone" => \$drop_when_done,
1.2 matthew 76: "log" => \$log);
1.1 matthew 77: if (! defined($course) || $help) {
78: print<<USAGE;
79: parse_activity_log.pl
80:
81: Process a lon-capa activity log into a database.
82: Parameters:
83: course Required
1.12 matthew 84: domain optional
1.5 matthew 85: backup optional if present, backup the activity log file
86: before processing it
1.12 matthew 87: dropwhendone optional if present, drop all course
88: specific activity log tables after processing.
89: srcfile optional Specify the file to parse, including path
1.1 matthew 90: time optional if present, print out timing data
91: nocleanup optional if present, do not remove old files
1.2 matthew 92: log optional if present, prepare log file of activity
1.12 matthew 93: logfile optional specifies the logfile to use
1.1 matthew 94: Examples:
95: $0 -course=123456abcdef -domain=msu
1.12 matthew 96: $0 -course=123456abcdef -srcfile=activity.log
97: $0 -course-123456abcdef -log -logfile=/tmp/logfile -dropwhendone
1.1 matthew 98: USAGE
99: exit;
100: }
101:
102: ##
103: ## Set up timing code
104: my $time_this = \¬hing;
105: if ($time_run) {
106: $time_this = \&time_action;
107: }
108: my $initial_time = Time::HiRes::time;
109:
110: ##
1.3 matthew 111: ## Read in configuration parameters
112: ##
1.8 matthew 113: my %perlvar = %{&LONCAPA::Configuration::read_conf('loncapa.conf')};
114:
1.3 matthew 115: if (! defined($domain) || $domain eq '') {
116: $domain = $perlvar{'lonDefDomain'};
117: }
118: &update_process_name($course.'@'.$domain);
119:
120: ##
1.2 matthew 121: ## Set up logging code
122: my $logthis = \¬hing;
1.12 matthew 123:
1.2 matthew 124: if ($log) {
1.12 matthew 125: if (! $logfile) {
126: $logfile = $perlvar{'lonDaemons'}.'/tmp/parse_activity_log.log.'.time;
127: }
1.2 matthew 128: print STDERR "$0: logging to $logfile".$/;
129: if (! open(LOGFILE,">$logfile")) {
1.9 matthew 130: warn("Unable to open $logfile for writing. Run aborted.");
1.17 albertel 131: &clean_up_and_exit(5);
1.2 matthew 132: } else {
133: $logthis = \&log_to_file;
134: }
135: }
1.3 matthew 136:
1.1 matthew 137:
138: ##
139: ## Determine filenames
140: ##
141: my $sourcefilename; # activity log data
142: my $newfilename; # $sourcefilename will be renamed to this
1.3 matthew 143: my $error_filename; # Errors in parsing the activity log will be written here
1.19 matthew 144: my $chunk_filename; # where we save data we are not going to write to db
1.12 matthew 145: if ($srcfile) {
146: $sourcefilename = $srcfile;
1.1 matthew 147: } else {
148: $sourcefilename = &get_filename($course,$domain);
149: }
1.6 matthew 150: my $sql_filename = $sourcefilename;
1.2 matthew 151: $sql_filename =~ s|[^/]*$|activity.log.sql|;
1.14 matthew 152: my $gz_sql_filename = $sql_filename.'.gz';
153: #
1.19 matthew 154: $chunk_filename = $sourcefilename.".unprocessed_chunks";
155: #
1.14 matthew 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".$/);
1.17 albertel 183: # don't call clean_up_and_exit another instance is running and
1.22 albertel 184: # we don't want to 'cleanup' their files
1.17 albertel 185: exit 6;
1.16 matthew 186: }
1.7 matthew 187:
1.16 matthew 188: if (! -e $newfilename && -e $sourcefilename) {
189: $logthis->('renaming '.$sourcefilename.' to '.$newfilename);
190: rename($sourcefilename,$newfilename);
191: Copy($newfilename,$newfilename.'.'.time) if ($backup);
192: $logthis->("renamed $sourcefilename to $newfilename");
193: } elsif (! -e $newfilename) {
194: utime(undef,undef,$newfilename);
195: }
1.1 matthew 196: }
197:
198: ##
199: ## Table definitions
200: ##
1.14 matthew 201: my %tables = &table_names($course,$domain);
1.1 matthew 202: my $student_table_def =
1.14 matthew 203: { id => $tables{'student'},
1.1 matthew 204: permanent => 'no',
205: columns => [
206: { name => 'student_id',
207: type => 'MEDIUMINT UNSIGNED',
208: restrictions => 'NOT NULL',
209: auto_inc => 'yes', },
210: { name => 'student',
211: type => 'VARCHAR(100) BINARY',
212: restrictions => 'NOT NULL', },
213: ],
214: 'PRIMARY KEY' => ['student_id',],
215: };
216:
217: my $res_table_def =
1.14 matthew 218: { id => $tables{'res'},
1.1 matthew 219: permanent => 'no',
220: columns => [{ name => 'res_id',
221: type => 'MEDIUMINT UNSIGNED',
222: restrictions => 'NOT NULL',
223: auto_inc => 'yes', },
224: { name => 'resource',
225: type => 'MEDIUMTEXT',
226: restrictions => 'NOT NULL'},
227: ],
228: 'PRIMARY KEY' => ['res_id'],
229: };
230:
1.4 matthew 231: #my $action_table_def =
232: #{ id => $action_table,
233: # permanent => 'no',
234: # columns => [{ name => 'action_id',
235: # type => 'MEDIUMINT UNSIGNED',
236: # restrictions => 'NOT NULL',
237: # auto_inc => 'yes', },
238: # { name => 'action',
239: # type => 'VARCHAR(100)',
240: # restrictions => 'NOT NULL'},
241: # ],
242: # 'PRIMARY KEY' => ['action_id',],
243: #};
1.1 matthew 244:
245: my $machine_table_def =
1.14 matthew 246: { id => $tables{'machine'},
1.1 matthew 247: permanent => 'no',
248: columns => [{ name => 'machine_id',
249: type => 'MEDIUMINT UNSIGNED',
250: restrictions => 'NOT NULL',
251: auto_inc => 'yes', },
252: { name => 'machine',
253: type => 'VARCHAR(100)',
254: restrictions => 'NOT NULL'},
255: ],
256: 'PRIMARY KEY' => ['machine_id',],
257: };
258:
259: my $activity_table_def =
1.14 matthew 260: { id => $tables{'activity'},
1.1 matthew 261: permanent => 'no',
262: columns => [
263: { name => 'res_id',
264: type => 'MEDIUMINT UNSIGNED',
265: restrictions => 'NOT NULL',},
266: { name => 'time',
267: type => 'DATETIME',
268: restrictions => 'NOT NULL',},
269: { name => 'student_id',
1.2 matthew 270: type => 'MEDIUMINT UNSIGNED',
1.1 matthew 271: restrictions => 'NOT NULL',},
1.4 matthew 272: { name => 'action',
273: type => 'VARCHAR(10)',
1.1 matthew 274: restrictions => 'NOT NULL',},
275: { name => 'idx', # This is here in case a student
276: type => 'MEDIUMINT UNSIGNED', # has multiple submissions during
277: restrictions => 'NOT NULL', # one second. It happens, trust
278: auto_inc => 'yes', }, # me.
279: { name => 'machine_id',
1.2 matthew 280: type => 'MEDIUMINT UNSIGNED',
1.1 matthew 281: restrictions => 'NOT NULL',},
282: { name => 'action_values',
283: type => 'MEDIUMTEXT', },
284: ],
1.4 matthew 285: 'PRIMARY KEY' => ['time','student_id','res_id','idx'],
286: 'KEY' => [{columns => ['student_id']},
287: {columns => ['time']},],
1.1 matthew 288: };
1.4 matthew 289:
1.8 matthew 290: my @Activity_Table = ($activity_table_def);
1.4 matthew 291: my @ID_Tables = ($student_table_def,$res_table_def,$machine_table_def);
1.14 matthew 292:
1.1 matthew 293: ##
1.13 matthew 294: ## End of table definitions
1.1 matthew 295: ##
1.14 matthew 296: $logthis->('tables = '.join(',',keys(%tables)));
1.1 matthew 297:
1.3 matthew 298: $logthis->('Connectiong to mysql');
1.8 matthew 299: &Apache::lonmysql::set_mysql_user_and_password('www',
1.1 matthew 300: $perlvar{'lonSqlAccess'});
301: if (!&Apache::lonmysql::verify_sql_connection()) {
302: warn "Unable to connect to MySQL database.";
1.2 matthew 303: $logthis->("Unable to connect to MySQL database.");
1.16 matthew 304: &clean_up_and_exit(3);
1.1 matthew 305: }
1.3 matthew 306: $logthis->('SQL connection is up');
307:
1.24 albertel 308: &update_process_name($course.'@'.$domain." loading existing data");
1.14 matthew 309: my $missing_table = &check_for_missing_tables(values(%tables));
310: if (-s $gz_sql_filename && ! -s $gz_xml_filename) {
1.8 matthew 311: my $backup_modification_time = (stat($gz_sql_filename))[9];
312: $logthis->($gz_sql_filename.' was last modified '.
313: localtime($backup_modification_time).
314: '('.$backup_modification_time.')');
1.14 matthew 315: if ($missing_table) {
316: # If the backup happened prior to the last table modification,
317: # we need to save the tables.
318: if (&latest_table_modification_time() > $backup_modification_time) {
319: # Save the current tables in case we need them another time.
320: $logthis->('Backing existing tables up');
321: &backup_tables_as_xml($gz_xml_filename.'.save_'.time,\%tables);
1.1 matthew 322: }
1.14 matthew 323: $time_this->();
324: &load_backup_sql_tables($gz_sql_filename);
325: &backup_tables_as_xml($gz_xml_filename,\%tables);
326: $time_this->('load backup tables');
1.1 matthew 327: }
1.14 matthew 328: } elsif (-s $gz_xml_filename) {
329: my $backup_modification_time = (stat($gz_xml_filename))[9];
330: $logthis->($gz_xml_filename.' was last modified '.
331: localtime($backup_modification_time).
332: '('.$backup_modification_time.')');
333: if ($missing_table) {
1.8 matthew 334: my $table_modification_time = $backup_modification_time;
335: # If the backup happened prior to the last table modification,
1.14 matthew 336: # we need to save the tables.
337: if (&latest_table_modification_time() > $backup_modification_time) {
1.8 matthew 338: # Save the current tables in case we need them another time.
1.14 matthew 339: $logthis->('Backing existing tables up');
340: &backup_tables_as_xml($gz_xml_filename.'.save_'.time,\%tables);
1.8 matthew 341: }
342: $time_this->();
1.14 matthew 343: # We have to make our own tables for the xml format
344: &drop_tables();
345: &create_tables();
346: &load_backup_xml_tables($gz_xml_filename,\%tables);
1.8 matthew 347: $time_this->('load backup tables');
1.14 matthew 348: }
1.1 matthew 349: }
350:
1.15 matthew 351: if (defined($xmlfile)) {
1.16 matthew 352: &clean_up_and_exit(0);
1.15 matthew 353: }
354:
1.3 matthew 355: ##
356: ## Ensure the tables we need exist
1.1 matthew 357: # create_tables does not complain if the tables already exist
1.3 matthew 358: $logthis->('creating tables');
1.1 matthew 359: if (! &create_tables()) {
360: warn "Unable to create tables";
1.2 matthew 361: $logthis->('Unable to create tables');
1.16 matthew 362: &clean_up_and_exit(4);
1.1 matthew 363: }
364:
1.3 matthew 365: ##
366: ## Read the ids used for various tables
1.2 matthew 367: $logthis->('reading id tables');
1.1 matthew 368: &read_id_tables();
1.2 matthew 369: $logthis->('finished reading id tables');
1.1 matthew 370:
371: ##
1.3 matthew 372: ## Set up the errors file
373: my $error_fh = IO::File->new(">>$error_filename");
374:
375: ##
376: ## Parse the course log
377: $logthis->('processing course log');
1.24 albertel 378: &update_process_name($course.'@'.$domain." processing new data");
1.3 matthew 379: if (-s $newfilename) {
1.14 matthew 380: my $result = &process_courselog($newfilename,$error_fh,\%tables);
1.24 albertel 381: &update_process_name($course.'@'.$domain." backing up new data");
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)
1.25 raeburn 484: # or
485: # %3aPUTSTORE%3a(name)%3d(value)%26(name)%3d(value)
1.26 ! raeburn 486: # or
! 487: # %3aEXPORT%3a(name)%3d(value)%26(name)%3d(value)
1.1 matthew 488: #
489: # get delimiter between timestamped entries to be &&&
490: $log=~s/\%26(\d{9,10})\%3a/\&\&\&$1\%3a/g;
491: $log = &unescape($log);
492: # now go over all log entries
1.2 matthew 493: if (! defined($host)) { $host = 'unknown'; }
494: my $prevchunk = 'none';
495: foreach my $chunk (split(/\&\&\&/,$log)) {
1.19 matthew 496: if (length($chunk) > 20000) {
497: # avoid putting too much data into the database
498: # (usually an uploaded file or something similar)
1.20 matthew 499: if (! &savechunk(\$chunk,$timestamp,$host)) {
1.19 matthew 500: close(IN);
501: return undef;
502: }
503: next;
504: }
1.2 matthew 505: my $warningflag = '';
506: my ($time,$res,$uname,$udom,$action,@values)= split(/:/,$chunk);
1.18 matthew 507: #
1.1 matthew 508: if (! defined($res) || $res =~ /^\s*$/) {
509: $res = '/adm/roles';
1.2 matthew 510: $action = 'LOGIN';
1.1 matthew 511: }
512: if ($res =~ m|^/prtspool/|) {
513: $res = '/prtspool/';
514: }
515: if (! defined($action) || $action eq '') {
1.2 matthew 516: $action = 'VIEW';
1.1 matthew 517: }
1.26 ! raeburn 518: if ($action !~ /^(LOGIN|VIEW|POST|CSTORE|STORE|PUTSTORE|EXPORT)$/) {
1.2 matthew 519: $warningflag .= 'action';
1.3 matthew 520: print $error_fh 'full log entry:'.$log.$/;
1.20 matthew 521: print $error_fh 'error on chunk (saving)'.$/;
522: if (! &savechunk(\$chunk,$timestamp,$host)) {
523: close(IN);
524: return undef;
525: }
526: $logthis->('(action) Unable to parse chunk'.$/.
1.3 matthew 527: 'got '.
528: 'time = '.$time.$/.
529: 'res = '.$res.$/.
530: 'uname= '.$uname.$/.
531: 'udom = '.$udom.$/.
532: 'action='.$action.$/.
1.11 matthew 533: '@values = '.join('&',@values));
1.3 matthew 534: next; #skip it if we cannot understand what is happening.
1.2 matthew 535: }
536: #
1.14 matthew 537: my %data = (student => $uname.':'.$udom,
538: resource => $res,
539: machine => $host,
540: action => $action,
541: time => &Apache::lonmysql::sqltime($time));
1.11 matthew 542: if ($action eq 'POST') {
1.14 matthew 543: $data{'action_values'} =
1.11 matthew 544: $dbh->quote(join('&',map { &escape($_); } @values));
545: } else {
1.14 matthew 546: $data{'action_values'} = $dbh->quote(join('&',@values));
547: }
548: my $error = &store_entry($dbh,$tables,\%data);
549: if ($error) {
550: $logthis->('error store_entry:'.$error." on %data");
1.11 matthew 551: }
1.2 matthew 552: $prevchunk = $chunk;
1.1 matthew 553: }
554: }
1.14 matthew 555: my $result = &store_entry($dbh,$tables);
556: if (! defined($result)) {
557: my $error = &Apache::lonmysql::get_error();
558: warn "Error occured during insert.".$error;
559: $logthis->('error = '.$error);
1.1 matthew 560: }
561: close IN;
562: return $linecount;
1.20 matthew 563: ##
564: ##
565: sub savechunk {
566: my ($chunkref,$timestamp,$host) = @_;
567: my $chunk = &escape(${$chunkref});
568: if (! open(CHUNKFILE,">>$chunk_filename") ||
569: ! print CHUNKFILE $timestamp.':'.$host.':'.$chunk.$/) {
570: # abort
571: close(CHUNKFILE);
572: return 0;
573: }
574: close(CHUNKFILE);
575: return 1;
576: }
1.1 matthew 577: }
578:
1.2 matthew 579:
580: ##
1.14 matthew 581: ## default value for $logthis and $time_this
1.2 matthew 582: sub nothing {
583: return;
584: }
585:
586: ##
1.14 matthew 587: ## Logging routine (look for $log)
1.2 matthew 588: ##
589: sub log_to_file {
590: my ($input)=@_;
591: print LOGFILE $input.$/;
592: }
593:
1.1 matthew 594: ##
595: ## Timing routines
596: ##
597: {
598: my %Timing;
599: my $starttime;
600:
601: sub time_action {
602: my ($key) = @_;
603: if (defined($key)) {
604: $Timing{$key}+=Time::HiRes::time-$starttime;
605: $Timing{'count_'.$key}++;
606: }
607: $starttime = Time::HiRes::time;
608: }
609:
610: sub outputtimes {
611: my $Str;
612: if ($time_run) {
613: $Str = "Timing Data:".$/;
614: while (my($k,$v) = each(%Timing)) {
615: next if ($k =~ /^count_/);
616: my $count = $Timing{'count_'.$k};
617: $Str .=
618: ' '.sprintf("%25.25s",$k).
619: ' '.sprintf('% 8d',$count).
620: ' '.sprintf('%12.5f',$v).$/;
621: }
622: }
623: return $Str;
624: }
625:
626: }
627:
1.14 matthew 628: sub latest_table_modification_time {
629: my $latest_time;
630: foreach my $table (@Activity_Table,@ID_Tables) {
631: my %tabledata = &Apache::lonmysql::table_information($table->{'id'});
632: next if (! scalar(keys(%tabledata))); # table does not exist
633: if (! defined($latest_time) ||
634: $latest_time < $tabledata{'Update_time'}) {
635: $latest_time = $tabledata{'Update_time'};
636: }
637: }
638: return $latest_time;
639: }
640:
641: sub check_for_missing_tables {
642: my @wanted_tables = @_;
643: # Check for missing tables
644: my @Current_Tables = &Apache::lonmysql::tables_in_db();
645: my %Found;
646: foreach my $tablename (@Current_Tables) {
647: foreach my $table (@wanted_tables) {
648: if ($tablename eq $table) {
649: $Found{$tablename}++;
650: }
651: }
652: }
653: $logthis->('Found tables '.join(',',keys(%Found)));
654: my $missing_a_table = 0;
655: foreach my $table (@wanted_tables) {
656: if (! $Found{$table}) {
657: $logthis->('Missing table '.$table);
658: $missing_a_table = 1;
659: last;
660: }
661: }
662: return $missing_a_table;
663: }
1.1 matthew 664:
665: ##
666: ## Use mysqldump to store backups of the tables
667: ##
1.14 matthew 668: sub backup_tables_as_sql {
1.6 matthew 669: my ($gz_sql_filename) = @_;
1.12 matthew 670: my $command = qq{mysqldump --quote-names --opt loncapa };
1.3 matthew 671: foreach my $table (@ID_Tables,@Activity_Table) {
1.1 matthew 672: my $tablename = $table->{'id'};
1.12 matthew 673: $tablename =~ s/\`//g;
1.1 matthew 674: $command .= $tablename.' ';
675: }
1.6 matthew 676: $command .= '| gzip >'.$gz_sql_filename;
1.2 matthew 677: $logthis->($command);
1.1 matthew 678: system($command);
679: }
680:
681: ##
682: ## Load in mysqldumped files
683: ##
1.14 matthew 684: sub load_backup_sql_tables {
1.6 matthew 685: my ($gz_sql_filename) = @_;
686: if (-s $gz_sql_filename) {
1.8 matthew 687: $logthis->('loading data from gzipped sql file');
688: my $command='gzip -dc '.$gz_sql_filename.' | mysql --database=loncapa';
1.6 matthew 689: system($command);
690: $logthis->('finished loading gzipped data');;
691: } else {
692: return undef;
693: }
1.1 matthew 694: }
695:
696: ##
697: ##
698: ##
699: sub update_process_name {
700: my ($text) = @_;
701: $0 = 'parse_activity_log.pl: '.$text;
702: }
703:
704: sub get_filename {
705: my ($course,$domain) = @_;
706: my ($a,$b,$c,undef) = split('',$course,4);
707: return "$perlvar{'lonUsersDir'}/$domain/$a/$b/$c/$course/activity.log";
708: }
709:
710: sub create_tables {
1.3 matthew 711: foreach my $table (@ID_Tables,@Activity_Table) {
1.1 matthew 712: my $table_id = &Apache::lonmysql::create_table($table);
713: if (! defined($table_id)) {
714: warn "Unable to create table ".$table->{'id'}.$/;
1.12 matthew 715: $logthis->('Unable to create table '.$table->{'id'});
716: $logthis->(join($/,&Apache::lonmysql::build_table_creation_request($table)));
1.1 matthew 717: return 0;
718: }
719: }
720: return 1;
721: }
722:
723: sub drop_tables {
1.3 matthew 724: foreach my $table (@ID_Tables,@Activity_Table) {
1.1 matthew 725: my $table_id = $table->{'id'};
726: &Apache::lonmysql::drop_table($table_id);
727: }
728: }
729:
730: #################################################################
731: #################################################################
732: ##
733: ## Database item id code
734: ##
735: #################################################################
736: #################################################################
737: { # Scoping for ID lookup code
738: my %IDs;
739:
740: sub read_id_tables {
1.3 matthew 741: foreach my $table (@ID_Tables) {
1.1 matthew 742: my @Data = &Apache::lonmysql::get_rows($table->{'id'});
1.3 matthew 743: my $count = 0;
1.1 matthew 744: foreach my $row (@Data) {
745: $IDs{$table->{'id'}}->{$row->[1]} = $row->[0];
746: }
747: }
1.3 matthew 748: return;
1.1 matthew 749: }
750:
751: sub get_id {
752: my ($table,$fieldname,$value) = @_;
1.16 matthew 753: if (exists($IDs{$table}->{$value}) && $IDs{$table}->{$value} =~ /^\d+$/) {
1.1 matthew 754: return $IDs{$table}->{$value};
755: } else {
756: # insert into the table - if the item already exists, that is
757: # okay.
758: my $result = &Apache::lonmysql::store_row($table,[undef,$value]);
759: if (! defined($result)) {
1.16 matthew 760: warn("Got error on id insert for $value\n".
761: &Apache::lonmysql::get_error());
1.1 matthew 762: }
763: # get the id
1.16 matthew 764: my $id = &Apache::lonmysql::get_dbh()->{'mysql_insertid'};
765: if (defined($id)) {
766: $IDs{$table}->{$value}=$id;
1.1 matthew 767: } else {
1.2 matthew 768: $logthis->("Unable to retrieve id for $table $fieldname $value");
1.1 matthew 769: return undef;
770: }
771: }
772: }
773:
774: } # End of ID scoping
775:
1.14 matthew 776: ###############################################################
777: ###############################################################
778: ##
779: ## Save as XML
780: ##
781: ###############################################################
782: ###############################################################
783: sub backup_tables_as_xml {
784: my ($filename,$tables) = @_;
785: open(XMLFILE,"|gzip - > $filename") || return ('error:unable to write '.$filename);
786: my $query = qq{
787: SELECT B.resource,
788: A.time,
789: A.idx,
790: C.student,
791: A.action,
792: E.machine,
793: A.action_values
794: FROM $tables->{'activity'} AS A
795: LEFT JOIN $tables->{'res'} AS B ON B.res_id=A.res_id
796: LEFT JOIN $tables->{'student'} AS C ON C.student_id=A.student_id
797: LEFT JOIN $tables->{'machine'} AS E ON E.machine_id=A.machine_id
798: ORDER BY A.time DESC
799: };
800: $query =~ s/\s+/ /g;
801: my $dbh = &Apache::lonmysql::get_dbh();
802: my $sth = $dbh->prepare($query);
803: if (! $sth->execute()) {
804: $logthis->('<font color="blue">'.
805: 'WARNING: Could not retrieve from database:'.
806: $sth->errstr().'</font>');
807: return undef;
808: } else {
809: my ($res,$sqltime,$idx,$student,$action,$machine,$action_values);
810: if ($sth->bind_columns(\$res,\$sqltime,\$idx,\$student,\$action,
811: \$machine,\$action_values)) {
812:
813: while ($sth->fetch) {
814: print XMLFILE '<row>'.
815: qq{<resource>$res</resource>}.
816: qq{<time>$sqltime</time>}.
817: qq{<idx>$idx</idx>}.
818: qq{<student>$student</student>}.
819: qq{<action>$action</action>}.
820: qq{<machine>$machine</machine>}.
821: qq{<action_values>$action_values</action_values>}.
822: '</row>'.$/;
823: }
824: } else {
825: warn "Unable to bind to columns.\n";
826: return undef;
827: }
828: }
829: close XMLFILE;
830: return;
831: }
832:
833: ###############################################################
834: ###############################################################
835: ##
836: ## load as xml
837: ##
838: ###############################################################
839: ###############################################################
1.15 matthew 840: {
841: my @fields = ('resource','time',
842: 'student','action','idx','machine','action_values');
843: my %ids = ();
1.14 matthew 844: sub load_backup_xml_tables {
845: my ($filename,$tables) = @_;
1.16 matthew 846: my $dbh = &Apache::lonmysql::get_dbh();
1.14 matthew 847: my $xmlfh;
848: open($xmlfh,"cat $filename | gzip -d - |");
849: if (! defined($xmlfh)) {
850: return ('error:unable to read '.$filename);
851: }
1.15 matthew 852: #
853: %ids = (resource=> {"\0count"=>1},
854: student=> {"\0count"=>1},
855: machine=> {"\0count"=>1});
856: #
1.14 matthew 857: my %data;
1.15 matthew 858: while (my $inputline = <$xmlfh>) {
859: my ($resource,$time,undef,$student,$action,$machine,$action_values) =
860: ($inputline =~ m{<row>
861: <resource>(.*)</resource>
862: <time>(.*)</time>
863: <idx>(.*)</idx>
864: <student>(.*)</student>
865: <action>(.*)</action>
866: <machine>(.*)</machine>
867: <action_values>(.*)</action_values>
868: </row>$
869: }x
870: );
871: my $resource_id = &xml_get_id('resource',$resource);
872: my $student_id = &xml_get_id('student',$student);
873: my $machine_id = &xml_get_id('machine',$machine);
1.16 matthew 874: &xml_store_activity_row(map { defined($_)?$dbh->quote($_):''
1.15 matthew 875: } ($resource_id,
876: $time,
877: $student_id,
878: $action,
879: 'NULL',
880: $machine_id,
881: $action_values));
882: }
883: &xml_store_activity_row();
884: close($xmlfh);
885: # Store id tables
886: while (my ($id_name,$id_data) = each(%ids)) {
887: if ($id_name eq 'resource') { $id_name = 'res'; }
888: delete($id_data->{"\0count"});
889: &xml_store_id_table($id_name,$id_data);
890: }
891: return;
892: }
893:
894: sub xml_get_id {
895: my ($table,$element) = @_;
896: if (! exists($ids{$table}->{$element})) {
897: $ids{$table}->{$element} = $ids{$table}->{"\0count"}++;
898: }
899: return $ids{$table}->{$element};
900: }
901:
902: {
903: my @data_rows;
904: sub xml_store_activity_row {
905: my @data = @_;
906: if (scalar(@data)) {
907: push(@data_rows,[@data]);
908: }
909: if (! scalar(@data) || scalar(@data_rows) > 500) {
910: if (! &Apache::lonmysql::bulk_store_rows($tables{'activity'},
911: scalar(@{$data_rows[0]}),
912: \@data_rows)) {
913: $logthis->("Error:".&Apache::lonmysql::get_error());
914: warn("Error:".&Apache::lonmysql::get_error());
915: } else {
916: undef(@data_rows);
1.14 matthew 917: }
918: }
919: return;
920: }
921:
1.15 matthew 922: }
923:
924: sub xml_store_id_table {
925: my ($table,$tabledata) =@_;
1.16 matthew 926: my $dbh = &Apache::lonmysql::get_dbh();
1.15 matthew 927: if (! &Apache::lonmysql::bulk_store_rows
928: ($tables{$table},2,
1.16 matthew 929: [map{[$tabledata->{$_},$dbh->quote($_)]} keys(%$tabledata)])) {
1.15 matthew 930: $logthis->("Error:".&Apache::lonmysql::get_error());
931: warn "Error:".&Apache::lonmysql::get_error().$/;
932: }
933: }
934:
935: } # End of load xml scoping
1.14 matthew 936:
937: #######################################################################
938: #######################################################################
939: ##
940: ## store_entry - accumulate data to be inserted into the database
1.15 matthew 941: ##
1.14 matthew 942: ## Pass no values in to clear accumulator
943: ## Pass ($dbh,\%tables) to initiate storage of values
944: ## Pass ($dbh,\%tables,\%data) to use normally
945: ##
946: #######################################################################
947: #######################################################################
948: {
949: my @rows;
950:
951: sub store_entry {
1.23 albertel 952: my $max_row_count = 100;
1.14 matthew 953: if (! @_) {
954: undef(@rows);
955: return '';
956: }
957: my ($dbh,$tables,$data) = @_;
958: return if (! defined($tables));
959: if (defined($data)) {
960: my $error;
961: foreach my $field ('student','resource','action','time') {
962: if (! defined($data->{$field}) || $data->{$field} eq ':' ||
963: $data->{$field}=~ /^\s*$/) {
964: $error.=$field.',';
965: }
966: }
967: if ($error) { $error=~s/,$//; return $error; }
968: #
969: my $student_id = &get_id($tables->{'student'},'student',
970: $data->{'student'});
971: my $res_id = &get_id($tables->{'res'},
972: 'resource',$data->{'resource'});
973: my $machine_id = &get_id($tables->{'machine'},
974: 'machine',$data->{'machine'});
975: my $idx = $data->{'idx'}; if (! $idx) { $idx = "''"; }
976: #
977: push(@rows,[$res_id,
978: qq{'$data->{'time'}'},
979: $student_id,
980: qq{'$data->{'action'}'},
981: $idx,
982: $machine_id,
983: $data->{'action_values'}]);
984: }
1.15 matthew 985: if (defined($tables) &&
1.14 matthew 986: ( (! defined($data) && scalar(@rows)) || scalar(@rows)>$max_row_count)
987: ){
988: # Store the rows
1.15 matthew 989: my $result =
1.14 matthew 990: &Apache::lonmysql::bulk_store_rows($tables->{'activity'},
991: undef,
992: \@rows);
993: if (! defined($result)) {
994: my $error = &Apache::lonmysql::get_error();
995: warn "Error occured during insert.".$error;
996: return $error;
997: }
998: undef(@rows);
999: return $result if (! defined($data));
1000: }
1001: return '';
1002: }
1003:
1004: } # end of scope for &store_entry
1.1 matthew 1005:
1006: ###############################################################
1007: ###############################################################
1008: ##
1009: ## The usual suspects
1010: ##
1011: ###############################################################
1012: ###############################################################
1013: sub escape {
1014: my $str=shift;
1015: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
1016: return $str;
1017: }
1018:
1019: sub unescape {
1020: my $str=shift;
1021: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1022: return $str;
1023: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>