stats-fe.php
author Dan
Sat, 10 Jan 2009 14:07:54 -0500
changeset 48 d793361a27ec
parent 44 73f74d395f95
permissions -rw-r--r--
Greeting: bot no longer greets itself
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
44
73f74d395f95 Added theme support. Oh yeah, you can customize graphs using graph_{line,bar}.def.
Dan
parents: 43
diff changeset
    17
$webtheme = ( isset($webtheme) && is_dir(ENANOBOT_ROOT . "/htdocs/themes/$webtheme") ) ? $webtheme : 'generic';
73f74d395f95 Added theme support. Oh yeah, you can customize graphs using graph_{line,bar}.def.
Dan
parents: 43
diff changeset
    18
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    19
mysql_reconnect();
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    21
/**
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    22
 * Gets ths list of channels.
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    23
 * @return array
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    24
 */
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    25
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    26
function stats_channel_list()
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    27
{
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    28
  return $GLOBALS['channels'];
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    29
}
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    30
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    31
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    32
 * Gets the number of messages posted in IRC in the last X minutes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    33
 * @param string Channel
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    34
 * @param int Optional - time period for message count. Defaults to 10 minutes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    35
 * @param int Optional - Base time, defaults to right now
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    36
 * @return int
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    37
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    38
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    39
function stats_message_count($channel, $mins = 10, $base = NOW)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    40
{
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    41
  $channel = db_escape($channel);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    42
  $time_min = $base - ( $mins * 60 );
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    43
  $time_max =& $base;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    44
  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
    45
  {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    46
    if ( mysql_num_rows($q) > 0 )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    47
    {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    48
      $row = mysql_fetch_assoc($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    49
      mysql_free_result($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    50
      return intval($row['message_count']);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    51
    }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    52
    mysql_free_result($q);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    53
  }
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    54
  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
    55
  {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    56
    $row = mysql_fetch_row($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    57
    $count = $row[0];
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    58
    mysql_free_result($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    59
    // avoid caching future queries
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    60
    if ( $base <= NOW )
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    61
    {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    62
      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
    63
    }
36
a7d884914a74 Added extern script to allow exporting some stats through a JSON or XML API. WiP.
Dan
parents: 28
diff changeset
    64
    return intval($count);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    65
  }
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    66
  return false;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    67
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    68
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    69
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    70
 * 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
    71
 * @param string Channel name
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    72
 * @param int Optional - How many minutes, defaults to 10
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    73
 * @param int Optional - Base time, defaults to right now
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    74
 * @return array Associative, with floats.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    75
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    76
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    77
function stats_activity_percent($channel, $mins = 10, $base = NOW)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    78
{
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    79
  $channel = db_escape($channel);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    80
  $time_min = $base - ( $mins * 60 );
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    81
  $time_max =& $base;
28
a7e537c0b549 Fix undefined $total in stats-fe
Dan
parents: 20
diff changeset
    82
  $total = 0;
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    83
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    84
  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
    85
  {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    86
    $userdata = array();
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    87
    while ( $row = @mysql_fetch_assoc($q) )
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    88
    {
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    89
      $total++;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    90
      if ( isset($userdata[ $row['nick'] ]) )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    91
      {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    92
        $userdata[ $row['nick'] ]++;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    93
      }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    94
      else
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    95
      {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    96
        $userdata[ $row['nick'] ] = 1;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    97
      }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    98
    }
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
    99
    foreach ( $userdata as &$val )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   100
    {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   101
      $val = $val / $total;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   102
    }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   103
    mysql_free_result($q);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   104
    arsort($userdata);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   105
    return $userdata;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   106
  }
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   107
  return false;
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   108
}
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   109
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   110
/**
43
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   111
 * Raw data.
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   112
 * @param string Channel name
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   113
 * @param int How many minutes, defaults to 60
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   114
 * @param int Base time, defaults to right now
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   115
 * @return array MySQL rows.
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   116
 */
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   117
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   118
function stats_raw_data($channel, $mins = 60, $base = NOW)
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   119
{
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   120
  $channel = db_escape($channel);
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   121
  $time_min = $base - ( $mins * 60 );
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   122
  $time_max =& $base;
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   123
  $total = 0;
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   124
  
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   125
  if ( $q = eb_mysql_query("SELECT * FROM stats_messages WHERE channel = '$channel' AND time >= $time_min AND time <= $time_max ORDER BY message_id ASC;") )
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   126
  {
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   127
    $userdata = array();
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   128
    while ( $row = @mysql_fetch_assoc($q) )
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   129
    {
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   130
      $userdata[] = $row;
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   131
    }
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   132
    return $userdata;
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   133
  }
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   134
  return false;
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   135
}
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   136
ce2e9caf2dfa Added support for different types of graphs; added a last 60 minutes line graph
Dan
parents: 36
diff changeset
   137
/**
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   138
 * 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
   139
 * @return int
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   140
 */
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   141
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   142
function stats_last_updated()
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   143
{
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   144
  // :-D
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents: 16
diff changeset
   145
  return NOW;
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   146
}
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   147
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   148