09 Alternative Statistics On Index

Started by Mindless, July 21, 2012, 09:56:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mindless

Credits to Xuzo and putyn.
Xhtml valid.
Stats will use one query on index cached, stats are updated in cleanup.
   
Run the sql :

Code (sql) Select
CREATE TABLE `stats` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `regusers` int(10) unsigned NOT NULL default '0',
  `unconusers` int(10) unsigned NOT NULL default '0',
  `torrents` int(10) unsigned NOT NULL default '0',
  `seeders` int(10) unsigned NOT NULL default '0',
  `leechers` int(10) unsigned NOT NULL default '0',
  `torrentstoday` int(10) unsigned NOT NULL default '0',
  `donors` int(10) unsigned NOT NULL default '0',
  `unconnectables` int(10) unsigned NOT NULL default '0',
  `forumtopics` int(10) unsigned NOT NULL default '0',
  `forumposts` int(10) unsigned NOT NULL default '0',
  `numactive` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `stats` (`id`) VALUES ('1');


@File include/cleanup.php ( Add it to the bottom of the docleanup function before the last } )

Code (php) Select
$registered = get_row_count('users');
$unverified = get_row_count('users', "WHERE status='pending'");
$torrents = get_row_count('torrents');
$seeders = get_row_count('peers', "WHERE seeder='yes'");
$leechers = get_row_count('peers', "WHERE seeder='no'");
$torrentstoday = get_row_count('torrents', 'WHERE added > DATE_SUB(NOW(), INTERVAL 1 DAY)');
$donors = get_row_count('users', "WHERE donor='yes'");
$unconnectables = get_row_count("peers", " WHERE connectable='no'");
$forumposts = get_row_count("posts");
$forumtopics = get_row_count("topics");
$dt = sqlesc(time() - 300); // Active users last 5 minutes
$numactive = get_row_count("users", "WHERE last_access >= $dt");
mysql_query("UPDATE stats SET regusers = '$registered', unconusers = '$unverified', torrents = '$torrents', seeders = '$seeders', leechers = '$leechers', unconnectables = '$unconnectables', torrentstoday = '$torrentstoday', donors = '$donors', forumposts = '$forumposts', forumtopics = '$forumtopics', numactive = '$numactive' WHERE id = '1' LIMIT 1");


@File index.php find :

   
Code (php) Select
$registered = number_format(get_row_count("users"));
    //$unverified = number_format(get_row_count("users", "WHERE status='pending'"));
    $torrents = number_format(get_row_count("torrents"));
    //$dead = number_format(get_row_count("torrents", "WHERE visible='no'"));

    $r = mysql_query("SELECT value_u FROM avps WHERE arg='seeders'") or sqlerr(__FILE__, __LINE__);
    $a = mysql_fetch_row($r);
    $seeders = 0 + $a[0];
    $r = mysql_query("SELECT value_u FROM avps WHERE arg='leechers'") or sqlerr(__FILE__, __LINE__);
    $a = mysql_fetch_row($r);
    $leechers = 0 + $a[0];
    if ($leechers == 0)
      $ratio = 0;
    else
      $ratio = round($seeders / $leechers * 100);
    $peers = number_format($seeders + $leechers);
    $seeders = number_format($seeders);
    $leechers = number_format($leechers);


Replace with :

   
Code (php) Select
//==Stats Begin
    $cache_stats = "./cache/stats.txt";
    $cache_stats_life = 5 * 60; // 5min
    if (file_exists($cache_stats) && is_array(unserialize(file_get_contents($cache_stats))) && (time() - filemtime($cache_stats)) < $cache_stats_life)
    $row = unserialize(@file_get_contents($cache_stats));
    else {
    $stats = mysql_query("SELECT *, seeders + leechers AS peers, seeders / leechers AS ratio, unconnectables / (seeders + leechers) AS ratiounconn FROM stats WHERE id = '1' LIMIT 1") or sqlerr(__FILE__, __LINE__);
    $row = mysql_fetch_assoc($stats);
    $handle = fopen($cache_stats, "w+");
    fwrite($handle, serialize($row));
    fclose($handle);
    }

    $seeders = number_format($row['seeders']);
    $leechers = number_format($row['leechers']);
    $registered = number_format($row['regusers']);
    $unverified = number_format($row['unconusers']);
    $torrents = number_format($row['torrents']);
    $torrentstoday = number_format($row['torrentstoday']);
    $ratiounconn = $row['ratiounconn'];
    $unconnectables = $row['unconnectables'];
    $ratio = round(($row['ratio'] * 100));
    $peers = number_format($row['peers']);
    $numactive = number_format($row['numactive']);
    $donors = number_format($row['donors']);
    $forumposts = number_format($row['forumposts']);
    $forumtopics = number_format($row['forumtopics']);
    //==End


Find :

