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 if ( !isset($GLOBALS['stats_data']) ) |
10 $stats_merged_data = array('counts' => array(), 'messages' => array()); |
11 { |
11 $stats_data =& $stats_merged_data; |
12 require(dirname(__FILE__) . '/stats-data.php'); |
|
13 $data =& $stats_data; |
|
14 } |
|
15 |
12 |
|
13 define('ENANOBOT_ROOT', dirname(__FILE__)); |
16 define('NOW', time()); |
14 define('NOW', time()); |
17 |
15 |
18 /** |
16 /** |
19 * Gets the number of messages posted in IRC in the last X minutes. |
17 * Gets the number of messages posted in IRC in the last X minutes. |
20 * @param string Channel |
18 * @param string Channel |
23 * @return int |
21 * @return int |
24 */ |
22 */ |
25 |
23 |
26 function stats_message_count($channel, $mins = 10, $base = NOW) |
24 function stats_message_count($channel, $mins = 10, $base = NOW) |
27 { |
25 { |
28 global $data; |
26 global $stats_merged_data; |
29 |
27 |
30 $time_min = $base - ( $mins * 60 ); |
28 $time_min = $base - ( $mins * 60 ); |
31 $time_max = $base; |
29 $time_max = $base; |
32 |
30 |
33 if ( !isset($data['messages'][$channel]) ) |
31 if ( !isset($stats_merged_data['messages'][$channel]) ) |
34 { |
32 { |
35 return 0; |
33 return 0; |
36 } |
34 } |
37 |
35 |
38 $count = 0; |
36 $count = 0; |
39 foreach ( $data['messages'][$channel] as $message ) |
37 foreach ( $stats_merged_data['messages'][$channel] as $message ) |
40 { |
38 { |
41 if ( $message['time'] >= $time_min && $message['time'] <= $time_max ) |
39 if ( $message['time'] >= $time_min && $message['time'] <= $time_max ) |
42 { |
40 { |
43 $count++; |
41 $count++; |
44 } |
42 } |
55 * @return array Associative, with floats. |
53 * @return array Associative, with floats. |
56 */ |
54 */ |
57 |
55 |
58 function stats_activity_percent($channel, $mins = 10, $base = NOW) |
56 function stats_activity_percent($channel, $mins = 10, $base = NOW) |
59 { |
57 { |
60 global $data; |
58 global $stats_merged_data; |
61 if ( !($total = stats_message_count($channel, $mins, $base)) ) |
59 if ( !($total = stats_message_count($channel, $mins, $base)) ) |
62 { |
60 { |
63 return array(); |
61 return array(); |
64 } |
62 } |
65 $results = array(); |
63 $results = array(); |
66 $usercounts = array(); |
64 $usercounts = array(); |
67 $time_min = $base - ( $mins * 60 ); |
65 $time_min = $base - ( $mins * 60 ); |
68 $time_max = $base; |
66 $time_max = $base; |
69 foreach ( $data['messages'][$channel] as $message ) |
67 foreach ( $stats_merged_data['messages'][$channel] as $message ) |
70 { |
68 { |
71 if ( $message['time'] >= $time_min && $message['time'] <= $time_max ) |
69 if ( $message['time'] >= $time_min && $message['time'] <= $time_max ) |
72 { |
70 { |
73 if ( !isset($usercounts[$message['nick']]) ) |
71 if ( !isset($usercounts[$message['nick']]) ) |
74 $usercounts[$message['nick']] = 0; |
72 $usercounts[$message['nick']] = 0; |
80 $results[$nick] = $count / $total; |
78 $results[$nick] = $count / $total; |
81 } |
79 } |
82 arsort($results); |
80 arsort($results); |
83 return $results; |
81 return $results; |
84 } |
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 } |
|
103 |
|
104 /** |
|
105 * Return the time that the stats DB was last updated. |
|
106 * @return int |
|
107 */ |
|
108 |
|
109 function stats_last_updated() |
|
110 { |
|
111 $day = gmdate('Ymd'); |
|
112 $file = ENANOBOT_ROOT . "/stats/stats-data-$day.php"; |
|
113 return ( file_exists($file) ) ? filemtime($file) : 0; |
|
114 } |
|
115 |
|
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 |
|
122 function stats_merge($data) |
|
123 { |
|
124 global $stats_merged_data; |
|
125 foreach ( $data['counts'] as $channel => $chaninfo ) |
|
126 { |
|
127 if ( isset($stats_merged_data['counts'][$channel]) ) |
|
128 { |
|
129 foreach ( $stats_merged_data['counts'][$channel] as $key => &$value ) |
|
130 { |
|
131 if ( is_int($value) ) |
|
132 { |
|
133 $value = max($value, $chaninfo[$key]); |
|
134 } |
|
135 else if ( is_array($value) ) |
|
136 { |
|
137 $value = array_merge($value, $chaninfo[$key]); |
|
138 } |
|
139 } |
|
140 } |
|
141 else |
|
142 { |
|
143 $stats_merged_data['counts'][$channel] = $chaninfo; |
|
144 } |
|
145 } |
|
146 foreach ( $data['messages'] as $channel => $chandata ) |
|
147 { |
|
148 if ( isset($stats_merged_data['messages'][$channel]) ) |
|
149 { |
|
150 foreach ( $chandata as $message ) |
|
151 { |
|
152 $stats_merged_data['messages'][$channel][] = $message; |
|
153 } |
|
154 } |
|
155 else |
|
156 { |
|
157 $stats_merged_data['messages'][$channel] = $chandata; |
|
158 } |
|
159 } |
|
160 } |
|
161 |
|
162 load_stats_data(); |
|
163 |