author | Dan |
Sun, 04 Jan 2009 16:42:52 -0500 | |
changeset 45 | 53a50ba4d690 |
parent 43 | ce2e9caf2dfa |
child 44 | 73f74d395f95 |
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 |
/** |
|
43
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
109 |
* Raw data. |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
110 |
* @param string Channel name |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
111 |
* @param int How many minutes, defaults to 60 |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
112 |
* @param int Base time, defaults to right now |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
113 |
* @return array MySQL rows. |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
114 |
*/ |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
115 |
|
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
116 |
function stats_raw_data($channel, $mins = 60, $base = NOW) |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
117 |
{ |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
118 |
$channel = db_escape($channel); |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
119 |
$time_min = $base - ( $mins * 60 ); |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
120 |
$time_max =& $base; |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
121 |
$total = 0; |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
122 |
|
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
123 |
if ( $q = eb_mysql_query("SELECT * FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max ORDER BY message_id ASC;") ) |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
124 |
{ |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
125 |
$userdata = array(); |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
126 |
while ( $row = @mysql_fetch_assoc($q) ) |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
127 |
{ |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
128 |
$userdata[] = $row; |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
129 |
} |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
130 |
return $userdata; |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
131 |
} |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
132 |
return false; |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
133 |
} |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
134 |
|
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
135 |
/** |
15 | 136 |
* Return the time that the stats DB was last updated. |
137 |
* @return int |
|
138 |
*/ |
|
139 |
||
140 |
function stats_last_updated() |
|
141 |
{ |
|
20 | 142 |
// :-D |
143 |
return NOW; |
|
15 | 144 |
} |
145 |
||
146 |