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 |
$stats_merged_data = array('counts' => array(), 'messages' => array());
|
|
11 |
$stats_data =& $stats_merged_data;
|
8
|
12 |
|
15
|
13 |
define('ENANOBOT_ROOT', dirname(__FILE__));
|
8
|
14 |
define('NOW', time());
|
|
15 |
|
|
16 |
/**
|
|
17 |
* Gets the number of messages posted in IRC in the last X minutes.
|
|
18 |
* @param string Channel
|
|
19 |
* @param int Optional - time period for message count. Defaults to 10 minutes.
|
|
20 |
* @param int Optional - Base time, defaults to right now
|
|
21 |
* @return int
|
|
22 |
*/
|
|
23 |
|
|
24 |
function stats_message_count($channel, $mins = 10, $base = NOW)
|
|
25 |
{
|
15
|
26 |
global $stats_merged_data;
|
8
|
27 |
|
|
28 |
$time_min = $base - ( $mins * 60 );
|
|
29 |
$time_max = $base;
|
|
30 |
|
15
|
31 |
if ( !isset($stats_merged_data['messages'][$channel]) )
|
8
|
32 |
{
|
|
33 |
return 0;
|
|
34 |
}
|
|
35 |
|
|
36 |
$count = 0;
|
15
|
37 |
foreach ( $stats_merged_data['messages'][$channel] as $message )
|
8
|
38 |
{
|
|
39 |
if ( $message['time'] >= $time_min && $message['time'] <= $time_max )
|
|
40 |
{
|
|
41 |
$count++;
|
|
42 |
}
|
|
43 |
}
|
|
44 |
|
|
45 |
return $count;
|
|
46 |
}
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Gets the percentages as to who's posted the most messages in the last X minutes.
|
|
50 |
* @param string Channel name
|
|
51 |
* @param int Optional - How many minutes, defaults to 10
|
|
52 |
* @param int Optional - Base time, defaults to right now
|
|
53 |
* @return array Associative, with floats.
|
|
54 |
*/
|
|
55 |
|
|
56 |
function stats_activity_percent($channel, $mins = 10, $base = NOW)
|
|
57 |
{
|
15
|
58 |
global $stats_merged_data;
|
8
|
59 |
if ( !($total = stats_message_count($channel, $mins, $base)) )
|
|
60 |
{
|
|
61 |
return array();
|
|
62 |
}
|
|
63 |
$results = array();
|
|
64 |
$usercounts = array();
|
|
65 |
$time_min = $base - ( $mins * 60 );
|
|
66 |
$time_max = $base;
|
15
|
67 |
foreach ( $stats_merged_data['messages'][$channel] as $message )
|
8
|
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 |
}
|
15
|
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;
|
16
|
125 |
if ( isset($data['counts']) )
|
15
|
126 |
{
|
16
|
127 |
foreach ( $data['counts'] as $channel => $chaninfo )
|
15
|
128 |
{
|
16
|
129 |
if ( isset($stats_merged_data['counts'][$channel]) )
|
15
|
130 |
{
|
16
|
131 |
foreach ( $stats_merged_data['counts'][$channel] as $key => &$value )
|
15
|
132 |
{
|
16
|
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 |
}
|
15
|
141 |
}
|
|
142 |
}
|
16
|
143 |
else
|
|
144 |
{
|
|
145 |
$stats_merged_data['counts'][$channel] = $chaninfo;
|
|
146 |
}
|
15
|
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 |
|