5 * @package EnanoBot |
5 * @package EnanoBot |
6 * @subpackage stats |
6 * @subpackage stats |
7 * @author Dan Fuhry <dan@enanocms.org> |
7 * @author Dan Fuhry <dan@enanocms.org> |
8 */ |
8 */ |
9 |
9 |
10 $stats_merged_data = array('counts' => array(), 'messages' => array()); |
|
11 $stats_data =& $stats_merged_data; |
|
12 |
|
13 define('ENANOBOT_ROOT', dirname(__FILE__)); |
10 define('ENANOBOT_ROOT', dirname(__FILE__)); |
14 define('NOW', time()); |
11 define('NOW', time()); |
|
12 |
|
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 } |
15 |
28 |
16 /** |
29 /** |
17 * Gets the number of messages posted in IRC in the last X minutes. |
30 * Gets the number of messages posted in IRC in the last X minutes. |
18 * @param string Channel |
31 * @param string Channel |
19 * @param int Optional - time period for message count. Defaults to 10 minutes. |
32 * @param int Optional - time period for message count. Defaults to 10 minutes. |
21 * @return int |
34 * @return int |
22 */ |
35 */ |
23 |
36 |
24 function stats_message_count($channel, $mins = 10, $base = NOW) |
37 function stats_message_count($channel, $mins = 10, $base = NOW) |
25 { |
38 { |
26 global $stats_merged_data; |
39 $channel = db_escape($channel); |
27 |
|
28 $time_min = $base - ( $mins * 60 ); |
40 $time_min = $base - ( $mins * 60 ); |
29 $time_max = $base; |
41 $time_max =& $base; |
30 |
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';") ) |
31 if ( !isset($stats_merged_data['messages'][$channel]) ) |
|
32 { |
43 { |
33 return 0; |
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); |
34 } |
51 } |
35 |
52 if ( $q = eb_mysql_query("SELECT COUNT(message_id) FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max;") ) |
36 $count = 0; |
|
37 foreach ( $stats_merged_data['messages'][$channel] as $message ) |
|
38 { |
53 { |
39 if ( $message['time'] >= $time_min && $message['time'] <= $time_max ) |
54 $row = mysql_fetch_row($q); |
|
55 $count = $row[0]; |
|
56 mysql_free_result($q); |
|
57 // avoid caching future queries |
|
58 if ( $base <= NOW ) |
40 { |
59 { |
41 $count++; |
60 eb_mysql_query("INSERT INTO stats_count_cache(channel, time_min, time_max, message_count) VALUES('$channel', $time_min, $time_max, $count);"); |
42 } |
61 } |
|
62 return $count; |
43 } |
63 } |
44 |
64 return false; |
45 return $count; |
|
46 } |
65 } |
47 |
66 |
48 /** |
67 /** |
49 * Gets the percentages as to who's posted the most messages in the last X minutes. |
68 * Gets the percentages as to who's posted the most messages in the last X minutes. |
50 * @param string Channel name |
69 * @param string Channel name |
53 * @return array Associative, with floats. |
72 * @return array Associative, with floats. |
54 */ |
73 */ |
55 |
74 |
56 function stats_activity_percent($channel, $mins = 10, $base = NOW) |
75 function stats_activity_percent($channel, $mins = 10, $base = NOW) |
57 { |
76 { |
58 global $stats_merged_data; |
77 $channel = db_escape($channel); |
59 if ( !($total = stats_message_count($channel, $mins, $base)) ) |
78 $time_min = $base - ( $mins * 60 ); |
|
79 $time_max =& $base; |
|
80 |
|
81 if ( $q = eb_mysql_query("SELECT nick FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max;") ) |
60 { |
82 { |
61 return array(); |
83 $userdata = array(); |
|
84 while ( $row = @mysql_fetch_assoc($q) ) |
|
85 { |
|
86 $total++; |
|
87 if ( isset($userdata[ $row['nick'] ]) ) |
|
88 { |
|
89 $userdata[ $row['nick'] ]++; |
|
90 } |
|
91 else |
|
92 { |
|
93 $userdata[ $row['nick'] ] = 1; |
|
94 } |
|
95 } |
|
96 foreach ( $userdata as &$val ) |
|
97 { |
|
98 $val = $val / $total; |
|
99 } |
|
100 mysql_free_result($q); |
|
101 arsort($userdata); |
|
102 return $userdata; |
62 } |
103 } |
63 $results = array(); |
104 return false; |
64 $usercounts = array(); |
|
65 $time_min = $base - ( $mins * 60 ); |
|
66 $time_max = $base; |
|
67 foreach ( $stats_merged_data['messages'][$channel] as $message ) |
|
68 { |
|
69 if ( $message['time'] >= $time_min && $message['time'] <= $time_max ) |
|
70 { |
|
71 if ( !isset($usercounts[$message['nick']]) ) |
|
72 $usercounts[$message['nick']] = 0; |
|
73 $usercounts[$message['nick']]++; |
|
74 } |
|
75 } |
|
76 foreach ( $usercounts as $nick => $count ) |
|
77 { |
|
78 $results[$nick] = $count / $total; |
|
79 } |
|
80 arsort($results); |
|
81 return $results; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Loads X days of statistics, minimum. |
|
86 * @param int Days to load, default is 1 |
|
87 */ |
|
88 |
|
89 function load_stats_data($days = 1) |
|
90 { |
|
91 $days++; |
|
92 for ( $i = 0; $i < $days; $i++ ) |
|
93 { |
|
94 $day = NOW - ( $i * 86400 ); |
|
95 $day = gmdate('Ymd', $day); |
|
96 if ( file_exists(ENANOBOT_ROOT . "/stats/stats-data-$day.php") ) |
|
97 { |
|
98 require(ENANOBOT_ROOT . "/stats/stats-data-$day.php"); |
|
99 stats_merge($stats_data); |
|
100 } |
|
101 } |
|
102 } |
105 } |
103 |
106 |
104 /** |
107 /** |
105 * Return the time that the stats DB was last updated. |
108 * Return the time that the stats DB was last updated. |
106 * @return int |
109 * @return int |
107 */ |
110 */ |
108 |
111 |
109 function stats_last_updated() |
112 function stats_last_updated() |
110 { |
113 { |
111 $day = gmdate('Ymd'); |
114 // :-D |
112 $file = ENANOBOT_ROOT . "/stats/stats-data-$day.php"; |
115 return NOW; |
113 return ( file_exists($file) ) ? filemtime($file) : 0; |
|
114 } |
116 } |
115 |
117 |
116 /** |
|
117 * Merges a newly loaded stats array with the current cache in RAM. |
|
118 * @param array Data to merge |
|
119 * @access private |
|
120 */ |
|
121 |
118 |
122 function stats_merge($data) |
|
123 { |
|
124 global $stats_merged_data; |
|
125 if ( isset($data['counts']) ) |
|
126 { |
|
127 foreach ( $data['counts'] as $channel => $chaninfo ) |
|
128 { |
|
129 if ( isset($stats_merged_data['counts'][$channel]) ) |
|
130 { |
|
131 foreach ( $stats_merged_data['counts'][$channel] as $key => &$value ) |
|
132 { |
|
133 if ( is_int($value) ) |
|
134 { |
|
135 $value = max($value, $chaninfo[$key]); |
|
136 } |
|
137 else if ( is_array($value) ) |
|
138 { |
|
139 $value = array_merge($value, $chaninfo[$key]); |
|
140 } |
|
141 } |
|
142 } |
|
143 else |
|
144 { |
|
145 $stats_merged_data['counts'][$channel] = $chaninfo; |
|
146 } |
|
147 } |
|
148 } |
|
149 foreach ( $data['messages'] as $channel => $chandata ) |
|
150 { |
|
151 if ( isset($stats_merged_data['messages'][$channel]) ) |
|
152 { |
|
153 foreach ( $chandata as $message ) |
|
154 { |
|
155 $stats_merged_data['messages'][$channel][] = $message; |
|
156 } |
|
157 } |
|
158 else |
|
159 { |
|
160 $stats_merged_data['messages'][$channel] = $chandata; |
|
161 } |
|
162 } |
|
163 } |
|
164 |
|
165 load_stats_data(); |
|
166 |
|