author | Dan |
Sat, 03 Jan 2009 17:09:16 -0500 | |
changeset 41 | 1d854f22ac5a |
parent 36 | a7d884914a74 |
child 43 | ce2e9caf2dfa |
permissions | -rw-r--r-- |
8 | 1 |
<?php |
2 |
||
3 |
/** |
|
4 |
* Frontend for statistics data. Handles fetching and calculating data from raw statistics stored in stats-data.php. |
|
5 |
* @package EnanoBot |
|
6 |
* @subpackage stats |
|
7 |
* @author Dan Fuhry <dan@enanocms.org> |
|
8 |
*/ |
|
9 |
||
15 | 10 |
define('ENANOBOT_ROOT', dirname(__FILE__)); |
8 | 11 |
define('NOW', time()); |
12 |
||
20 | 13 |
require(ENANOBOT_ROOT . '/config.php'); |
14 |
require(ENANOBOT_ROOT . '/hooks.php'); |
|
15 |
require(ENANOBOT_ROOT . '/database.php'); |
|
16 |
||
17 |
mysql_reconnect(); |
|
18 |
||
19 |
/** |
|
20 |
* Gets ths list of channels. |
|
21 |
* @return array |
|
22 |
*/ |
|
23 |
||
24 |
function stats_channel_list() |
|
25 |
{ |
|
26 |
return $GLOBALS['channels']; |
|
27 |
} |
|
28 |
||
8 | 29 |
/** |
30 |
* Gets the number of messages posted in IRC in the last X minutes. |
|
31 |
* @param string Channel |
|
32 |
* @param int Optional - time period for message count. Defaults to 10 minutes. |
|
33 |
* @param int Optional - Base time, defaults to right now |
|
34 |
* @return int |
|
35 |
*/ |
|
36 |
||
37 |
function stats_message_count($channel, $mins = 10, $base = NOW) |
|
38 |
{ |
|
20 | 39 |
$channel = db_escape($channel); |
8 | 40 |
$time_min = $base - ( $mins * 60 ); |
20 | 41 |
$time_max =& $base; |
42 |
if ( $q = eb_mysql_query("SELECT message_count FROM stats_count_cache WHERE time_min = $time_min AND time_max = $time_max AND channel = '$channel';") ) |
|
8 | 43 |
{ |
20 | 44 |
if ( mysql_num_rows($q) > 0 ) |
45 |
{ |
|
46 |
$row = mysql_fetch_assoc($q); |
|
47 |
mysql_free_result($q); |
|
48 |
return intval($row['message_count']); |
|
49 |
} |
|
50 |
mysql_free_result($q); |
|
8 | 51 |
} |
20 | 52 |
if ( $q = eb_mysql_query("SELECT COUNT(message_id) FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max;") ) |
8 | 53 |
{ |
20 | 54 |
$row = mysql_fetch_row($q); |
55 |
$count = $row[0]; |
|
56 |
mysql_free_result($q); |
|
57 |
// avoid caching future queries |
|
58 |
if ( $base <= NOW ) |
|
8 | 59 |
{ |
20 | 60 |
eb_mysql_query("INSERT INTO stats_count_cache(channel, time_min, time_max, message_count) VALUES('$channel', $time_min, $time_max, $count);"); |
8 | 61 |
} |
36
a7d884914a74
Added extern script to allow exporting some stats through a JSON or XML API. WiP.
Dan
parents:
28
diff
changeset
|
62 |
return intval($count); |
8 | 63 |
} |
20 | 64 |
return false; |
8 | 65 |
} |
66 |
||
67 |
/** |
|
68 |
* Gets the percentages as to who's posted the most messages in the last X minutes. |
|
69 |
* @param string Channel name |
|
70 |
* @param int Optional - How many minutes, defaults to 10 |
|
71 |
* @param int Optional - Base time, defaults to right now |
|
72 |
* @return array Associative, with floats. |
|
73 |
*/ |
|
74 |
||
75 |
function stats_activity_percent($channel, $mins = 10, $base = NOW) |
|
76 |
{ |
|
20 | 77 |
$channel = db_escape($channel); |
8 | 78 |
$time_min = $base - ( $mins * 60 ); |
20 | 79 |
$time_max =& $base; |
28 | 80 |
$total = 0; |
20 | 81 |
|
82 |
if ( $q = eb_mysql_query("SELECT nick FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max;") ) |
|
8 | 83 |
{ |
20 | 84 |
$userdata = array(); |
85 |
while ( $row = @mysql_fetch_assoc($q) ) |
|
8 | 86 |
{ |
20 | 87 |
$total++; |
88 |
if ( isset($userdata[ $row['nick'] ]) ) |
|
89 |
{ |
|
90 |
$userdata[ $row['nick'] ]++; |
|
91 |
} |
|
92 |
else |
|
93 |
{ |
|
94 |
$userdata[ $row['nick'] ] = 1; |
|
95 |
} |
|
8 | 96 |
} |
20 | 97 |
foreach ( $userdata as &$val ) |
98 |
{ |
|
99 |
$val = $val / $total; |
|
100 |
} |
|
101 |
mysql_free_result($q); |
|
102 |
arsort($userdata); |
|
103 |
return $userdata; |
|
8 | 104 |
} |
20 | 105 |
return false; |
15 | 106 |
} |
107 |
||
108 |
/** |
|
109 |
* Return the time that the stats DB was last updated. |
|
110 |
* @return int |
|
111 |
*/ |
|
112 |
||
113 |
function stats_last_updated() |
|
114 |
{ |
|
20 | 115 |
// :-D |
116 |
return NOW; |
|
15 | 117 |
} |
118 |
||
119 |