MDL-30643 - Added definition for new temporary table to install.xml.

- Added code to generate tempoary tables from existing standard tables where possible.
This commit is contained in:
Tyler Bannister 2012-08-03 15:51:49 -04:00
parent 0934f5c304
commit 4614b02905
2 changed files with 81 additions and 66 deletions

View file

@ -2882,7 +2882,7 @@
<KEY NAME="fk_raterid" TYPE="foreign" FIELDS="raterid" REFTABLE="user" REFFIELDS="id" PREVIOUS="fk_definitionid"/> <KEY NAME="fk_raterid" TYPE="foreign" FIELDS="raterid" REFTABLE="user" REFFIELDS="id" PREVIOUS="fk_definitionid"/>
</KEYS> </KEYS>
</TABLE> </TABLE>
<TABLE NAME="event_subscriptions" COMMENT="Tracks subscriptions to remote calendars." PREVIOUS="grading_instances"> <TABLE NAME="event_subscriptions" COMMENT="Tracks subscriptions to remote calendars." PREVIOUS="grading_instances" NEXT="temp_log_template">
<FIELDS> <FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="url"/> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="url"/>
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="id" NEXT="courseid"/> <FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="id" NEXT="courseid"/>
@ -2897,5 +2897,22 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/> <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS> </KEYS>
</TABLE> </TABLE>
<TABLE NAME="temp_log_template" COMMENT="Temporary storage for daily logs" PREVIOUS="event_subscriptions">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="course"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="action"/>
<FIELD NAME="action" TYPE="char" LENGTH="40" NOTNULL="true" SEQUENCE="false" PREVIOUS="course"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="action" UNIQUE="false" FIELDS="action" NEXT="course"/>
<INDEX NAME="course" UNIQUE="false" FIELDS="course" PREVIOUS="action" NEXT="user"/>
<INDEX NAME="user" UNIQUE="false" FIELDS="userid" PREVIOUS="course" NEXT="usercourseaction"/>
<INDEX NAME="usercourseaction" UNIQUE="false" FIELDS="userid, course, action" PREVIOUS="user"/>
</INDEXES>
</TABLE>
</TABLES> </TABLES>
</XMLDB> </XMLDB>

View file

