Revert "MDL-30643 - Fixed speed issues with stats generation."

This reverts commit 5d911452cd9ef60f8f79ed9bd39fc8302211a96c.
This commit is contained in:
Eloy Lafuente (stronk7) 2012-09-05 19:04:01 +02:00
parent cbcf50a2f4
commit 44a2eb911b

View File

@ -80,12 +80,12 @@ function stats_daily_progress($ident) {
static $init = 0;
if ($ident == 'init') {
$init = $start = microtime(true);
$init = $start = time();
return;
}
$elapsed = round(microtime(true) - $start);
$start = microtime(true);
$elapsed = time() - $start;
$start = time();
if (debugging('', DEBUG_ALL)) {
mtrace("$ident:$elapsed ", '');
@ -176,27 +176,29 @@ function stats_cron_daily($maxdays=1) {
$daystart = time();
$timesql = "l.time >= $timestart AND l.time < $nextmidnight";
$timesql1 = "l1.time >= $timestart AND l1.time < $nextmidnight";
$timesql2 = "l2.time >= $timestart AND l2.time < $nextmidnight";
stats_daily_progress('init');
if ( !stats_create_logs_view($timestart, $nextmidnight) ) {
$failed = true;
break;
}
stats_daily_progress('gen');
/// find out if any logs available for this day
$sql = "SELECT 'x' FROM {tmp_stats_log1} l";
$sql = "SELECT 'x'
FROM {log} l
WHERE $timesql";
$logspresent = $DB->get_records_sql($sql, null, 0, 1);
/// process login info first
$sql = "INSERT INTO {stats_user_daily} (stattype, timeend, courseid, userid, statsreads)
SELECT 'logins', $nextmidnight AS timeend, ".SITEID." AS courseid, userid, count(id) as statsreads
FROM {tmp_stats_log1} l
WHERE action = 'login'
GROUP BY timeend, courseid, userid
SELECT 'logins', timeend, courseid, userid, count(statsreads)
FROM (
SELECT $nextmidnight AS timeend, ".SITEID." AS courseid, l.userid, l.id AS statsreads
FROM {log} l
WHERE action = 'login' AND $timesql
) inline_view
GROUP BY timeend, courseid, userid
HAVING count(statsreads) > 0";
if ($logspresent and !$DB->execute($sql)) {
@ -208,9 +210,13 @@ function stats_cron_daily($maxdays=1) {
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'logins' AS stattype, $nextmidnight AS timeend, ".SITEID." as courseid, 0,
COALESCE(SUM(statsreads), 0) as stat1, COUNT('x') as stat2
FROM {stats_user_daily}
WHERE stattype = 'logins' AND timeend = $nextmidnight";
COALESCE((SELECT SUM(statsreads)
FROM {stats_user_daily} s1
WHERE s1.stattype = 'logins' AND timeend = $nextmidnight), 0) AS stat1,
(SELECT COUNT('x')
FROM {stats_user_daily} s2
WHERE s2.stattype = 'logins' AND timeend = $nextmidnight) AS stat2" .
$DB->sql_null_from_clause();
if ($logspresent and !$DB->execute($sql)) {
$failed = true;
@ -231,13 +237,15 @@ function stats_cron_daily($maxdays=1) {
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments' as stattype, $nextmidnight as timeend, e.courseid, ra.roleid,
COUNT(DISTINCT ue.userid) as stat1, 0 as stat2
FROM {role_assignments} ra
JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :courselevel)
JOIN {enrol} e ON e.courseid = c.instanceid
JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid)
GROUP BY courseid, roleid";
SELECT 'enrolments', timeend, courseid, roleid, COUNT(DISTINCT userid), 0
FROM (
SELECT $nextmidnight AS timeend, e.courseid, ra.roleid, ue.userid
FROM {role_assignments} ra
JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :courselevel)
JOIN {enrol} e ON e.courseid = c.instanceid
JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid)
) inline_view
GROUP BY timeend, courseid, roleid";
if (!$DB->execute($sql, array('courselevel'=>CONTEXT_COURSE))) {
$failed = true;
@ -255,14 +263,15 @@ function stats_cron_daily($maxdays=1) {
WHERE ra.roleid = {stats_daily}.roleid AND
e.courseid = {stats_daily}.courseid AND
EXISTS (SELECT 'x'
FROM {tmp_stats_log1} l
FROM {log} l
WHERE l.course = {stats_daily}.courseid AND
l.userid = ra.userid))
l.userid = ra.userid AND $timesql))
WHERE {stats_daily}.stattype = 'enrolments' AND
{stats_daily}.timeend = $nextmidnight AND
{stats_daily}.courseid IN
(SELECT DISTINCT course
FROM {tmp_stats_log2})";
(SELECT DISTINCT l.course
FROM {log} l
WHERE $timesql)";
if (!$DB->execute($sql, array('courselevel'=>CONTEXT_COURSE))) {
$failed = true;
@ -273,11 +282,13 @@ function stats_cron_daily($maxdays=1) {
/// now get course total enrolments (roleid==0) - except frontpage
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments', $nextmidnight AS timeend, e.courseid AS courseid, 0 AS roleid,
COUNT(DISTINCT userid) AS stat1, 0 AS stat2
FROM {enrol} e
JOIN {user_enrolments} ue ON ue.enrolid = e.id
GROUP BY courseid
SELECT 'enrolments', timeend, id, nroleid, COUNT(DISTINCT userid), 0
FROM (
SELECT $nextmidnight AS timeend, e.courseid AS id, 0 AS nroleid, ue.userid
FROM {enrol} e
JOIN {user_enrolments} ue ON ue.enrolid = e.id
) inline_view
GROUP BY timeend, id, nroleid
HAVING COUNT(DISTINCT userid) > 0";
if ($logspresent and !$DB->execute($sql)) {
@ -292,16 +303,16 @@ function stats_cron_daily($maxdays=1) {
JOIN {user_enrolments} ue ON ue.enrolid = e.id
WHERE e.courseid = {stats_daily}.courseid AND
EXISTS (SELECT 'x'
FROM {tmp_stats_log1} l
FROM {log} l
WHERE l.course = {stats_daily}.courseid AND
l.userid = ue.userid))
l.userid = ue.userid AND $timesql))
WHERE {stats_daily}.stattype = 'enrolments' AND
{stats_daily}.timeend = $nextmidnight AND
{stats_daily}.roleid = 0 AND
{stats_daily}.courseid IN
(SELECT l.course
FROM {tmp_stats_log2} l
WHERE l.course <> ".SITEID.")";
FROM {log} l
WHERE $timesql AND l.course <> ".SITEID.")";
if ($logspresent and !$DB->execute($sql, array())) {
$failed = true;
@ -309,7 +320,7 @@ function stats_cron_daily($maxdays=1) {
}
stats_daily_progress('6');
/// frontpage(==site) enrolments total
/// frontapge(==site) enrolments total
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments', $nextmidnight, ".SITEID.", 0,
@ -318,8 +329,8 @@ function stats_cron_daily($maxdays=1) {
WHERE u.deleted = 0) AS stat1,
(SELECT COUNT(DISTINCT u.id)
FROM {user} u
JOIN {tmp_stats_log1} l ON l.userid = u.id
WHERE u.deleted = 0) AS stat2" .
JOIN {log} l ON l.userid = u.id
WHERE u.deleted = 0 AND $timesql) AS stat2" .
$DB->sql_null_from_clause();
if ($logspresent and !$DB->execute($sql)) {
@ -349,8 +360,8 @@ function stats_cron_daily($maxdays=1) {
WHERE u.deleted = 0) AS stat1,
(SELECT COUNT(DISTINCT u.id)
FROM {user} u
JOIN {tmp_stats_log1} l ON l.userid = u.id
WHERE u.deleted = 0) AS stat2" .
JOIN {log} l ON l.userid = u.id
WHERE u.deleted = 0 AND $timesql) AS stat2" .
$DB->sql_null_from_clause();;
if ($logspresent and !$DB->execute($sql)) {
@ -365,17 +376,29 @@ function stats_cron_daily($maxdays=1) {
}
/// 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($postactionssql, $params2) = $DB->get_in_or_equal($postactions, SQL_PARAMS_NAMED, 'post');
$sql = "INSERT INTO {stats_user_daily} (stattype, timeend, courseid, userid, statsreads, statswrites)
SELECT 'activity' AS stattype, $nextmidnight AS timeend, course as courseid, userid,
SUM(CASE WHEN action $viewactionssql THEN 1 ELSE 0 END) AS statsreads,
SUM(CASE WHEN action $postactionssql THEN 1 ELSE 0 END) AS statswrites
FROM {tmp_stats_log1} l
WHERE !(course = 0 AND userid = 0)
GROUP BY userid, courseid";
SELECT 'activity' AS stattype, $nextmidnight AS timeend, d.courseid, d.userid,
(SELECT COUNT('x')
FROM {log} l
WHERE l.userid = d.userid AND
l.course = d.courseid AND $timesql AND
l.action $viewactionssql) AS statsreads,
(SELECT COUNT('x')
FROM {log} l
WHERE l.userid = d.userid AND
l.course = d.courseid AND $timesql AND
l.action $postactionssql) AS statswrites
FROM (SELECT DISTINCT u.id AS userid, l.course AS courseid
FROM {user} u, {log} l
WHERE u.id = l.userid AND $timesql
UNION
SELECT 0 AS userid, ".SITEID." AS courseid" . $DB->sql_null_from_clause() . ") d";
// can not use group by here because pg can not handle it :-(
if ($logspresent and !$DB->execute($sql, array_merge($params1, $params2))) {
$failed = true;
@ -388,11 +411,18 @@ function stats_cron_daily($maxdays=1) {
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity' AS stattype, $nextmidnight AS timeend, c.id AS courseid, 0,
SUM(CASE WHEN l.action $viewactionssql THEN 1 ELSE 0 END) AS stat1,
SUM(CASE WHEN l.action $postactionssql THEN 1 ELSE 0 END) AS stat2
FROM {course} c, {tmp_stats_log1} l
WHERE l.course = c.id
GROUP BY courseid";
(SELECT COUNT('x')
FROM {log} l1
WHERE l1.course = c.id AND l1.action $viewactionssql AND
$timesql1) AS stat1,
(SELECT COUNT('x')
FROM {log} l2
WHERE l2.course = c.id AND l2.action $postactionssql AND
$timesql2) AS stat2
FROM {course} c
WHERE EXISTS (SELECT 'x'
FROM {log} l
WHERE l.course = c.id and $timesql)";
if ($logspresent and !$DB->execute($sql, array_merge($params1, $params2))) {
$failed = true;
@ -405,9 +435,9 @@ function stats_cron_daily($maxdays=1) {
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight AS timeend, courseid, roleid, SUM(statsreads), SUM(statswrites)
SELECT 'activity', timeend, courseid, roleid, SUM(statsreads), SUM(statswrites)
FROM (
SELECT pl.courseid, pl.roleid, sud.statsreads, sud.statswrites
SELECT $nextmidnight AS timeend, pl.courseid, pl.roleid, sud.statsreads, sud.statswrites
FROM {stats_user_daily} sud,
(SELECT DISTINCT ra.userid, ra.roleid, e.courseid
FROM {role_assignments} ra
@ -436,10 +466,9 @@ function stats_cron_daily($maxdays=1) {
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight as timeend, courseid, $guestrole AS roleid,
SUM(statsreads), SUM(statswrites)
SELECT 'activity', timeend, courseid, nroleid, SUM(statsreads), SUM(statswrites)
FROM (
SELECT sud.courseid, sud.statsreads, sud.statswrites
SELECT $nextmidnight AS timeend, sud.courseid, $guestrole AS nroleid, sud.statsreads, sud.statswrites
FROM {stats_user_daily} sud
WHERE sud.timeend = $nextmidnight AND sud.courseid <> ".SITEID." AND
sud.stattype='activity' AND
@ -449,7 +478,7 @@ function stats_cron_daily($maxdays=1) {
JOIN {enrol} e ON ue.enrolid = e.id
WHERE e.courseid = sud.courseid))
) inline_view
GROUP BY timeend, courseid, roleid
GROUP BY timeend, courseid, nroleid
HAVING SUM(statsreads) > 0 OR SUM(statswrites) > 0";
if ($logspresent and !$DB->execute($sql, array())) {
@ -462,10 +491,9 @@ function stats_cron_daily($maxdays=1) {
/// how many view actions for each role on frontpage - excluding guests, not-logged-in and default frontpage role
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight AS timeend, courseid, roleid,
SUM(statsreads), SUM(statswrites)
SELECT 'activity', timeend, courseid, roleid, SUM(statsreads), SUM(statswrites)
FROM (
SELECT pl.courseid, pl.roleid, sud.statsreads, sud.statswrites
SELECT $nextmidnight AS timeend, pl.courseid, pl.roleid, sud.statsreads, sud.statswrites
FROM {stats_user_daily} sud,
(SELECT DISTINCT ra.userid, ra.roleid, c.instanceid AS courseid
FROM {role_assignments} ra
@ -493,10 +521,9 @@ function stats_cron_daily($maxdays=1) {
/// how many view actions for default frontpage role on frontpage only
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', timeend, courseid, $defaultfproleid AS roleid,
SUM(statsreads), SUM(statswrites)
SELECT 'activity', timeend, courseid, nroleid, SUM(statsreads), SUM(statswrites)
FROM (
SELECT sud.timeend AS timeend, sud.courseid, sud.statsreads, sud.statswrites
SELECT sud.timeend AS timeend, sud.courseid, $defaultfproleid AS nroleid, sud.statsreads, sud.statswrites
FROM {stats_user_daily} sud
WHERE sud.timeend = :nextm AND sud.courseid = :siteid AND
sud.stattype='activity' AND
@ -506,7 +533,7 @@ function stats_cron_daily($maxdays=1) {
WHERE ra.roleid <> $guestrole AND
ra.roleid <> $defaultfproleid AND ra.contextid = :fpcontext)
) inline_view
GROUP BY timeend, courseid, roleid
GROUP BY timeend, courseid, nroleid
HAVING SUM(statsreads) > 0 OR SUM(statswrites) > 0";
if ($logspresent and !$DB->execute($sql, array('fpcontext'=>$fpcontext->id, 'siteid'=>SITEID, 'nextm'=>$nextmidnight))) {
@ -518,10 +545,9 @@ function stats_cron_daily($maxdays=1) {
/// how many view actions for guests or not-logged-in on frontpage
$sql = "INSERT INTO {stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight AS timeend, ".SITEID." AS courseid, $guestrole AS roleid,
SUM(statsreads), SUM(statswrites)
SELECT 'activity', timeend, courseid, nroleid, SUM(statsreads), SUM(statswrites)
FROM (
SELECT pl.statsreads, pl.statswrites
SELECT $nextmidnight AS timeend, ".SITEID." AS courseid, $guestrole AS nroleid, pl.statsreads, pl.statswrites
FROM (
SELECT sud.statsreads, sud.statswrites
FROM {stats_user_daily} sud
@ -530,7 +556,7 @@ function stats_cron_daily($maxdays=1) {
sud.stattype='activity'
) pl
) inline_view
GROUP BY timeend, courseid, roleid
GROUP BY timeend, courseid, nroleid
HAVING SUM(statsreads) > 0 OR SUM(statswrites) > 0";
if ($logspresent and !$DB->execute($sql)) {
@ -547,8 +573,6 @@ function stats_cron_daily($maxdays=1) {
$nextmidnight = stats_get_next_day_start($nextmidnight);
}
stats_drop_logs_view();
set_cron_lock('statsrunning', null);
if ($failed) {
@ -1438,73 +1462,3 @@ function stats_check_uptodate($courseid=0) {
//return error as string
return get_string('statscatchupmode','error',$a);
}
/**
* Creates tmp log table
*
* @param timestart timestamp of the start time of logs view
* @param timeend timestamp of the end time of logs view
* @returns boolen success (true) or failure(false)
*/
function stats_create_logs_view ( $timestart, $timeend ) {
global $CFG, $DB;
$dbman = $DB->get_manager(); // We are going to use database_manager services
stats_drop_logs_view();
$table = new xmldb_table('tmp_stats_log1');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
$table->add_field('action', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('tmp_stats_log_course_ix', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table->add_index('tmp_stats_log_act_ix', XMLDB_INDEX_NOTUNIQUE, array('action'));
$table->add_index('tmp_stats_log_user_ix', XMLDB_INDEX_NOTUNIQUE, array('userid'));
$table->add_index('tmp_stats_log_usecouact_ix', XMLDB_INDEX_NOTUNIQUE, array('userid','course','action'));
$dbman->create_temp_table($table);
$table->name = 'tmp_stats_log2';
$dbman->create_temp_table($table);
$sql = "INSERT INTO {tmp_stats_log1}
SELECT id, userid, course, action FROM {log} l
WHERE l.time >= $timestart AND l.time < $timeend";
if (!$DB->execute($sql)) {
return false;
}
$sql = "INSERT INTO {tmp_stats_log2}
SELECT * FROM {tmp_stats_log1}";
if (!$DB->execute($sql)) {
return false;
}
return true;
}
/**
* Deletes summary logs table for stats calculation
*/
function stats_drop_logs_view() {
global $DB;
$dbman = $DB->get_manager();
if ( $dbman->table_exists('tmp_stats_log1') ) {
$table = new xmldb_table('tmp_stats_log1');
$dbman->drop_temp_table($table);
}
if ( $dbman->table_exists('tmp_stats_log2') ) {
$table = new xmldb_table('tmp_stats_log2');
$dbman->drop_temp_table($table);
}
}