Example #1
0
/**
 * Execute monthly statistics gathering
 * @return boolean success
 */
function stats_cron_monthly()
{
    global $CFG, $DB;
    $now = time();
    // read last execution date from db
    if (!($timestart = get_config(NULL, 'statslastmonthly'))) {
        $timestart = stats_get_base_monthly(stats_get_start_from('monthly'));
        set_config('statslastmonthly', $timestart);
    }
    $nextstartmonth = stats_get_next_month_start($timestart);
    // are there any months that need to be processed?
    if ($now < $nextstartmonth) {
        return true;
        // everything ok and up-to-date
    }
    $timeout = empty($CFG->statsmaxruntime) ? 60 * 60 * 24 : $CFG->statsmaxruntime;
    if (!set_cron_lock('statsrunning', $now + $timeout)) {
        return false;
    }
    // fisr delete entries that should not be there yet
    $DB->delete_records_select('stats_monthly', "timeend > {$timestart}");
    $DB->delete_records_select('stats_user_monthly', "timeend > {$timestart}");
    $startmonth = stats_get_base_monthly($now);
    mtrace("Running monthly statistics gathering, starting at {$timestart}:");
    $months = 0;
    while ($now > $nextstartmonth) {
        @set_time_limit($timeout - 200);
        $months++;
        if ($months > 1) {
            // move the lock
            set_cron_lock('statsrunning', time() + $timeout, true);
        }
        $logtimesql = "l.time >= {$timestart} AND l.time < {$nextstartmonth}";
        $stattimesql = "timeend > {$timestart} AND timeend <= {$nextstartmonth}";
        $monthstart = time();
        stats_progress('init');
        /// process login info first
        $sql = "INSERT INTO {stats_user_monthly} (stattype, timeend, courseid, userid, statsreads)\n\n                SELECT 'logins', timeend, courseid, userid, COUNT(statsreads)\n                  FROM (\n                           SELECT {$nextstartmonth} AS timeend, " . SITEID . " as courseid, l.userid, l.id AS statsreads\n                             FROM {log} l\n                            WHERE action = 'login' AND {$logtimesql}\n                       ) inline_view\n              GROUP BY timeend, courseid, userid";
        $DB->execute($sql);
        stats_progress('1');
        $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2)\n\n                SELECT 'logins' AS stattype, {$nextstartmonth} AS timeend, " . SITEID . " as courseid, 0,\n                       COALESCE((SELECT SUM(statsreads)\n                                   FROM {stats_user_monthly} s1\n                                  WHERE s1.stattype = 'logins' AND timeend = {$nextstartmonth}), 0) AS nstat1,\n                       (SELECT COUNT('x')\n                          FROM {stats_user_monthly} s2\n                         WHERE s2.stattype = 'logins' AND timeend = {$nextstartmonth}) AS nstat2" . $DB->sql_null_from_clause();
        $DB->execute($sql);
        stats_progress('2');
        /// now enrolments averages
        $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2)\n\n                SELECT 'enrolments', ntimeend, courseid, roleid, " . $DB->sql_ceil('AVG(stat1)') . ", " . $DB->sql_ceil('AVG(stat2)') . "\n                  FROM (\n                           SELECT {$nextstartmonth} AS ntimeend, courseid, roleid, stat1, stat2\n                             FROM {stats_daily} sd\n                            WHERE stattype = 'enrolments' AND {$stattimesql}\n                       ) inline_view\n              GROUP BY ntimeend, courseid, roleid";
        $DB->execute($sql);
        stats_progress('3');
        /// activity read/write averages
        $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2)\n\n                SELECT 'activity', ntimeend, courseid, roleid, SUM(stat1), SUM(stat2)\n                  FROM (\n                           SELECT {$nextstartmonth} AS ntimeend, courseid, roleid, stat1, stat2\n                             FROM {stats_daily}\n                            WHERE stattype = 'activity' AND {$stattimesql}\n                       ) inline_view\n              GROUP BY ntimeend, courseid, roleid";
        $DB->execute($sql);
        stats_progress('4');
        /// user read/write averages
        $sql = "INSERT INTO {stats_user_monthly} (stattype, timeend, courseid, userid, statsreads, statswrites)\n\n                SELECT 'activity', ntimeend, courseid, userid, SUM(statsreads), SUM(statswrites)\n                  FROM (\n                           SELECT {$nextstartmonth} AS ntimeend, courseid, userid, statsreads, statswrites\n                             FROM {stats_user_daily}\n                            WHERE stattype = 'activity' AND {$stattimesql}\n                       ) inline_view\n              GROUP BY ntimeend, courseid, userid";
        $DB->execute($sql);
        stats_progress('5');
        set_config('statslastmonthly', $nextstartmonth);
        $elapsed = time() - $monthstart;
        mtrace(" finished until {$nextstartmonth}: " . userdate($nextstartmonth) . " (in {$elapsed} s)");
        $timestart = $nextstartmonth;
        $nextstartmonth = stats_get_next_month_start($nextstartmonth);
    }
    set_cron_lock('statsrunning', null);
    mtrace("...completed {$months} months of statistics.");
    return true;
}
Example #2
0
    /**
     * Test progress output when debug is off
     */
    public function test_statslib_progress_no_debug() {
        global $CFG;

        $CFG->debug = DEBUG_NONE;
        $this->expectOutputString('.');
        stats_progress('init');
        stats_progress('1');
    }
