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 |
}
|
20
|
62 |
return $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;
|
|
80 |
|
|
81 |
if ( $q = eb_mysql_query("SELECT nick FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max;") )
|
8
|
82 |
{
|
20
|
83 |
$userdata = array();
|
|
84 |
while ( $row = @mysql_fetch_assoc($q) )
|
8
|
85 |
{
|
20
|
86 |
$total++;
|
|
87 |
if ( isset($userdata[ $row['nick'] ]) )
|
|
88 |
{
|
|
89 |
$userdata[ $row['nick'] ]++;
|
|
90 |
}
|
|
91 |
else
|
|
92 |
{
|
|
93 |
$userdata[ $row['nick'] ] = 1;
|
|
94 |
}
|
8
|
95 |
}
|
20
|
96 |
foreach ( $userdata as &$val )
|
|
97 |
{
|
|
98 |
$val = $val / $total;
|
|
99 |
}
|
|
100 |
mysql_free_result($q);
|
|
101 |
arsort($userdata);
|
|
102 |
return $userdata;
|
8
|
103 |
}
|
20
|
104 |
return false;
|
15
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Return the time that the stats DB was last updated.
|
|
109 |
* @return int
|
|
110 |
*/
|
|
111 |
|
|
112 |
function stats_last_updated()
|
|
113 |
{
|
20
|
114 |
// :-D
|
|
115 |
return NOW;
|
15
|
116 |
}
|
|
117 |
|
|
118 |
|