| 1 |
|
<?php |
| 2 |
|
// $Id: graphstat_statistics.inc,v 1.2.2.1 2009/01/25 14:32:28 profix898 Exp $ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* Implementation of hook_graphstat(). |
| 6 |
|
*/ |
| 7 |
|
function graphstat_statistics_graphstat() { |
| 8 |
|
global $user; |
| 9 |
|
|
| 10 |
|
// Daily statistics are only available if 'access_log' is enabled |
| 11 |
|
if (!variable_get('statistics_enable_access_log', 0)) |
| 12 |
|
return array(); |
| 13 |
|
|
| 14 |
|
if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) { |
| 15 |
|
$timezone = $user->timezone; |
| 16 |
|
} |
| 17 |
|
else { |
| 18 |
|
$timezone = variable_get('date_default_timezone', 0); |
| 19 |
|
} |
| 20 |
|
// Get oldest record and current date |
| 21 |
|
$min = db_result(db_query('SELECT MIN(timestamp) FROM {accesslog}')); |
| 22 |
|
if ($min) { |
| 23 |
|
$now = time(); |
| 24 |
|
// Figure out which local dates correspond with these timestamps - can be different between timezones |
| 25 |
|
$min_date = format_date($min, 'custom', 'm/d/Y'); |
| 26 |
|
$now_date = format_date($now, 'custom', 'm/d/Y'); |
| 27 |
|
// Figure out the (UNIX Epoch) timestamp that corresponds to when the first day began locally |
| 28 |
|
// Figure out the (UNIX Epoch) timestamp that corresponds to when today ends locally |
| 29 |
|
$min_date_frag = explode('/', $min_date); |
| 30 |
|
$now_date_frag = explode('/', $now_date); |
| 31 |
|
$gm_timestamp_min = gmmktime(0, 0, 0, $min_date_frag[0], $min_date_frag[1], $min_date_frag[2]); |
| 32 |
|
$gm_timestamp_now = gmmktime(23, 59, 59, $now_date_frag[0], $now_date_frag[1], $now_date_frag[2]); |
| 33 |
|
if ($timezone > 0) { |
| 34 |
|
$local_timestamp_min = $gm_timestamp_min - $timezone; |
| 35 |
|
$local_timestamp_now = $gm_timestamp_now - $timezone; |
| 36 |
|
} |
| 37 |
|
elseif ($timezone < 0) { |
| 38 |
|
$local_timestamp_min = $gm_timestamp_min + abs($timezone); |
| 39 |
|
$local_timestamp_now = $gm_timestamp_now + abs($timezone); |
| 40 |
|
} |
| 41 |
|
else { |
| 42 |
|
$local_timestamp_min = $gm_timestamp_min; |
| 43 |
|
$local_timestamp_now = $gm_timestamp_now; |
| 44 |
|
} |
| 45 |
|
// Build the filter options array with all dates |
| 46 |
|
$options = array('<'. t('Select a date') .'>'); |
| 47 |
|
while ($local_timestamp_min < $local_timestamp_now) { |
| 48 |
|
$options[$local_timestamp_min] = format_date($local_timestamp_min, 'custom', 'm/d/Y'); |
| 49 |
|
$local_timestamp_min = $local_timestamp_min + 86400; |
| 50 |
|
} |
| 51 |
|
} |
| 52 |
|
else { |
| 53 |
|
$options = array(NULL => t('No items available')); |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
$graphs = array(); |
| 57 |
|
$graphs['daily'] = array( |
| 58 |
|
'#title' => t('Daily'), |
| 59 |
|
'#filter' => array( |
| 60 |
|
'#options' => $options, |
| 61 |
|
'#callback' => 'graphstat_statistics_daily_filter' |
| 62 |
|
), |
| 63 |
|
'graph_page_generation' => array( |
| 64 |
|
'#title' => t('Average page generation time'), |
| 65 |
|
'#xlabel' => t('Time (hh:mm)'), |
| 66 |
|
'#ylabel' => t('Page generation / ms'), |
| 67 |
|
'#legend' => array(t('avg'), t('avg + stddev'), t('avg - stddev')), |
| 68 |
|
'#description' => t('Average (avg) page generation time +/- standard deviation (stddev).') |
| 69 |
|
), |
| 70 |
|
'graph_pages' => array( |
| 71 |
|
'#title' => t('Number of pages served (total)'), |
| 72 |
|
'#xlabel' => t('Time (hh:mm)'), |
| 73 |
|
'#ylabel' => t('# Pages') |
| 74 |
|
), |
| 75 |
|
'graph_unique_pages' => array( |
| 76 |
|
'#title' => t('Number of unique pages served'), |
| 77 |
|
'#xlabel' => t('Time (hh:mm)'), |
| 78 |
|
'#ylabel' => t('# Pages') |
| 79 |
|
), |
| 80 |
|
'graph_visitors' => array( |
| 81 |
|
'#title' => t('Number of unique visitors'), |
| 82 |
|
'#xlabel' => t('Time (hh:mm)'), |
| 83 |
|
'#ylabel' => t('# Visitors') |
| 84 |
|
) |
| 85 |
|
); |
| 86 |
|
|
| 87 |
|
return $graphs; |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
/** |
| 91 |
|
* Function graphstat_statistics_daily_filter(). |
| 92 |
|
*/ |
| 93 |
|
function graphstat_statistics_daily_filter(&$data, $start) { |
| 94 |
|
if (!$start) { |
| 95 |
|
return; |
| 96 |
|
} |
| 97 |
|
$stop = $start + 86400; |
| 98 |
|
// Average page generation time |
| 99 |
|
$loop = $start; |
| 100 |
|
while ($loop < $stop) { |
| 101 |
|
$stats = db_fetch_object(db_query('SELECT AVG(timer) AS avg, STDDEV(timer) AS dev FROM {accesslog} WHERE timestamp > %d AND timestamp <= %d', $loop, $loop + 3600)); |
| 102 |
|
$avg = $stats->avg ? round($stats->avg , 0) : 0; |
| 103 |
|
$dev = $stats->dev ? round($stats->dev, 0) : 0; |
| 104 |
|
$data['graph_page_generation'][] = array(format_date($loop, 'custom', 'H:i'), $avg, $avg + $dev, max($avg - $dev, 0)); |
| 105 |
|
$loop += 3600; |
| 106 |
|
} |
| 107 |
|
// Number of pages served (total) |
| 108 |
|
$loop = $start; |
| 109 |
|
while ($loop < $stop) { |
| 110 |
|
$stats = db_fetch_object(db_query('SELECT COUNT(aid) AS count FROM {accesslog} WHERE timestamp > %d AND timestamp <= %d', $loop, $loop + 3600)); |
| 111 |
|
$data['graph_pages'][format_date($loop, 'custom', 'H:i')] = $stats->count ? $stats->count : 0; |
| 112 |
|
$loop += 3600; |
| 113 |
|
} |
| 114 |
|
// Number of unique pages served |
| 115 |
|
$loop = $start; |
| 116 |
|
while ($loop < $stop) { |
| 117 |
|
// TODO: optimize query (using an additional table to store statistics) |
| 118 |
|
$stats = db_fetch_object(db_query('SELECT COUNT(DISTINCT(path)) AS count FROM {accesslog} WHERE timestamp > %d AND timestamp <= %d', $loop, $loop + 3600)); |
| 119 |
|
$data['graph_unique_pages'][format_date($loop, 'custom', 'H:i')] = $stats->count ? $stats->count : 0; |
| 120 |
|
$loop += 3600; |
| 121 |
|
} |
| 122 |
|
// Number of unique visitors |
| 123 |
|
$loop = $start; |
| 124 |
|
while ($loop < $stop) { |
| 125 |
|
$stats = db_fetch_object(db_query('SELECT COUNT(DISTINCT(hostname)) AS count FROM {accesslog} WHERE timestamp > %d AND timestamp <= %d', $loop, $loop + 3600)); |
| 126 |
|
$data['graph_visitors'][format_date($loop, 'custom', 'H:i')] = $stats->count ? $stats->count : 0; |
| 127 |
|
$loop += 3600; |
| 128 |
|
} |
| 129 |
|
} |