Example #3
0
 /**
  * Test progress output when debug is off.
  */
 public function test_statslib_progress_no_debug()
 {
     set_debugging(DEBUG_NONE);
     $this->expectOutputString('.');
     stats_progress('init');
     stats_progress('1');
     $this->resetDebugging();
 }
Example #4
0
/**
 * Execute monthly statistics gathering
 * @return boolean success
 */
function stats_cron_monthly() {
    global $CFG, $DB;

    $now = time();

    // read last execution date from db
    if (!$timestart = get_config(NULL, 'statslastmonthly')) {
        $timestart = stats_get_base_monthly(stats_get_start_from('monthly'));
        set_config('statslastmonthly', $timestart);
    }

    $nextstartmonth = stats_get_next_month_start($timestart);

    // are there any months that need to be processed?
    if ($now < $nextstartmonth) {
        return true; // everything ok and up-to-date
    }

    $timeout = empty($CFG->statsmaxruntime) ? 60*60*24 : $CFG->statsmaxruntime;

    if (!set_cron_lock('statsrunning', $now + $timeout)) {
        return false;
    }

    // fisr delete entries that should not be there yet
    $DB->delete_records_select('stats_monthly', "timeend > $timestart");
    $DB->delete_records_select('stats_user_monthly', "timeend > $timestart");

    $startmonth = stats_get_base_monthly($now);


    mtrace("Running monthly statistics gathering, starting at $timestart:");
    cron_trace_time_and_memory();

    $months = 0;
    while ($now > $nextstartmonth) {
        core_php_time_limit::raise($timeout - 200);
        $months++;

        if ($months > 1) {
            // move the lock
            set_cron_lock('statsrunning', time() + $timeout, true);
        }

        $stattimesql = "timeend > $timestart AND timeend <= $nextstartmonth";

        $monthstart = time();
        stats_progress('init');

    /// process login info first
        $sql = "INSERT INTO {stats_user_monthly} (stattype, timeend, courseid, userid, statsreads)

                SELECT 'logins', timeend, courseid, userid, SUM(statsreads)
                  FROM (
                           SELECT $nextstartmonth AS timeend, courseid, userid, statsreads
                             FROM {stats_user_daily} sd
                            WHERE stattype = 'logins' AND $stattimesql
                       ) inline_view
              GROUP BY timeend, courseid, userid
                HAVING SUM(statsreads) > 0";

        $DB->execute($sql);

        stats_progress('1');

        $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2)

                SELECT 'logins' AS stattype, $nextstartmonth AS timeend, ".SITEID." as courseid, 0,
                       COALESCE((SELECT SUM(statsreads)
                                   FROM {stats_user_monthly} s1
                                  WHERE s1.stattype = 'logins' AND timeend = $nextstartmonth), 0) AS nstat1,
                       (SELECT COUNT('x')
                          FROM {stats_user_monthly} s2
                         WHERE s2.stattype = 'logins' AND timeend = $nextstartmonth) AS nstat2" .
                $DB->sql_null_from_clause();

        $DB->execute($sql);

        stats_progress('2');

    /// now enrolments averages
        $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2)

                SELECT 'enrolments', ntimeend, courseid, roleid, " . $DB->sql_ceil('AVG(stat1)') . ", " . $DB->sql_ceil('AVG(stat2)') . "
                  FROM (
                           SELECT $nextstartmonth AS ntimeend, courseid, roleid, stat1, stat2
                             FROM {stats_daily} sd
                            WHERE stattype = 'enrolments' AND $stattimesql
                       ) inline_view
              GROUP BY ntimeend, courseid, roleid";

        $DB->execute($sql);

        stats_progress('3');

    /// activity read/write averages
        $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2)

                SELECT 'activity', ntimeend, courseid, roleid, SUM(stat1), SUM(stat2)
                  FROM (
                           SELECT $nextstartmonth AS ntimeend, courseid, roleid, stat1, stat2
                             FROM {stats_daily}
                            WHERE stattype = 'activity' AND $stattimesql
                       ) inline_view
              GROUP BY ntimeend, courseid, roleid";

        $DB->execute($sql);

        stats_progress('4');

    /// user read/write averages
        $sql = "INSERT INTO {stats_user_monthly} (stattype, timeend, courseid, userid, statsreads, statswrites)

                SELECT 'activity', ntimeend, courseid, userid, SUM(statsreads), SUM(statswrites)
                  FROM (
                           SELECT $nextstartmonth AS ntimeend, courseid, userid, statsreads, statswrites
                             FROM {stats_user_daily}
                            WHERE stattype = 'activity' AND $stattimesql
                       ) inline_view
              GROUP BY ntimeend, courseid, userid";

        $DB->execute($sql);

        stats_progress('5');

        set_config('statslastmonthly', $nextstartmonth);
        $elapsed = time() - $monthstart;
        mtrace(" finished until $nextstartmonth: ".userdate($nextstartmonth) ." (in $elapsed s)");

        $timestart      = $nextstartmonth;
        $nextstartmonth = stats_get_next_month_start($nextstartmonth);
    }

    set_cron_lock('statsrunning', null);
    mtrace("...completed $months months of statistics.");
    return true;
}