stats-fe.php
changeset 8 0acb8d9a3194
child 15 5e2d1514ccd0
equal deleted inserted replaced
7:1d6e762433fe 8:0acb8d9a3194
       
     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 
       
    10 if ( !isset($GLOBALS['stats_data']) )
       
    11 {
       
    12   require(dirname(__FILE__) . '/stats-data.php');
       
    13   $data =& $stats_data;
       
    14 }
       
    15 
       
    16 define('NOW', time());
       
    17 
       
    18 /**
       
    19  * Gets the number of messages posted in IRC in the last X minutes.
       
    20  * @param string Channel
       
    21  * @param int Optional - time period for message count. Defaults to 10 minutes.
       
    22  * @param int Optional - Base time, defaults to right now
       
    23  * @return int
       
    24  */
       
    25 
       
    26 function stats_message_count($channel, $mins = 10, $base = NOW)
       
    27 {
       
    28   global $data;
       
    29   
       
    30   $time_min = $base - ( $mins * 60 );
       
    31   $time_max = $base;
       
    32   
       
    33   if ( !isset($data['messages'][$channel]) )
       
    34   {
       
    35     return 0;
       
    36   }
       
    37   
       
    38   $count = 0;
       
    39   foreach ( $data['messages'][$channel] as $message )
       
    40   {
       
    41     if ( $message['time'] >= $time_min && $message['time'] <= $time_max )
       
    42     {
       
    43       $count++;
       
    44     }
       
    45   }
       
    46   
       
    47   return $count;
       
    48 }
       
    49 
       
    50 /**
       
    51  * Gets the percentages as to who's posted the most messages in the last X minutes.
       
    52  * @param string Channel name
       
    53  * @param int Optional - How many minutes, defaults to 10
       
    54  * @param int Optional - Base time, defaults to right now
       
    55  * @return array Associative, with floats.
       
    56  */
       
    57 
       
    58 function stats_activity_percent($channel, $mins = 10, $base = NOW)
       
    59 {
       
    60   global $data;
       
    61   if ( !($total = stats_message_count($channel, $mins, $base)) )
       
    62   {
       
    63     return array();
       
    64   }
       
    65   $results = array();
       
    66   $usercounts = array();
       
    67   $time_min = $base - ( $mins * 60 );
       
    68   $time_max = $base;
       
    69   foreach ( $data['messages'][$channel] as $message )
       
    70   {
       
    71     if ( $message['time'] >= $time_min && $message['time'] <= $time_max )
       
    72     {
       
    73       if ( !isset($usercounts[$message['nick']]) )
       
    74         $usercounts[$message['nick']] = 0;
       
    75       $usercounts[$message['nick']]++;
       
    76     }
       
    77   }
       
    78   foreach ( $usercounts as $nick => $count )
       
    79   {
       
    80     $results[$nick] = $count / $total;
       
    81   }
       
    82   arsort($results);
       
    83   return $results;
       
    84 }