stats-fe.php
author Dan
Sat, 25 Oct 2008 20:24:31 +0000
changeset 18 07be890ef627
parent 16 01c6a003d762
child 20 e2f6e3af9959
permissions -rw-r--r--
Minor fix to snippets web iface
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
$stats_merged_data = array('counts' => array(), 'messages' => array());
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    11
$stats_data =& $stats_merged_data;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    12
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    13
define('ENANOBOT_ROOT', dirname(__FILE__));
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    14
define('NOW', time());
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    15
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    16
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    17
 * Gets the number of messages posted in IRC in the last X minutes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    18
 * @param string Channel
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    19
 * @param int Optional - time period for message count. Defaults to 10 minutes.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    20
 * @param int Optional - Base time, defaults to right now
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    21
 * @return int
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    22
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    23
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    24
function stats_message_count($channel, $mins = 10, $base = NOW)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    25
{
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    26
  global $stats_merged_data;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    27
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    28
  $time_min = $base - ( $mins * 60 );
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    29
  $time_max = $base;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    30
  
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    31
  if ( !isset($stats_merged_data['messages'][$channel]) )
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    32
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    33
    return 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    34
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    35
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    36
  $count = 0;
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    37
  foreach ( $stats_merged_data['messages'][$channel] as $message )
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    38
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    39
    if ( $message['time'] >= $time_min && $message['time'] <= $time_max )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    40
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    41
      $count++;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    42
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    43
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    44
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    45
  return $count;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    46
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    47
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    48
/**
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    49
 * 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
    50
 * @param string Channel name
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    51
 * @param int Optional - How many minutes, defaults to 10
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    52
 * @param int Optional - Base time, defaults to right now
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    53
 * @return array Associative, with floats.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    54
 */
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    55
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    56
function stats_activity_percent($channel, $mins = 10, $base = NOW)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    57
{
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    58
  global $stats_merged_data;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    59
  if ( !($total = stats_message_count($channel, $mins, $base)) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    60
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    61
    return array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    62
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    63
  $results = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    64
  $usercounts = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    65
  $time_min = $base - ( $mins * 60 );
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    66
  $time_max = $base;
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    67
  foreach ( $stats_merged_data['messages'][$channel] as $message )
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    68
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    69
    if ( $message['time'] >= $time_min && $message['time'] <= $time_max )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    70
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    71
      if ( !isset($usercounts[$message['nick']]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    72
        $usercounts[$message['nick']] = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    73
      $usercounts[$message['nick']]++;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    74
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    75
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    76
  foreach ( $usercounts as $nick => $count )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    77
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    78
    $results[$nick] = $count / $total;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    79
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    80
  arsort($results);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    81
  return $results;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    82
}
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    83
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    84
/**
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    85
 * Loads X days of statistics, minimum.
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    86
 * @param int Days to load, default is 1
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    87
 */
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    88
 
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    89
function load_stats_data($days = 1)
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    90
{
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    91
  $days++;
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    92
  for ( $i = 0; $i < $days; $i++ )
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    93
  {
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    94
    $day = NOW - ( $i * 86400 );
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    95
    $day = gmdate('Ymd', $day);
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    96
    if ( file_exists(ENANOBOT_ROOT . "/stats/stats-data-$day.php") )
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    97
    {
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    98
      require(ENANOBOT_ROOT . "/stats/stats-data-$day.php");
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    99
      stats_merge($stats_data);
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   100
    }
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   101
  }
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   102
}
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   103
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   104
/**
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   105
 * 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
   106
 * @return int
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
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   109
function stats_last_updated()
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
  $day = gmdate('Ymd');
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   112
  $file = ENANOBOT_ROOT . "/stats/stats-data-$day.php";
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   113
  return ( file_exists($file) ) ? filemtime($file) : 0;
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   114
}
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   115
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
 * Merges a newly loaded stats array with the current cache in RAM.
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   118
 * @param array Data to merge
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   119
 * @access private
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   120
 */
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   121
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   122
function stats_merge($data)
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   123
{
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   124
  global $stats_merged_data;
16
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   125
  if ( isset($data['counts']) )
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   126
  {
16
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   127
    foreach ( $data['counts'] as $channel => $chaninfo )
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   128
    {
16
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   129
      if ( isset($stats_merged_data['counts'][$channel]) )
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   130
      {
16
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   131
        foreach ( $stats_merged_data['counts'][$channel] as $key => &$value )
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   132
        {
16
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   133
          if ( is_int($value) )
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   134
          {
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   135
            $value = max($value, $chaninfo[$key]);
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   136
          }
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   137
          else if ( is_array($value) )
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   138
          {
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   139
            $value = array_merge($value, $chaninfo[$key]);
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   140
          }
15
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
      }
16
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   143
      else
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   144
      {
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   145
        $stats_merged_data['counts'][$channel] = $chaninfo;
01c6a003d762 Fixed reference to missing key "counts" in stats-fe
Dan
parents: 15
diff changeset
   146
      }
15
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
  }
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   149
  foreach ( $data['messages'] as $channel => $chandata )
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   150
  {
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   151
    if ( isset($stats_merged_data['messages'][$channel]) )
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   152
    {
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   153
      foreach ( $chandata as $message )
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   154
      {
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   155
        $stats_merged_data['messages'][$channel][] = $message;
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   156
      }
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   157
    }
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   158
    else
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   159
    {
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   160
      $stats_merged_data['messages'][$channel] = $chandata;
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   161
    }
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   162
  }
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   163
}
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   164
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   165
load_stats_data();
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   166