From 649b527a8f9faeb3e50463770057975ad523b73a Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 15 May 2013 01:07:59 +0200 Subject: [PATCH] further moved stats from user into statistics class, added more caching --- public/include/classes/statistics.class.php | 25 +++++++++++++++++ public/include/classes/user.class.php | 16 +---------- public/include/smarty_globals.inc.php | 31 +++++++++++++++------ public/templates/mmcFE/global/sidebar.tpl | 8 +++--- 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index d409c131..aa08f014 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -87,6 +87,31 @@ class Statistics { return false; } + public function getUserShares($account_id) { + $stmt = $this->mysqli->prepare(" + SELECT + ( + SELECT COUNT(s.id) + FROM " . $this->share->getTableName() . " AS s, " . $this->user->getTableName() . " AS u + WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) + AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM blocks AS b),0) + AND our_result = 'Y' + AND u.id = ? + ) AS valid, + ( + SELECT COUNT(s.id) + FROM " . $this->share->getTableName() . " AS s, " . $this->user->getTableName() . " AS u + WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) + AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM blocks AS b),0) + AND our_result = 'N' + AND u.id = ? + ) AS invalid"); + if ($stmt && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $result = $stmt->get_result()) return $result->fetch_assoc(); + // Catchall + $this->debug->append("Unable to fetch user round shares: " . $this->mysqli->error); + return false; + } + public function getUserHashrate($account_id) { $stmt = $this->mysqli->prepare(" SELECT ROUND(COUNT(s.id) * POW(2,21)/600/1000) AS hashrate diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index fe358314..11f12403 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -171,21 +171,7 @@ class User { $stmt = $this->mysqli->prepare(" SELECT id, username, pin, pass, admin, - IFNULL(donate_percent, '0') as donate_percent, coin_address, ap_threshold, - ( - SELECT COUNT(id) - FROM shares - WHERE $this->table.username = SUBSTRING_INDEX( `username` , '.', 1 ) - AND UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(time) FROM blocks),0) - AND our_result = 'Y' - ) AS valid, - ( - SELECT COUNT(id) - FROM shares - WHERE $this->table.username = SUBSTRING_INDEX( `username` , '.', 1 ) - AND UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(time) FROM blocks),0) - AND our_result = 'N' - ) AS invalid + IFNULL(donate_percent, '0') as donate_percent, coin_address, ap_threshold FROM $this->table WHERE id = ? LIMIT 0,1"); echo $this->mysqli->error; diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index 218e6af4..29db7683 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -9,27 +9,31 @@ $debug->append('Global smarty variables', 3); // Store some stuff in memcache prior to assigning it to Smarty if (!$aRoundShares = $memcache->get('aRoundShares')) { - $debug->append('Fetching aRoundShares from database'); + $debug->append('STA Fetching aRoundShares from database'); $aRoundShares = $statistics->getRoundShares(); - $memcache->set('aRoundShares', $aRoundShares, 60); + $debug->append('END Fetching aRoundShares from database'); + $memcache->set('aRoundShares', $aRoundShares, 90); } if (!$iCurrentActiveWorkers = $memcache->get('iCurrentActiveWorkers')) { - $debug->append('Fetching iCurrentActiveWorkers from database'); + $debug->append('STA Fetching iCurrentActiveWorkers from database'); $iCurrentActiveWorkers = $worker->getCountAllActiveWorkers(); - $memcache->set('iCurrentActiveWorkers', $iCurrentActiveWorkers, 60); + $debug->append('END Fetching iCurrentActiveWorkers from database'); + $memcache->set('iCurrentActiveWorkers', $iCurrentActiveWorkers, 80); } if (!$iCurrentPoolHashrate = $memcache->get('iCurrentPoolHashrate')) { - $debug->append('Fetching iCurrentPoolHashrate from database'); + $debug->append('STA Fetching iCurrentPoolHashrate from database'); $iCurrentPoolHashrate = $statistics->getCurrentHashrate(); - $memcache->set('iCurrentPoolHashrate', $iCurrentPoolHashrate, 60); + $debug->append('END Fetching iCurrentPoolHashrate from database'); + $memcache->set('iCurrentPoolHashrate', $iCurrentPoolHashrate, 90); } if (!$iCurrentPoolShareRate = $memcache->get('iCurrentPoolShareRate')) { - $debug->append('Fetching iCurrentPoolShareRate from database'); + $debug->append('STA Fetching iCurrentPoolShareRate from database'); $iCurrentPoolShareRate = $statistics->getCurrentShareRate(); - $memcache->set('iCurrentPoolShareRate', $iCurrentPoolShareRate, 60); + $debug->append('END Fetching iCurrentPoolShareRate from database'); + $memcache->set('iCurrentPoolShareRate', $iCurrentPoolShareRate, 90); } $aGlobal = array( @@ -53,9 +57,18 @@ $aGlobal['userdata'] = $_SESSION['USERDATA']['id'] ? $user->getUserData($_SESSIO $aGlobal['userdata']['balance'] = $transaction->getBalance($_SESSION['USERDATA']['id']); // Other userdata that we can cache savely +if (!$aGlobal['userdata']['shares'] = $memcache->get('global_' . $_SESSION['USERDATA']['id'] . '_shares')) { + $debug->append('STA Loading user shares from database'); + $aGlobal['userdata']['shares'] = $statistics->getUserShares($_SESSION['USERDATA']['id']); + $debug->append('END Loading user shares from database'); + $memcache->set('global_' . $_SESSION['USERDATA']['id'] . '_shares', $aGlobal['userdata']['shares'], 80); +} + if (!$aGlobal['userdata']['hashrate'] = $memcache->get('global_' . $_SESSION['USERDATA']['id'] . '_hashrate') ) { + $debug->append('STA Loading user hashrate from database'); $aGlobal['userdata']['hashrate'] = $statistics->getUserHashrate($_SESSION['USERDATA']['id']); - $memcache->set('global_' . $_SESSION['USERDATA']['id'] . '_hashrate', $aGlobal['userdata']['hashrate'], 60); + $debug->append('END Loading user hashrate from database'); + $memcache->set('global_' . $_SESSION['USERDATA']['id'] . '_hashrate', $aGlobal['userdata']['hashrate'], 70); } // Make it available in Smarty diff --git a/public/templates/mmcFE/global/sidebar.tpl b/public/templates/mmcFE/global/sidebar.tpl index 269f7dbc..fbe95818 100644 --- a/public/templates/mmcFE/global/sidebar.tpl +++ b/public/templates/mmcFE/global/sidebar.tpl @@ -9,15 +9,15 @@ Your Current Hashrate
{$GLOBAL.userdata.hashrate} KH/s

Unpaid Shares
- Your Valid: {$GLOBAL.userdata.valid}
+ Your Valid: {$GLOBAL.userdata.shares.valid}
Pool Valid: {$GLOBAL.roundshares.valid}

Round Shares
Pool Valid: {$GLOBAL.roundshares.valid}
Pool Inalid: {$GLOBAL.roundshares.invalid}
- Your Invalid: {$GLOBAL.userdata.invalid}

+ Your Invalid: {$GLOBAL.userdata.shares.invalid}

Round Estimate
- {math equation="round(( x / y ) * z, 8)" x=$GLOBAL.userdata.valid y=$GLOBAL.roundshares.valid z=$GLOBAL.reward} LTC

- Account Balance
{$GLOBAL.userdata.balance} LTC

+ {math equation="round(( x / y ) * z, 8)" x=$GLOBAL.userdata.shares.valid y=$GLOBAL.roundshares.valid z=$GLOBAL.reward} LTC

+ Account Balance
{$GLOBAL.userdata.balance|default:"0"} LTC