Code (php) Select
$HTMLOUT .= "<div style='text-align:left;width:80%;border:1px solid blue;padding:5px;'>
    <div style='background:lightgrey;height:25px;'><span style='font-weight:bold;font-size:12pt;'>{$lang['stats_title']}</span></div><br />
   
      <table align='center' class='main' border='1' cellspacing='0' cellpadding='5'>
      <tr>
      <td class='rowhead'>{$lang['stats_regusers']}</td><td align='right'>{$registered}</td>
      </tr>
      <!-- <tr><td class='rowhead'>{$lang['stats_unverified']}</td><td align=right>{unverified}</td></tr> -->
      <tr>
      <td class='rowhead'>{$lang['stats_torrents']}</td><td align='right'>{$torrents}</td>
      </tr>";
     
    if (isset($peers))
    {
      $HTMLOUT .= "<tr><td class='rowhead'>{$lang['stats_peers']}</td><td align='right'>{$peers}</td></tr>
      <tr><td class='rowhead'>{$lang['stats_seed']}</td><td align='right'>{$seeders}</td></tr>
      <tr><td class='rowhead'>{$lang['stats_leech']}</td><td align='right'>{$leechers}</td></tr>
      <tr><td class='rowhead'>{$lang['stats_sl_ratio']}</td><td align='right'>{$ratio}</td></tr>";
    }
   
      $HTMLOUT .= "</table>
      </div>";


Replace with :

   
Code (php) Select
$HTMLOUT .="<div class='roundedCorners' style='text-align:left;width:80%;border:1px solid black;padding:5px;'>
   <div style='background:transparent;height:25px;'><span style='font-weight:bold;font-size:12pt;'>{$lang['index_stats_title']}</span></div><br />
   <table width='100%' border='1' cellspacing='0' cellpadding='10'><tr><td align='center'>
   <table class='main' border='1' cellspacing='0' cellpadding='5'>
   <tr>
<td class='rowhead'>{$lang['index_stats_regged']}</td><td align='right'>{$registered}/{$TBDEV['maxusers']}</td>
<td class='rowhead'>{$lang['index_stats_online']}</td><td align='right'>{$numactive}</td>
   </tr>
   <tr>
<td class='rowhead'>{$lang['index_stats_uncon']}</td><td align='right'>{$unverified}</td>
<td class='rowhead'>{$lang['index_stats_donor']}</td><td align='right'>{$donors}</td>
   </tr>
   <tr>
<td colspan='4'> </td>
   </tr>
   <tr>
<td class='rowhead'>{$lang['index_stats_topics']}</td><td align='right'>{$forumtopics}</td>
<td class='rowhead'>{$lang['index_stats_torrents']}</td><td align='right'>{$torrents}</td>
   </tr>
   <tr>
   <td class='rowhead'>{$lang['index_stats_posts']}</td><td align='right'>{$forumposts}</td>
<td class='rowhead'>{$lang['index_stats_newtor']}</td><td align='right'>{$torrentstoday}</td>
   </tr>
   <tr>
   <td colspan='4'> </td>
   </tr>
   <tr>
<td class='rowhead'>{$lang['index_stats_peers']}</td><td align='right'>{$peers}</td>
<td class='rowhead'>{$lang['index_stats_unconpeer']}</td><td align='right'>{$unconnectables}</td>
   </tr>
   <tr>
<td class='rowhead'>{$lang['index_stats_seeders']}</td><td align='right'>{$seeders}</td>
<td class='rowhead' align='right'><b>{$lang['index_stats_unconratio']}</b></td><td align='right'><b>".round($ratiounconn * 100)."</b></td>
   </tr>
   <tr>
<td class='rowhead'>{$lang['index_stats_leechers']}</td><td align='right'>{$leechers}</td>
<td class='rowhead'>{$lang['index_stats_slratio']}</td><td align='right'>{$ratio}</td>
   </tr></table></td></tr></table></div><br />";


Adjust html tables ect to suite your needs, as normal its done to default vanila style.

@File lang/en/lang_index.php find :

Code (php) Select
#Stats
'stats_title' => "Stats",
'stats_regusers' => "Registered Users",
'stats_unverified' => "Unconfirmed Users",
'stats_torrents' => "Torrents",
'stats_peers' => "Peers",
'stats_seed' => "Seeders",
'stats_leech' => "Leechers",
'stats_sl_ratio' => "Seeder/leecher ratio (%)",


Replace with :

Code (php) Select
#Stats
'index_stats_title' => "Stats",
'index_stats_regged' => "Registered Users",
'index_stats_online' => "Users Online",
'index_stats_uncon' => "Unconfirmed Users",
'index_stats_donor' => "Donors",
'index_stats_topics' => "Forum Topics",
'index_stats_torrents' => "Torrents",
'index_stats_posts' => "Forum Posts",
'index_stats_newtor' => "New Torrents Today",
'index_stats_peers' => "Peers",
'index_stats_unconpeer' => "Unconnectable Peers",
'index_stats_seeders' => "Seeders",
'index_stats_unconratio' => "Unconnectables ratio (%)",
'index_stats_leechers' => "Leechers",
'index_stats_slratio' => "Seeder/leecher ratio (%)",[code]


Thats all - any problems - improvements suggestions please post :)