stats-fe.php
author Dan
Sun, 31 Aug 2008 10:13:20 +0000
changeset 11 b3fec7467c05
parent 8 0acb8d9a3194
child 15 5e2d1514ccd0
permissions -rw-r--r--
Fixed $nick being overwritten in index
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     1
<?php
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     2
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     3
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     4
 * Frontend for statistics data. Handles fetching and calculating data from raw statistics stored in stats-data.php.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     5
 * @package EnanoBot
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     6
 * @subpackage stats
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     7
 * @author Dan Fuhry <dan@enanocms.org>
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     8
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     9
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    10
if ( !isset($GLOBALS['stats_data']) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    11
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    12
  require(dirname(__FILE__) . '/stats-data.php');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    13
  $data =& $stats_data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    14
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    15
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    16
define('NOW', time());
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    17
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    18
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    19
 * Gets the number of messages posted in IRC in the last X minutes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    20
 * @param string Channel
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    21
 * @param int Optional - time period for message count. Defaults to 10 minutes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    22
 * @param int Optional - Base time, defaults to right now
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    23
 * @return int
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    24
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    25
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    26
function stats_message_count($channel, $mins = 10, $base = NOW)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    27
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    28
  global $data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    29
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    30
  $time_min = $base - ( $mins * 60 );
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    31
  $time_max = $base;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    32
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    33
  if ( !isset($data['messages'][$channel]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    34
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    35
    return 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    36
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    37
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    38
  $count = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    39
  foreach ( $data['messages'][$channel] as $message )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    40
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    41
    if ( $message['time'] >= $time_min && $message['time'] <= $time_max )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    42
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    43
      $count++;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    44
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    45
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    46
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    47
  return $count;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    48
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    49
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    50
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    51
 * Gets the percentages as to who's posted the most messages in the last X minutes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    52
 * @param string Channel name
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    53
 * @param int Optional - How many minutes, defaults to 10
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    54
 * @param int Optional - Base time, defaults to right now
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    55
 * @return array Associative, with floats.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    56
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    57
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    58
function stats_activity_percent($channel, $mins = 10, $base = NOW)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    59
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    60
  global $data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    61
  if ( !($total = stats_message_count($channel, $mins, $base)) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    62
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    63
    return array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    64
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    65
  $results = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    66
  $usercounts = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    67
  $time_min = $base - ( $mins * 60 );
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    68
  $time_max = $base;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    69
  foreach ( $data['messages'][$channel] as $message )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    70
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    71
    if ( $message['time'] >= $time_min && $message['time'] <= $time_max )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    72
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    73
      if ( !isset($usercounts[$message['nick']]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    74
        $usercounts[$message['nick']] = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    75
      $usercounts[$message['nick']]++;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    76
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    77
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    78
  foreach ( $usercounts as $nick => $count )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    79
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    80
    $results[$nick] = $count / $total;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    81
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    82
  arsort($results);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    83
  return $results;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    84
}