| 1 |
<?php
|
| 2 |
// $Id: graphstat_statistics.inc,v 1.0 2007/06/18 17:04:10 profix898 Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_graphstat().
|
| 6 |
*/
|
| 7 |
function graphstat_graphstat() {
|
| 8 |
$graphs = graphstat_statistics_history();
|
| 9 |
$graphs = array_merge($graphs, graphstat_statistics_nodes());
|
| 10 |
$graphs = array_merge($graphs, graphstat_statistics_user_activity());
|
| 11 |
// Daily statistics are only available if 'access_log' is enabled
|
| 12 |
if (variable_get('statistics_enable_access_log', 0)) {
|
| 13 |
$graphs = array_merge($graphs, graphstat_statistics_daily());
|
| 14 |
}
|
| 15 |
|
| 16 |
return $graphs;
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Function graphstat_statistics_nodes().
|
| 21 |
*/
|
| 22 |
function graphstat_statistics_nodes() {
|
| 23 |
$graphs = array();
|
| 24 |
|
| 25 |
// Node type distribution
|
| 26 |
$node_types = array();
|
| 27 |
$node_types_nodes = array('');
|
| 28 |
$result = db_query("SELECT type, COUNT(nid) AS nodes FROM {node} WHERE nid != 0 GROUP BY type ORDER BY nodes DESC", $_SERVER['HTTP_HOST']);
|
| 29 |
while ($object = db_fetch_object($result)) {
|
| 30 |
$node_types[] = db_result(db_query("SELECT name FROM {node_type} WHERE type = '%s'", $object->type));
|
| 31 |
$node_types_nodes[] = $object->nodes;
|
| 32 |
}
|
| 33 |
|
| 34 |
// Node details (total, published, queued) of the last two weeks
|
| 35 |
$start_date = time() - (60*60*24*14);
|
| 36 |
$nodes_details_total = graphstat_statistics_tablehistory('node', 'nid', '', 14, $start_date);
|
| 37 |
$nodes_details_published = graphstat_statistics_tablehistory('node', 'nid', ' AND status=1', 14, $start_date);
|
| 38 |
$nodes_details_queued = graphstat_statistics_tablehistory('node', 'nid', ' AND moderate=1', 14, $start_date);
|
| 39 |
$nodes_details_all = array();
|
| 40 |
foreach (array_keys($nodes_details_total) as $date) {
|
| 41 |
$nodes_details_all[] = array(
|
| 42 |
$date,
|
| 43 |
$nodes_details_total[$date],
|
| 44 |
$nodes_details_published[$date],
|
| 45 |
$nodes_details_queued[$date]
|
| 46 |
);
|
| 47 |
}
|
| 48 |
|
| 49 |
$graphs['nodes'] = array(
|
| 50 |
'title' => t('Nodes'),
|
| 51 |
'graph_node_types' => array(
|
| 52 |
'type' => 'pie',
|
| 53 |
'title' => t('Node Type Distribution'),
|
| 54 |
'legend' => $node_types,
|
| 55 |
'data' => array($node_types_nodes),
|
| 56 |
'description' => t('Number of nodes for each node type.')
|
| 57 |
),
|
| 58 |
'graph_nodes_details' => array(
|
| 59 |
'type' => 'bars',
|
| 60 |
'title' => t('Number of Nodes (last 2 weeks)'),
|
| 61 |
'xlabel' => t('Date (MM/DD/YYYY)'),
|
| 62 |
'ylabel' => t('# Nodes'),
|
| 63 |
'legend' => array(t('total'), t('published'), t('queued')),
|
| 64 |
'data' => $nodes_details_all,
|
| 65 |
'description' => t('Number of total/published/queued nodes in the last two weeks.')
|
| 66 |
)
|
| 67 |
);
|
| 68 |
|
| 69 |
return $graphs;
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Function graphstat_statistics_history().
|
| 74 |
*/
|
| 75 |
function graphstat_statistics_history() {
|
| 76 |
$graphs = array();
|
| 77 |
$graphs['history'] = array(
|
| 78 |
'title' => t('History'),
|
| 79 |
'graph_nodes' => array(
|
| 80 |
'title' => t('Number of Nodes'),
|
| 81 |
'xlabel' => t('Date (MM/DD/YYYY)'),
|
| 82 |
'ylabel' => t('# Nodes'),
|
| 83 |
'data' => graphstat_statistics_tablehistory()
|
| 84 |
),
|
| 85 |
'graph_users' => array(
|
| 86 |
'title' => t('Number of Users'),
|
| 87 |
'xlabel' => t('Date (MM/DD/YYYY)'),
|
| 88 |
'ylabel' => t('# Users'),
|
| 89 |
'data' => graphstat_statistics_tablehistory('users', 'uid')
|
| 90 |
)
|
| 91 |
);
|
| 92 |
|
| 93 |
if (module_exists('comment')) {
|
| 94 |
$graphs['history']['graph_comments'] = array(
|
| 95 |
'title' => t('Number of Comments'),
|
| 96 |
'xlabel' => t('Date (MM/DD/YYYY)'),
|
| 97 |
'ylabel' => t('# Comments'),
|
| 98 |
'data' => graphstat_statistics_tablehistory('comments', 'cid')
|
| 99 |
);
|
| 100 |
}
|
| 101 |
|
| 102 |
return $graphs;
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Function graphstat_statistics_user_activity().
|
| 107 |
*/
|
| 108 |
function graphstat_statistics_user_activity() {
|
| 109 |
$graphs = array();
|
| 110 |
|
| 111 |
// Most active users (page hits)
|
| 112 |
if (variable_get('statistics_enable_access_log', 0)) {
|
| 113 |
$top_users_hits = array('');
|
| 114 |
$top_users_hits_legend = array();
|
| 115 |
$result = db_query("SELECT uid, COUNT(DISTINCT(url)) AS hits FROM {accesslog} WHERE url <> '' GROUP BY uid ORDER BY hits DESC LIMIT 10", $_SERVER['HTTP_HOST']);
|
| 116 |
while ($object = db_fetch_object($result)) {
|
| 117 |
$top_users_hits_legend[] = graphstat_statistics_uid2username($object->uid);
|
| 118 |
$top_users_hits[] = $object->hits;
|
| 119 |
}
|
| 120 |
$total_hits = max(array_sum($top_users_hits), 1);
|
| 121 |
foreach ($top_users_hits as $pos => $hits) {
|
| 122 |
$top_users_hits[$pos] = round($hits / $total_hits * 100, 1);
|
| 123 |
}
|
| 124 |
}
|
| 125 |
|
| 126 |
// Most active users (number of nodes)
|
| 127 |
$top_users_nodes = array('');
|
| 128 |
$top_users_nodes_legend = array();
|
| 129 |
$result = db_query("SELECT uid, COUNT(nid) AS nodes FROM {node} WHERE nid != 0 GROUP BY uid ORDER BY nodes DESC LIMIT 10", $_SERVER['HTTP_HOST']);
|
| 130 |
while ($object = db_fetch_object($result)) {
|
| 131 |
$top_users_nodes_legend[] = graphstat_statistics_uid2username($object->uid);
|
| 132 |
$top_users_nodes[] = $object->nodes;
|
| 133 |
}
|
| 134 |
$total_nodes = max(array_sum($top_users_nodes), 1);
|
| 135 |
foreach ($top_users_nodes as $pos => $nodes) {
|
| 136 |
$top_users_nodes[$pos] = round($nodes / $total_nodes * 100, 1);
|
| 137 |
}
|
| 138 |
|
| 139 |
$graphs['activity'] = array(
|
| 140 |
'title' => t('User Activity'),
|
| 141 |
'graph_user_hits' => array(
|
| 142 |
'type' => 'pie',
|
| 143 |
'title' => t('Most active users (page hits)'),
|
| 144 |
'legend' => isset($top_users_hits_legend) ? $top_users_hits_legend : NULL,
|
| 145 |
'data' => isset($top_users_hits) ? array($top_users_hits) : NULL
|
| 146 |
),
|
| 147 |
'graph_user_nodes' => array(
|
| 148 |
'type' => 'pie',
|
| 149 |
'title' => t('Most active users (number of nodes)'),
|
| 150 |
'legend' => $top_users_nodes_legend,
|
| 151 |
'data' => array($top_users_nodes)
|
| 152 |
)
|
| 153 |
);
|
| 154 |
|
| 155 |
// Most active users (number of comments)
|
| 156 |
if (module_exists('comment')) {
|
| 157 |
$top_users_comments = array('');
|
| 158 |
$top_users_comments_legend = array();
|
| 159 |
$result = db_query("SELECT uid, COUNT(cid) AS comments FROM {comments} WHERE cid != 0 GROUP BY uid ORDER BY comments DESC LIMIT 10", $_SERVER['HTTP_HOST']);
|
| 160 |
while ($object = db_fetch_object($result)) {
|
| 161 |
$top_users_comments_legend[] = graphstat_statistics_uid2username($object->uid);
|
| 162 |
$top_users_comments[] = $object->comments;
|
| 163 |
}
|
| 164 |
$total_comments = max(array_sum($top_users_comments), 1);
|
| 165 |
foreach ($top_users_comments as $pos => $comments) {
|
| 166 |
$top_users_comments[$pos] = round($comments / $total_comments * 100, 1);
|
| 167 |
}
|
| 168 |
|
| 169 |
$graphs['activity']['graph_user_comments'] = array(
|
| 170 |
'type' => 'pie',
|
| 171 |
'title' => t('Most active users (number of comments)'),
|
| 172 |
'legend' => $top_users_comments_legend,
|
| 173 |
'data' => (count($top_users_comments) > 1) ? array($top_users_comments) : NULL
|
| 174 |
);
|
| 175 |
}
|
| 176 |
|
| 177 |
return $graphs;
|
| 178 |
}
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Function graphstat_statistics_daily().
|
| 182 |
*/
|
| 183 |
function graphstat_statistics_daily() {
|
| 184 |
global $user;
|
| 185 |
if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
|
| 186 |
$timezone = $user->timezone;
|
| 187 |
}
|
| 188 |
else {
|
| 189 |
$timezone = variable_get('date_default_timezone', 0);
|
| 190 |
}
|
| 191 |
// Get oldest record and current date
|
| 192 |
$min = db_result(db_query('SELECT MIN(timestamp) FROM {accesslog}'));
|
| 193 |
if ($min) {
|
| 194 |
$now = time();
|
| 195 |
// Figure out which local dates correspond with these timestamps - can be different between timezones
|
| 196 |
$min_date = format_date($min, 'custom', 'm/d/Y');
|
| 197 |
$now_date = format_date($now, 'custom', 'm/d/Y');
|
| 198 |
// Figure out the (UNIX Epoch) timestamp that corresponds to when the first day began locally
|
| 199 |
// Figure out the (UNIX Epoch) timestamp that corresponds to when today ends locally
|
| 200 |
$min_date_frag = explode('/', $min_date);
|
| 201 |
$now_date_frag = explode('/', $now_date);
|
| 202 |
$gm_timestamp_min = gmmktime(0, 0, 0, $min_date_frag[0], $min_date_frag[1], $min_date_frag[2]);
|
| 203 |
$gm_timestamp_now = gmmktime(23, 59, 59, $now_date_frag[0], $now_date_frag[1], $now_date_frag[2]);
|
| 204 |
if ($timezone > 0) {
|
| 205 |
$local_timestamp_min = $gm_timestamp_min - $timezone;
|
| 206 |
$local_timestamp_now = $gm_timestamp_now - $timezone;
|
| 207 |
}
|
| 208 |
elseif ($timezone < 0) {
|
| 209 |
$local_timestamp_min = $gm_timestamp_min + abs($timezone);
|
| 210 |
$local_timestamp_now = $gm_timestamp_now + abs($timezone);
|
| 211 |
}
|
| 212 |
else {
|
| 213 |
$local_timestamp_min = $gm_timestamp_min;
|
| 214 |
$local_timestamp_now = $gm_timestamp_now;
|
| 215 |
}
|
| 216 |
// Build the filter options array with all dates
|
| 217 |
$options = array('<'. t('Select a date') .'>');
|
| 218 |
while ($local_timestamp_min < $local_timestamp_now) {
|
| 219 |
$options[$local_timestamp_min] = format_date($local_timestamp_min, 'custom', 'm/d/Y');
|
| 220 |
$local_timestamp_min = $local_timestamp_min + 86400;
|
| 221 |
}
|
| 222 |
}
|
| 223 |
else {
|
| 224 |
$options = array(NULL => t('No items available'));
|
| 225 |
}
|
| 226 |
|
| 227 |
$graphs = array();
|
| 228 |
$graphs['daily'] = array(
|
| 229 |
'title' => t('Daily'),
|
| 230 |
'filter' => array(
|
| 231 |
'options' => $options,
|
| 232 |
'callback' => 'graphstat_statistics_daily_filter'
|
| 233 |
),
|
| 234 |
// The empty data arrays will be filled in the filter callback
|
| 235 |
'graph_page_generation' => array(
|
| 236 |
'title' => t('Average page generation time'),
|
| 237 |
'xlabel' => t('Time (hh:mm)'),
|
| 238 |
'ylabel' => t('Page generation / ms'),
|
| 239 |
'legend' => array(t('avg'), t('avg + stddev'), t('avg - stddev')),
|
| 240 |
'data' => array(),
|
| 241 |
'description' => t('Average (avg) page generation time +/- standard deviation (stddev).')
|
| 242 |
),
|
| 243 |
'graph_pages' => array(
|
| 244 |
'title' => t('Number of pages served (total)'),
|
| 245 |
'xlabel' => t('Time (hh:mm)'),
|
| 246 |
'ylabel' => t('# Pages'),
|
| 247 |
'data' => array()
|
| 248 |
),
|
| 249 |
'graph_unique_pages' => array(
|
| 250 |
'title' => t('Number of unique pages served'),
|
| 251 |
'xlabel' => t('Time (hh:mm)'),
|
| 252 |
'ylabel' => t('# Pages'),
|
| 253 |
'data' => array()
|
| 254 |
),
|
| 255 |
'graph_visitors' => array(
|
| 256 |
'title' => t('Number of unique visitors'),
|
| 257 |
'xlabel' => t('Time (hh:mm)'),
|
| 258 |
'ylabel' => t('# Visitors'),
|
| 259 |
'data' => array()
|
| 260 |
)
|
| 261 |
);
|
| 262 |
|
| 263 |
return $graphs;
|
| 264 |
}
|
| 265 |
|
| 266 |
/**
|
| 267 |
* Function graphstat_statistics_daily_filter().
|
| 268 |
*/
|
| 269 |
function graphstat_statistics_daily_filter(&$graphs, $start) {
|
| 270 |
if (!$start) {
|
| 271 |
return;
|
| 272 |
}
|
| 273 |
$stop = $start + 86400;
|
| 274 |
// Average page generation time
|
| 275 |
$loop = $start;
|
| 276 |
while ($loop < $stop) {
|
| 277 |
$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));
|
| 278 |
$avg = $stats->avg ? round($stats->avg , 0) : 0;
|
| 279 |
$dev = $stats->dev ? round($stats->dev, 0) : 0;
|
| 280 |
$graphs['graph_page_generation']['data'][] = array(format_date($loop, 'custom', 'H:i'), $avg, $avg + $dev, max($avg - $dev, 0));
|
| 281 |
$loop += 3600;
|
| 282 |
}
|
| 283 |
// Number of pages served (total)
|
| 284 |
$loop = $start;
|
| 285 |
while ($loop < $stop) {
|
| 286 |
$stats = db_fetch_object(db_query('SELECT COUNT(aid) AS count FROM {accesslog} WHERE timestamp > %d AND timestamp <= %d', $loop, $loop + 3600));
|
| 287 |
$graphs['graph_pages']['data'][format_date($loop, 'custom', 'H:i')] = $stats->count ? $stats->count : 0;
|
| 288 |
$loop += 3600;
|
| 289 |
}
|
| 290 |
// Number of unique pages served
|
| 291 |
$loop = $start;
|
| 292 |
while ($loop < $stop) {
|
| 293 |
$stats = db_fetch_object(db_query('SELECT COUNT(DISTINCT(path)) AS count FROM {accesslog} WHERE timestamp > %d AND timestamp <= %d', $loop, $loop + 3600));
|
| 294 |
$graphs['graph_unique_pages']['data'][format_date($loop, 'custom', 'H:i')] = $stats->count ? $stats->count : 0;
|
| 295 |
$loop += 3600;
|
| 296 |
}
|
| 297 |
// Number of unique visitors
|
| 298 |
$loop = $start;
|
| 299 |
while ($loop < $stop) {
|
| 300 |
$stats = db_fetch_object(db_query('SELECT COUNT(DISTINCT(hostname)) AS count FROM {accesslog} WHERE timestamp > %d AND timestamp <= %d', $loop, $loop + 3600));
|
| 301 |
$graphs['graph_visitors']['data'][format_date($loop, 'custom', 'H:i')] = $stats->count ? $stats->count : 0;
|
| 302 |
$loop += 3600;
|
| 303 |
}
|
| 304 |
}
|
| 305 |
|
| 306 |
/**
|
| 307 |
* Function graphstat_statistics_tablehistory().
|
| 308 |
*/
|
| 309 |
function graphstat_statistics_tablehistory($table = 'node', $id = 'nid', $qfragment = '', $points = 0, $timestamp = 0) {
|
| 310 |
$data = array();
|
| 311 |
|
| 312 |
$key = in_array($table, array('node', 'users')) ? 'created' : 'timestamp';
|
| 313 |
$timestamp = $timestamp ? $timestamp : db_result(db_query('SELECT MIN(%s) FROM {%s} WHERE %s != 0', $key, $table, $id));
|
| 314 |
if ($timestamp) {
|
| 315 |
// one point per week (at least 15 points)
|
| 316 |
if (!$points) {
|
| 317 |
$points = max((time() - $timestamp) / (60*60*24*7), 15);
|
| 318 |
while ($points > 30) {
|
| 319 |
$points /= 2;
|
| 320 |
}
|
| 321 |
}
|
| 322 |
$interval = round((time() - $timestamp) / ($points - 1), 0);
|
| 323 |
$time = $timestamp;
|
| 324 |
for ($i = 0; $i < $points; $i++) {
|
| 325 |
$data[format_date($time, 'custom', 'm/d/Y')] = db_result(db_query('SELECT COUNT(DISTINCT(%s)) FROM {%s} WHERE %s <= %d AND %s != 0 %s', $id, $table, $key, $time, $id, $qfragment));
|
| 326 |
$time += $interval;
|
| 327 |
}
|
| 328 |
}
|
| 329 |
|
| 330 |
return $data;
|
| 331 |
}
|
| 332 |
|
| 333 |
/**
|
| 334 |
* Function graphstat_statistics_uid2username().
|
| 335 |
*/
|
| 336 |
function graphstat_statistics_uid2username($uid) {
|
| 337 |
if ($uid) {
|
| 338 |
$user = user_load(array('uid' => $uid));
|
| 339 |
return $user->name;
|
| 340 |
}
|
| 341 |
else {
|
| 342 |
return variable_get('anonymous', t('anonymous'));
|
| 343 |
}
|
| 344 |
}
|