Exemplo n.º 1
0
/**
 * Execute monthly statistics gathering
 * @return boolean success
 */
function stats_cron_monthly()
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/adminlib.php';
    $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)\n\n                SELECT 'logins', timeend, courseid, userid, SUM(statsreads)\n                  FROM (\n                           SELECT {$nextstartmonth} AS timeend, courseid, userid, statsreads\n                             FROM {stats_user_daily} sd\n                            WHERE stattype = 'logins' AND {$stattimesql}\n                       ) inline_view\n              GROUP BY timeend, courseid, userid\n                HAVING SUM(statsreads) > 0";
        $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;
}
Exemplo n.º 2
0
function stats_fix_zeros($stats, $timeafter, $timestr, $line2 = true, $line3 = false)
{
    if (empty($stats)) {
        return;
    }
    $timestr = str_replace('user_', '', $timestr);
    // just in case.
    $fun = 'stats_get_base_' . $timestr;
    $now = $fun();
    $times = array();
    // add something to timeafter since it is our absolute base
    $actualtimes = array();
    foreach ($stats as $statid => $s) {
        //normalize the times in stats - those might have been created in different timezone, DST etc.
        $s->timeend = $fun($s->timeend + 60 * 60 * 5);
        $stats[$statid] = $s;
        $actualtimes[] = $s->timeend;
    }
    $timeafter = array_pop(array_values($actualtimes));
    while ($timeafter < $now) {
        $times[] = $timeafter;
        if ($timestr == 'daily') {
            $timeafter = stats_get_next_day_start($timeafter);
        } else {
            if ($timestr == 'weekly') {
                $timeafter = stats_get_next_week_start($timeafter);
            } else {
                if ($timestr == 'monthly') {
                    $timeafter = stats_get_next_month_start($timeafter);
                } else {
                    return $stats;
                    // this will put us in a never ending loop.
                }
            }
        }
    }
    foreach ($times as $count => $time) {
        if (!in_array($time, $actualtimes) && $count != count($times) - 1) {
            $newobj = new StdClass();
            $newobj->timeend = $time;
            $newobj->id = 0;
            $newobj->roleid = 0;
            $newobj->line1 = 0;
            if (!empty($line2)) {
                $newobj->line2 = 0;
            }
            if (!empty($line3)) {
                $newobj->line3 = 0;
            }
            $newobj->zerofixed = true;
            $stats[] = $newobj;
        }
    }
    usort($stats, "stats_compare_times");
    return $stats;
}
Exemplo n.º 3
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";

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

                SELECT 'logins', timeend, courseid, userid, COUNT(statsreads)
                  FROM (
                           SELECT $nextstartmonth AS timeend, ".SITEID." as courseid, l.userid, l.id AS statsreads
                             FROM {log} l
                            WHERE action = 'login' AND $logtimesql
                       ) inline_view
              GROUP BY timeend, courseid, userid";

        $DB->execute($sql);

        $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);


    /// 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);


    /// 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);


    /// 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);

        set_config('statslastmonthly', $nextstartmonth);
        mtrace(" finished until $nextstartmonth: ".userdate($nextstartmonth));

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

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