// Call the daily and weekly processing tasks as needed. // ------------------------------------------------------------ $now = time(); // Figure out if it's been 24 hours since our last daily processing. if (state_get('project_usage_last_daily', 0) <= $now - PROJECT_USAGE_DAY) { project_usage_process_daily(); state_set('project_usage_last_daily', $now); } // We can't process the weekly data until the week has completed. To see if // there's data available: determine the last time we completed the weekly // processing and compare that to the start of this week. If the last // weekly processing occurred before the current week began then there should // be one (or more) week's worth of data ready to process. $default = $now - config_get('project_usage.settings', 'life_daily'); $last_weekly = state_get('project_usage_last_weekly', $default); $current_week_start = project_usage_weekly_timestamp(NULL, 0); if ($last_weekly <= $current_week_start) { project_usage_process_weekly($last_weekly); state_set('project_usage_last_weekly', $now); // Reset the list of active weeks. project_usage_get_active_weeks(TRUE); } // Wipe the cache of all expired usage pages. cache('project_usage')->flush(); /** * Process all the raw data up to the previous day. * * The primary key on the {project_usage_raw} table will prevent duplicate * records provided we process them once the day is complete. If we pull them * out too soon and the site checks in again they will be counted twice. */
/** * Compute the weekly summaries for the week starting at the given timestamp. * * @param $week_start_timestamp * Timestamp indicating the start of the week for which stats should be * calculated. */ function project_usage_process_weekly($week_start_timestamp) { // Ensure the timestamp is a starting timestamp. $start = project_usage_weekly_timestamp($week_start_timestamp); $end = project_usage_weekly_timestamp($week_start_timestamp, 1); // Safety check to prevent processing of the current week's numbers, as the // week has not yet finished. if ($end > REQUEST_TIME) { return; } $start_date = format_date($start, 'custom', 'Y-m-d'); $end_date = format_date(project_usage_daily_timestamp($end, -1), 'custom', 'Y-m-d'); // Try to compute the usage tallies per project and per release. If there // is a problem--perhaps some rows existed from a previous, incomplete // run that are preventing inserts, throw a watchdog error. try { $sql = "INSERT INTO {project_usage_week_project} (nid, timestamp, version_api, count) SELECT project_nid as nid, :start, version_api, COUNT(DISTINCT site_key) FROM {project_usage_day} WHERE timestamp >= :start AND timestamp < :end AND project_nid <> 0 GROUP BY project_nid, version_api"; $query_args = array(':start' => $start, ':end' => $end); $result = db_query($sql, $query_args); $project_count = $result->rowCount(); } catch (PDOException $e) { $project_count = 0; $substitutions = array('@start' => $start_date, '@end' => $end_date); watchdog('project_usage', 'Weekly project tallies for the week of @start through @end already calculated. No data updated.', $substitutions); } try { $sql = "INSERT INTO {project_usage_week_release} (nid, timestamp, count) SELECT release_nid as nid, :start, COUNT(DISTINCT site_key) FROM {project_usage_day} WHERE timestamp >= :start AND timestamp < :end AND release_nid <> 0 GROUP BY release_nid"; $query_args = array(':start' => $start, ':end' => $end); $result = db_query($sql, $query_args); $release_count = $result->rowCount(); } catch (PDOException $e) { $release_count = 0; $substitutions = array('@start' => $start_date, '@end' => $end_date); watchdog('project_usage', 'Weekly release tallies for the week of @start through @end already calculated. No data updated.', $substitutions); } $substitutions = array('@projects' => format_plural($project_count, '1 project', '@count projects'), '@releases' => format_plural($release_count, '1 release', '@count releases'), '@start' => $start_date, '@end' => $end_date); watchdog('project_usage', 'Completed weekly usage data processing on @projects and @releases for the week of @start through @end.', $substitutions); }