author | Dan |
Mon, 05 Jan 2009 22:30:26 -0500 | |
changeset 46 | 186507f67064 |
parent 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 |
||
44
73f74d395f95
Added theme support. Oh yeah, you can customize graphs using graph_{line,bar}.def.
Dan
parents:
43
diff
changeset
|
17 |
$webtheme = ( isset($webtheme) && is_dir(ENANOBOT_ROOT . "/htdocs/themes/$webtheme") ) ? $webtheme : 'generic'; |
73f74d395f95
Added theme support. Oh yeah, you can customize graphs using graph_{line,bar}.def.
Dan
parents:
43
diff
changeset
|
18 |
|
20 | 19 |
mysql_reconnect(); |
20 |
||
21 |
/** |
|
22 |
* Gets ths list of channels. |
|
23 |
* @return array |
|
24 |
*/ |
|
25 |
||
26 |
function stats_channel_list() |
|
27 |
{ |
|
28 |
return $GLOBALS['channels']; |
|
29 |
} |
|
30 |
||
8 | 31 |
/** |
32 |
* Gets the number of messages posted in IRC in the last X minutes. |
|
33 |
* @param string Channel |
|
34 |
* @param int Optional - time period for message count. Defaults to 10 minutes. |
|
35 |
* @param int Optional - Base time, defaults to right now |
|
36 |
* @return int |
|
37 |
*/ |
|
38 |
||
39 |
function stats_message_count($channel, $mins = 10, $base = NOW) |
|
40 |
{ |
|
20 | 41 |
$channel = db_escape($channel); |
8 | 42 |
$time_min = $base - ( $mins * 60 ); |
20 | 43 |
$time_max =& $base; |
44 |
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 | 45 |
{ |
20 | 46 |
if ( mysql_num_rows($q) > 0 ) |
47 |
{ |
|
48 |
$row = mysql_fetch_assoc($q); |
|
49 |
mysql_free_result($q); |
|
50 |
return intval($row['message_count']); |
|
51 |
} |
|
52 |
mysql_free_result($q); |
|
8 | 53 |
} |
20 | 54 |
if ( $q = eb_mysql_query("SELECT COUNT(message_id) FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max;") ) |
8 | 55 |
{ |
20 | 56 |
$row = mysql_fetch_row($q); |
57 |
$count = $row[0]; |
|
58 |
mysql_free_result($q); |
|
59 |
// avoid caching future queries |
|
60 |
if ( $base <= NOW ) |
|
8 | 61 |
{ |
20 | 62 |
eb_mysql_query("INSERT INTO stats_count_cache(channel, time_min, time_max, message_count) VALUES('$channel', $time_min, $time_max, $count);"); |
8 | 63 |
} |
36
a7d884914a74
Added extern script to allow exporting some stats through a JSON or XML API. WiP.
Dan
parents:
28
diff
changeset
|
64 |
return intval($count); |
8 | 65 |
} |
20 | 66 |
return false; |
8 | 67 |
} |
68 |
||
69 |
/** |
|
70 |
* Gets the percentages as to who's posted the most messages in the last X minutes. |
|
71 |
* @param string Channel name |
|
72 |
* @param int Optional - How many minutes, defaults to 10 |
|
73 |
* @param int Optional - Base time, defaults to right now |
|
74 |
* @return array Associative, with floats. |
|
75 |
*/ |
|
76 |
||
77 |
function stats_activity_percent($channel, $mins = 10, $base = NOW) |
|
78 |
{ |
|
20 | 79 |
$channel = db_escape($channel); |
8 | 80 |
$time_min = $base - ( $mins * 60 ); |
20 | 81 |
$time_max =& $base; |
28 | 82 |
$total = 0; |
20 | 83 |
|
84 |
if ( $q = eb_mysql_query("SELECT nick FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max;") ) |
|
8 | 85 |
{ |
20 | 86 |
$userdata = array(); |
87 |
while ( $row = @mysql_fetch_assoc($q) ) |
|
8 | 88 |
{ |
20 | 89 |
$total++; |
90 |
if ( isset($userdata[ $row['nick'] ]) ) |
|
91 |
{ |
|
92 |
$userdata[ $row['nick'] ]++; |
|
93 |
} |
|
94 |
else |
|
95 |
{ |
|
96 |
$userdata[ $row['nick'] ] = 1; |
|
97 |
} |
|
8 | 98 |
} |
20 | 99 |
foreach ( $userdata as &$val ) |
100 |
{ |
|
101 |
$val = $val / $total; |
|
102 |
} |
|
103 |
mysql_free_result($q); |
|
104 |
arsort($userdata); |
|
105 |
return $userdata; |
|
8 | 106 |
} |
20 | 107 |
return false; |
15 | 108 |
} |
109 |
||
110 |
/** |
|
43
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
111 |
* Raw data. |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
112 |
* @param string Channel name |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
113 |
* @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
|
114 |
* @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
|
115 |
* @return array MySQL rows. |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
116 |
*/ |
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 |
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
|
119 |
{ |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
120 |
$channel = db_escape($channel); |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
121 |
$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
|
122 |
$time_max =& $base; |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
123 |
$total = 0; |
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 |
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
|
126 |
{ |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
127 |
$userdata = array(); |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
128 |
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
|
129 |
{ |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
130 |
$userdata[] = $row; |
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 $userdata; |
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 |
return false; |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
135 |
} |
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
136 |
|
ce2e9caf2dfa
Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents:
36
diff
changeset
|
137 |
/** |
15 | 138 |
* Return the time that the stats DB was last updated. |
139 |
* @return int |
|
140 |
*/ |
|
141 |
||
142 |
function stats_last_updated() |
|
143 |
{ |
|
20 | 144 |
// :-D |
145 |
return NOW; |
|
15 | 146 |
} |
147 |
||
148 |