public static function view_past($system, $time_unit, $duration) { $increments = Array(); // figure out value to use for $begin in sql query // and number of seconds for each $increment $time = time(); switch ($time_unit) { case 'day': $begin = $time - $duration * 86400; $increments = array_pad($increments, $duration, 86400); break; case 'week': $begin = $time - $duration * 86400 * 7; $increments = array_pad($increments, $duration, 86400 * 7); break; case 'month': $month = date('n', $time) + 1; $year = date('Y', $time) - 1; if ($month > 12) { $month = 1; $year += 1; } $begin = mktime(0, 0, 0, $month, 1, $year); $last_begin = $begin; for ($i = 0; $i < $duration; $i++) { $month += 1; if ($month > 12) { $month = 1; $year += 1; } $next_begin = mktime(0, 0, 0, $month, 1, $year); $increments[] = $next_begin - $last_begin; $last_begin = $next_begin; } break; case 'quarter': $current_quarter = PageViews::quarter_of($time); $month = date('n', $current_quarter) + 3; $year = date('Y', $current_quarter) - 3; if ($month > 12) { $month -= 12; $year += 1; } $begin = mktime(0, 0, 0, $month, 1, $year); $last_begin = $begin; for ($i = 0; $i <= $duration; $i++) { $month += 3; if ($month > 12) { $month -= 12; $year += 1; } $next_begin = mktime(0, 0, 0, $month, 1, $year); $increments[] = $next_begin - $last_begin; $last_begin = $next_begin; } break; } $views = Array(); for ($i = 0; $i < $duration; $i++) { $sql_start_date = date('Y-m-d', $begin); $end = $begin + $increments[$i]; $sql_end_date = date('Y-m-d', $end); $new_view = Array('date' => $begin, 'total' => 0); // array below has index for each module, bucket // and the index 'day' for the day or first day of week/month $results = self::getTimeSeries($system, $sql_start_date, NULL, NULL, $sql_end_date); foreach ($results as $row) { if (array_key_exists('platform', $row)) { if (!array_key_exists($row['platform'], $new_view)) $new_view[$row['platform']] = 0; $new_view[$row['platform']] += $row['viewcount']; } if (array_key_exists('module', $row)) { if (!array_key_exists($row['platform'], $new_view)) $new_view[$row['platform']] = 0; $new_view[$row['module']] += $row['viewcount']; } $new_view['total'] += $row['viewcount']; } $views[] = $new_view; $begin = $end; } return $views; }
function aggregate_days($days, $interval_type, $duration) { $intervals = array(); $counter = array(); // get all intervals, fill in 0 if missing $last_day = $days[count($days) - 1]['date']; $year = substr($last_day, 0, 4); $month = substr($last_day, 5, 7); switch ($interval_type) { case 'quarter': $month = (int) $month - 1; $month = $month - $month % 3 + 1; $utime = mktime(0, 0, 0, $month, 1, $year); break; case 'month': $utime = mktime(0, 0, 0, $month, 1, $year); break; case 'day': case 'week': default: $utime = strtotime($last_day); } $dayofweek = date('w', $utime); for ($i = 1; $i <= $duration; $i++) { $counter[$utime] = 0; switch ($interval_type) { case 'day': $utime -= 86400; break; case 'week': $is_dst = date('I', $utime); $utime -= 86400 * 7; $was_dst = date('I', $utime); // this is further in the past $utime -= ($was_dst - $is_dst) * 3600; break; case 'month': if ($month <= $i) { $utime = mktime(0, 0, 0, $month - $i + 12, 1, $year - 1); } else { $utime = mktime(0, 0, 0, $month - $i, 1, $year); } break; case 'quarter': if ($month - $month % 3 <= $i) { $utime = mktime(0, 0, 0, $month - 3 * $i + 12, 1, $year - 1); } else { $utime = mktime(0, 0, 0, $month - 3 * $i, 1, $year); } break; } } foreach ($days as $day) { $utime = strtotime($day['date']); switch ($interval_type) { case 'day': $interval = $utime; break; case 'week': // week starting on specified date $interval = $utime; while (date('w', $interval) != $dayofweek) { $was_dst = date('I', $interval); $interval += 86400; $is_dst = date('I', $interval); $interval += ($was_dst - $is_dst) * 3600; } break; case 'month': $interval = mktime(0, 0, 0, date('n', $utime), 1, date('Y', $utime)); break; case 'quarter': $interval = PageViews::quarter_of($utime); break; } $counter[$interval] += $day['count']; } ksort($counter); foreach ($counter as $interval => $count) { $intervals[] = array('date' => $interval, 'count' => $count); } return $intervals; }