stats-fe.php
author Dan
Sat, 15 Nov 2008 15:02:59 -0500
changeset 23 df31a3872d19
parent 20 e2f6e3af9959
child 28 a7e537c0b549
permissions -rw-r--r--
Made error handling for MySQL better; added ability to use custom shutdown messages
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
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    10
define('ENANOBOT_ROOT', dirname(__FILE__));
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    11
define('NOW', time());
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    12
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    13
require(ENANOBOT_ROOT . '/config.php');
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    14
require(ENANOBOT_ROOT . '/hooks.php');
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    15
require(ENANOBOT_ROOT . '/database.php');
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    16
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    17
mysql_reconnect();
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    18
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    19
/**
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    20
 * Gets ths list of channels.
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    21
 * @return array
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    22
 */
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    23
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    24
function stats_channel_list()
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    25
{
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    26
  return $GLOBALS['channels'];
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    27
}
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    28
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    29
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    30
 * Gets the number of messages posted in IRC in the last X minutes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    31
 * @param string Channel
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    32
 * @param int Optional - time period for message count. Defaults to 10 minutes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    33
 * @param int Optional - Base time, defaults to right now
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    34
 * @return int
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    35
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    36
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    37
function stats_message_count($channel, $mins = 10, $base = NOW)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    38
{
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    39
  $channel = db_escape($channel);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    40
  $time_min = $base - ( $mins * 60 );
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    41
  $time_max =& $base;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    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
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    43
  {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    44
    if ( mysql_num_rows($q) > 0 )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    45
    {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    46
      $row = mysql_fetch_assoc($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    47
      mysql_free_result($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    48
      return intval($row['message_count']);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    49
    }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    50
    mysql_free_result($q);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    51
  }
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    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
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    53
  {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    54
    $row = mysql_fetch_row($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    55
    $count = $row[0];
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    56
    mysql_free_result($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    57
    // avoid caching future queries
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    58
    if ( $base <= NOW )
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    59
    {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    60
      eb_mysql_query("INSERT INTO stats_count_cache(channel, time_min, time_max, message_count) VALUES('$channel', $time_min, $time_max, $count);");
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    61
    }
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    62
    return $count;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    63
  }
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    64
  return false;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    65
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    66
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    67
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    68
 * 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
    69
 * @param string Channel name
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    70
 * @param int Optional - How many minutes, defaults to 10
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    71
 * @param int Optional - Base time, defaults to right now
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    72
 * @return array Associative, with floats.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    73
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    74
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    75
function stats_activity_percent($channel, $mins = 10, $base = NOW)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    76
{
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    77
  $channel = db_escape($channel);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    78
  $time_min = $base - ( $mins * 60 );
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    79
  $time_max =& $base;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    80
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    81
  if ( $q = eb_mysql_query("SELECT nick FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max;") )
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    82
  {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    83
    $userdata = array();
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    84
    while ( $row = @mysql_fetch_assoc($q) )
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    85
    {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    86
      $total++;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    87
      if ( isset($userdata[ $row['nick'] ]) )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    88
      {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    89
        $userdata[ $row['nick'] ]++;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    90
      }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    91
      else
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    92
      {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    93
        $userdata[ $row['nick'] ] = 1;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    94
      }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    95
    }
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    96
    foreach ( $userdata as &$val )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    97
    {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    98
      $val = $val / $total;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    99
    }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   100
    mysql_free_result($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   101
    arsort($userdata);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   102
    return $userdata;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   103
  }
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   104
  return false;
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   105
}
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   106
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   107
/**
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   108
 * Return the time that the stats DB was last updated.
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   109
 * @return int
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   110
 */
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   111
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   112
function stats_last_updated()
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   113
{
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   114
  // :-D
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   115
  return NOW;
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   116
}
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   117
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   118