@ -111,10 +111,10 @@ function stats_run_query($sql, $parameters) {
} catch (dml_exception $e) { } catch (dml_exception $e) {
if (debugging('', DEBUG_ALL)) { if (debugging('', DEBUG_ALL)) {
mtrace($e->getMessage()); mtrace($e->getMessage());
} }
return false; return false;
} }
return true; return true;
} }
@ -217,7 +217,7 @@ function stats_cron_daily($maxdays=1) {
break; break;
} }
stats_progress('in'); stats_progress('0');
// Find out if any logs available for this day // Find out if any logs available for this day
$sql = "SELECT 'x' FROM {temp_log1} l"; $sql = "SELECT 'x' FROM {temp_log1} l";
@ -289,6 +289,7 @@ function stats_cron_daily($maxdays=1) {
} }
stats_progress('3'); stats_progress('3');
// Set stat2 to the number distinct users with role assignments in the course that were active
// using table alias in UPDATE does not work in pg < 8.2 // using table alias in UPDATE does not work in pg < 8.2
$sql = "UPDATE {temp_stats_daily} $sql = "UPDATE {temp_stats_daily}
SET stat2 = ( SET stat2 = (
@ -316,7 +317,7 @@ function stats_cron_daily($maxdays=1) {
SELECT DISTINCT course FROM {temp_log2} SELECT DISTINCT course FROM {temp_log2}
)"; )";
if (!stats_run_query($sql, array('courselevel'=>CONTEXT_COURSE))) { if ($logspresent && !stats_run_query($sql, array('courselevel'=>CONTEXT_COURSE))) {
$failed = true; $failed = true;
break; break;
} }
@ -338,6 +339,7 @@ function stats_cron_daily($maxdays=1) {
} }
stats_progress('5'); stats_progress('5');
// Set stat 2 to the number of enrolled users who were active in the course
$sql = "UPDATE {temp_stats_daily} $sql = "UPDATE {temp_stats_daily}
SET stat2 = ( SET stat2 = (
@ -400,7 +402,7 @@ function stats_cron_daily($maxdays=1) {
$sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments', $nextmidnight, ".SITEID.", $defaultfproleid, SELECT 'enrolments', $nextmidnight, ".SITEID.", $defaultfproleid,
$totalactiveusers AS stat1, $dailyactiveusers AS stat2" . $totalactiveusers AS stat1, $dailyactiveusers AS stat2" .
$DB->sql_null_from_clause();; $DB->sql_null_from_clause();;
@ -416,7 +418,7 @@ function stats_cron_daily($maxdays=1) {
} }
/// individual user stats (including not-logged-in) in each course, this is slow - reuse this data if possible /// individual user stats (including not-logged-in) in each course, this is slow - reuse this data if possible
list($viewactionssql, $params1) = $DB->get_in_or_equal($viewactions, SQL_PARAMS_NAMED, 'view'); list($viewactionssql, $params1) = $DB->get_in_or_equal($viewactions, SQL_PARAMS_NAMED, 'view');
list($postactionssql, $params2) = $DB->get_in_or_equal($postactions, SQL_PARAMS_NAMED, 'post'); list($postactionssql, $params2) = $DB->get_in_or_equal($postactions, SQL_PARAMS_NAMED, 'post');
$sql = "INSERT INTO {temp_stats_user_daily} (stattype, timeend, courseid, userid, statsreads, statswrites) $sql = "INSERT INTO {temp_stats_user_daily} (stattype, timeend, courseid, userid, statsreads, statswrites)
@ -435,7 +437,7 @@ function stats_cron_daily($maxdays=1) {
stats_progress('10'); stats_progress('10');
/// how many view/post actions in each course total /// How many view/post actions in each course total
$sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity' AS stattype, $nextmidnight AS timeend, c.id AS courseid, 0, SELECT 'activity' AS stattype, $nextmidnight AS timeend, c.id AS courseid, 0,
@ -452,7 +454,7 @@ function stats_cron_daily($maxdays=1) {
stats_progress('11'); stats_progress('11');
/// how many view actions for each course+role - excluding guests and frontpage /// how many view actions for each course+role - excluding guests and frontpage
$sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
@ -485,8 +487,8 @@ function stats_cron_daily($maxdays=1) {
} }
stats_progress('12'); stats_progress('12');
/// how many view actions from guests only in each course - excluding frontpage /// how many view actions from guests only in each course - excluding frontpage
/// normal users may enter course with temporary guest access too /// normal users may enter course with temporary guest access too
$sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
@ -518,7 +520,7 @@ function stats_cron_daily($maxdays=1) {
stats_progress('13'); stats_progress('13');
/// how many view actions for each role on frontpage - excluding guests, not-logged-in and default frontpage role /// How many view actions for each role on frontpage - excluding guests, not-logged-in and default frontpage role
$sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight AS timeend, courseid, roleid, SELECT 'activity', $nextmidnight AS timeend, courseid, roleid,
@ -551,7 +553,7 @@ function stats_cron_daily($maxdays=1) {
stats_progress('14'); stats_progress('14');
/// how many view actions for default frontpage role on frontpage only // How many view actions for default frontpage role on frontpage only
$sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', timeend, courseid, $defaultfproleid AS roleid, SELECT 'activity', timeend, courseid, $defaultfproleid AS roleid,
@ -582,7 +584,7 @@ function stats_cron_daily($maxdays=1) {
} }
stats_progress('15'); stats_progress('15');
/// how many view actions for guests or not-logged-in on frontpage // How many view actions for guests or not-logged-in on frontpage
$sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight AS timeend, ".SITEID." AS courseid, $guestrole AS roleid, SELECT 'activity', $nextmidnight AS timeend, ".SITEID." AS courseid, $guestrole AS roleid,
@ -626,11 +628,11 @@ function stats_cron_daily($maxdays=1) {
if ($failed) { if ($failed) {
$days--; $days--;
mtrace("...error occurred, completed $days days of statistics."); mtrace("...error occurred, completed $days days of statistics in {$total} s.");
return false; return false;
} else if ($timeout) { } else if ($timeout) {
mtrace("...stopping early, reached maximum number of $maxdays days - will continue next time."); mtrace("...stopping early, reached maximum number of $maxdays days ({$total} s) - will continue next time.");
return false; return false;
} else { } else {
@ -768,7 +770,7 @@ function stats_cron_weekly() {
set_config('statslastweekly', $nextstartweek); set_config('statslastweekly', $nextstartweek);
$elapsed = time()-$weekstart; $elapsed = time()-$weekstart;
mtrace(" finished until $nextstartweek: ".userdate($nextstartweek) ." ( in $elapsed s)"); mtrace(" finished until $nextstartweek: ".userdate($nextstartweek) ." (in $elapsed s)");
$timestart = $nextstartweek; $timestart = $nextstartweek;
$nextstartweek = stats_get_next_week_start($nextstartweek); $nextstartweek = stats_get_next_week_start($nextstartweek);
@ -1548,60 +1550,56 @@ function stats_temp_table_create() {
stats_temp_table_drop(); stats_temp_table_drop();
$log = new xmldb_table('temp_log1'); $xmlfile = $CFG->dirroot . '/lib/db/install.xml';
$tempfile = $CFG->dirroot . '/lib/db/temp_stats_log_template.xml';
$tables = array();
$log->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); // Allows for the additional xml files to be used (if necessary)
$log->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); $files = array(
$log->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); $xmlfile => array(
$log->add_field('action', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null); 'stats_daily' => array('temp_stats_daily'),
'stats_user_daily' => array('temp_stats_user_daily'),
'temp_log_template' => array('temp_log1', 'temp_log2'),
),
);
$log->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); foreach ($files as $file => $contents) {
$log->add_index('temp_tl_course_ix', XMLDB_INDEX_NOTUNIQUE, array('course'));
$log->add_index('temp_tl_act_ix', XMLDB_INDEX_NOTUNIQUE, array('action'));
$log->add_index('temp_tl_user_ix', XMLDB_INDEX_NOTUNIQUE, array('userid'));
$log->add_index('temp_tl_usecouact_ix', XMLDB_INDEX_NOTUNIQUE, array('userid','course','action'));
$user = new xmldb_table('temp_stats_daily'); $xmldb_file = new xmldb_file($file);
$user->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0); if (!$xmldb_file->fileExists()) {
$user->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0); throw new ddl_exception('ddlxmlfileerror', null, 'File does not exist');
$user->add_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity'); }
$user->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0); $loaded = $xmldb_file->loadXMLStructure();
$user->add_field('stat1', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0); if (!$loaded || !$xmldb_file->isLoaded()) {
$user->add_field('stat2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0); throw new ddl_exception('ddlxmlfileerror', null, 'not loaded??');
}
$user->add_index('temp_tsd_courseid_ix', XMLDB_INDEX_NOTUNIQUE, array('courseid')); $xmldb_structure = $xmldb_file->getStructure();
$user->add_index('temp_tsd_roleid_ix', XMLDB_INDEX_NOTUNIQUE, array('roleid'));
$user->add_index('temp_tsd_statype_ix', XMLDB_INDEX_NOTUNIQUE, array('stattype'));
$user->add_index('temp_tsd_timeend_ix', XMLDB_INDEX_NOTUNIQUE, array('timeend'));
$daily = new xmldb_table('temp_stats_user_daily'); foreach ($contents as $template => $names) {
$daily->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0); $table = $xmldb_structure->getTable($template);
$daily->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
$daily->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
$daily->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
$daily->add_field('statsreads', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
$daily->add_field('statswrites', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
$daily->add_field('stattype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
$daily->add_index('temp_tsud_courseid_ix', XMLDB_INDEX_NOTUNIQUE, array('courseid')); if (is_null($table)) {
$daily->add_index('temp_tsud_userid_ix', XMLDB_INDEX_NOTUNIQUE, array('userid')); throw new ddl_exception('ddlunknowntable', null, 'The table '. $name .' is not defined in the file '. $xmlfile);
$daily->add_index('temp_tsud_roleid_ix', XMLDB_INDEX_NOTUNIQUE, array('roleid')); }
$daily->add_index('temp_tsud_stattype_ix', XMLDB_INDEX_NOTUNIQUE, array('stattype')); $table->setNext(null);
$daily->add_index('temp_tsud_timeend_ix', XMLDB_INDEX_NOTUNIQUE, array('timeend')); $table->setPrevious(null);
foreach ($names as $name) {
$named = clone $table;
$named->setName($name);
$tables[$name] = $named;
}
}
}
try { try {
$dbman->create_temp_table($log);
$log->name = 'temp_log2'; foreach ($tables as $table) {
$dbman->create_temp_table($table);
$dbman->create_temp_table($log); }
$dbman->create_temp_table($user);
$dbman->create_temp_table($daily);
} catch (Exception $e) { } catch (Exception $e) {
mtrace("Temporary table creation failed!"); mtrace('Temporary table creation failed: '. $e->getMessage());
return false; return false;
} }
@ -1624,7 +1622,7 @@ function stats_temp_table_drop() {
$table = new xmldb_table($name); $table = new xmldb_table($name);
try { try {
$dbman->drop_temp_table($table); $dbman->drop_table($table);
} catch (Exception $e) { } catch (Exception $e) {
mtrace("Error occured while dropping temporary tables!"); mtrace("Error occured while dropping temporary tables!");
} }
@ -1642,10 +1640,10 @@ function stats_temp_table_drop() {
function stats_temp_table_fill($timestart, $timeend) { function stats_temp_table_fill($timestart, $timeend) {
global $DB; global $DB;
$sql = 'INSERT INTO {temp_log1} (userid, course, action) $sql = "INSERT INTO {temp_log1} (userid, course, action)
SELECT userid, course, action FROM {log} l SELECT userid, course, action FROM {log}
WHERE l.time >= ? AND l.time < ?'; WHERE time >= ? AND time < ?";
$DB->execute($sql, array($timestart, $timeend)); $DB->execute($sql, array($timestart, $timeend));