Permalink
Browse files

Various fixes to stats after our attempts to run it on a large real d…

…ataset
  • Loading branch information...
mjollnir_
mjollnir_ committed Oct 4, 2005
1 parent 1fc53fa commit 5560ed98c8051b48af51e9940d2363fc7a5e3724
Showing with 43 additions and 31 deletions.
  1. +15 −9 admin/cron.php
  2. +28 −22 lib/statslib.php
View
@@ -181,29 +181,35 @@
}
if (!empty($CFG->enablestats)) {
+ $time = 60*60*20; // set it to 20 here for first run... (overridden by $CFG)
+ $clobber = true;
+ if (!empty($CFG->statsmaxruntime)) {
+ $time = $CFG->statsmaxruntime+(60*30); // add on half an hour just to make sure (it could take that long to break out of the loop)
+ }
if (!get_field_sql('SELECT id FROM '.$CFG->prefix.'stats_daily LIMIT 1')) {
// first run, set another lock. we'll check for this in subsequent runs to set the timeout to later for the normal lock.
- set_cron_lock('statsfirstrunlock',true,60*60*20,true);
+ set_cron_lock('statsfirstrunlock',true,$time,true);
$firsttime = true;
}
- $time = 60*60*2;
- $clobber = true;
+ $time = 60*60*2; // this time set to 2.. (overridden by $CFG)
+ if (!empty($CFG->statsmaxruntime)) {
+ $time = $CFG->statsmaxruntime+(60*30); // add on half an hour to make sure (it could take that long to break out of the loop)
+ }
if ($config = get_record('config','name','statsfirstrunlock')) {
if (!empty($config->value)) {
- $time = 60*60*20;
- $clobber = false;
+ $clobber = false; // if we're on the first run, just don't clobber it.
}
}
if (set_cron_lock('statsrunning',true,$time, $clobber)) {
require_once($CFG->dirroot.'/lib/statslib.php');
$return = stats_cron_daily();
- if ($return == STATS_RUN_COMPLETE) {
+ if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) {
stats_cron_weekly();
}
- if ($return == STATS_RUN_COMPLETE) {
- stats_cron_monthly();
+ if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) {
+ $return = $return && stats_cron_monthly();
}
- if ($return == STATS_RUN_COMPLETE) {
+ if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) {
stats_clean_old();
}
set_cron_lock('statsrunning',false);
View
@@ -49,31 +49,30 @@ function stats_cron_daily () {
global $CFG;
if (empty($CFG->enablestats)) {
- return;
+ return STATS_RUN_ABORTED;
}
if (!$timestart = stats_get_start_from('daily')) {
- return;
+ return STATS_RUN_ABORTED;
}
$midnight = usergetmidnight(time());
// check to make sure we're due to run, at least one day after last run
if ((time() - 24*60*60) < $CFG->statslastdaily) {
- return;
+ return STATS_RUN_ABORTED;
}
//and we're not before our runtime
$timetocheck = strtotime("$CFG->statsruntimestarthour:$CFG->statsruntimestartminute today");
if (time() < $timetocheck) {
- return;
+ return STATS_RUN_ABORTED;
}
mtrace("Running daily statistics gathering...");
set_config('statslastdaily',time());
-
$return = STATS_RUN_COMPLETE; // optimistic
static $daily_modules;
@@ -92,10 +91,11 @@ function stats_cron_daily () {
$nextmidnight = $timestart + (60*60*24);
if (!$courses = get_records('course','','','','id,1')) {
- return;
+ return STATS_RUN_ABORTED;
}
$days = 0;
+ mtrace("starting at $timestart");
while ($midnight >= $nextmidnight) {
$timesql = " (l.time > $timestart AND l.time < $nextmidnight) ";
@@ -165,10 +165,10 @@ function stats_cron_daily () {
stats_get_course_users($course,$timesql,$students,$teachers);
foreach ($students as $user) {
- stats_do_daily_user_cron($course,$user,1,$timesql,$nextmidnight,'daily',$daily_mods);
+ stats_do_daily_user_cron($course,$user,1,$timesql,$nextmidnight,'daily',$daily_modules);
}
foreach ($teachers as $user) {
- stats_do_daily_user_cron($course,$user,2,$timesql,$nextmidnight,'daily',$daily_mods);
+ stats_do_daily_user_cron($course,$user,2,$timesql,$nextmidnight,'daily',$daily_modules);
}
}
$timestart = $nextmidnight;
@@ -181,6 +181,7 @@ function stats_cron_daily () {
break;
}
}
+ mtrace("got up to ".$timestart);
mtrace("Completed $days days");
return $return;
@@ -192,24 +193,24 @@ function stats_cron_weekly () {
global $CFG;
if (empty($CFG->enablestats)) {
- return;
+ STATS_RUN_ABORTED;
}
if (!$timestart = stats_get_start_from('weekly')) {
- return;
+ return STATS_RUN_ABORTED;
}
// check to make sure we're due to run, at least one week after last run
$sunday = stats_get_base_weekly();
if ((time() - (7*24*60*60)) <= $CFG->statslastweekly) {
- return;
+ return STATS_RUN_ABORTED;
}
//and we're not before our runtime
$timetocheck = strtotime("$CFG->statsruntimestarthour:$CFG->statsruntimestartminute today");
if (time() < $timetocheck) {
- return;
+ return STATS_RUN_ABORTED;
}
mtrace("Running weekly statistics gathering...");
@@ -233,10 +234,11 @@ function stats_cron_weekly () {
$nextsunday = $timestart + (60*60*24*7);
if (!$courses = get_records('course','','','','id,1')) {
- return;
+ return STATS_RUN_ABORTED;
}
$weeks = 0;
+ mtrace("starting at $timestart");
while ($sunday >= $nextsunday) {
$timesql = " (timeend > $timestart AND timeend < $nextsunday) ";
@@ -268,11 +270,11 @@ function stats_cron_weekly () {
stats_get_course_users($course,$timesql,$students,$teachers);
foreach ($students as $user) {
- stats_do_aggregate_user_cron($course,$user,1,$timesql,$nextsunday,'weekly',$weekly_mods);
+ stats_do_aggregate_user_cron($course,$user,1,$timesql,$nextsunday,'weekly',$weekly_modules);
}
foreach ($teachers as $user) {
- stats_do_aggregate_user_cron($course,$user,2,$timesql,$nextsunday,'weekly',$weekly_mods);
+ stats_do_aggregate_user_cron($course,$user,2,$timesql,$nextsunday,'weekly',$weekly_modules);
}
}
@@ -286,6 +288,7 @@ function stats_cron_weekly () {
break;
}
}
+ mtrace("got up to ".$timestart);
mtrace("Completed $weeks weeks");
return $return;
}
@@ -295,24 +298,24 @@ function stats_cron_monthly () {
global $CFG;
if (empty($CFG->enablestats)) {
- return;
+ return STATS_RUN_ABORTED;
}
if (!$timestart = stats_get_start_from('monthly')) {
- return;
+ return STATS_RUN_ABORTED;
}
// check to make sure we're due to run, at least one month after last run
$monthend = stats_get_base_monthly();
if ((time() - (31*24*60*60)) <= $CFG->statslastmonthly) {
- return;
+ return STATS_RUN_ABORTED;
}
//and we're not before our runtime
$timetocheck = strtotime("$CFG->statsruntimestarthour:$CFG->statsruntimestartminute today");
if (time() < $timetocheck) {
- return;
+ return STATS_RUN_ABORTED;
}
mtrace("Running monthly statistics gathering...");
@@ -336,10 +339,11 @@ function stats_cron_monthly () {
$nextmonthend = stats_get_next_monthend($timestart);
if (!$courses = get_records('course','','','','id,1')) {
- return;
+ return STATS_RUN_ABORTED;
}
$months = 0;
+ mtrace("starting from $timestart");
while ($monthend >= $nextmonthend) {
$timesql = " (timeend > $timestart AND timeend < $nextmonthend) ";
@@ -366,11 +370,11 @@ function stats_cron_monthly () {
stats_get_course_users($course,$timesql,$students,$teachers);
foreach ($students as $user) {
- stats_do_aggregate_user_cron($course,$user,1,$timesql,$nextmonthend,'monthly',$monthly_mods);
+ stats_do_aggregate_user_cron($course,$user,1,$timesql,$nextmonthend,'monthly',$monthly_modules);
}
foreach ($teachers as $user) {
- stats_do_aggregate_user_cron($course,$user,2,$timesql,$nextmonthend,'monthly',$monthly_mods);
+ stats_do_aggregate_user_cron($course,$user,2,$timesql,$nextmonthend,'monthly',$monthly_modules);
}
}
@@ -383,6 +387,7 @@ function stats_cron_monthly () {
$return = STATS_RUN_ABORTED;
}
}
+ mtrace("got up to $timestart");
mtrace("Completed $months months");
return $return;
}
@@ -444,6 +449,7 @@ function stats_get_next_monthend($lastmonth) {
}
function stats_clean_old() {
+ mtrace("Running stats cleanup tasks... ");
// delete dailies older than 2 months (to be safe)
$deletebefore = stats_get_next_monthend(strtotime('-2 months',time()));
delete_records_select('stats_daily',"timeend < $deletebefore");

0 comments on commit 5560ed9

Please sign in to comment.