From a45140d3e0bf8a1987e1dcf2c853e815dfd13360 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 16 May 2013 14:56:08 +0200 Subject: [PATCH] moved more stats to stats class --- public/include/classes/statistics.class.php | 74 +++++++++++++++----- public/include/pages/statistics/pool.inc.php | 30 +------- 2 files changed, 61 insertions(+), 43 deletions(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 5eae4cff..41803133 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -8,12 +8,13 @@ class Statistics { private $sError = ''; private $table = 'statistics_shares'; - public function __construct($debug, $mysqli, $config, $share, $user) { + public function __construct($debug, $mysqli, $config, $share, $user, $block) { $this->debug = $debug; $this->mysqli = $mysqli; $this->share = $share; $this->config = $config; $this->user = $user; + $this->block = $block; $this->debug->append("Instantiated Share class", 2); } @@ -34,6 +35,19 @@ class Statistics { return true; } + public function getBlocksFound($limit=10) { + $stmt = $this->mysqli->prepare(" + SELECT b.*, a.username as finder + FROM " . $this->block->getTableName() . " AS b + LEFT JOIN accounts AS a + ON b.account_id = a.id + ORDER BY height DESC LIMIT ?"); + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_all(MYSQLI_ASSOC); + // Catchall + $this->debug->append("Failed to find blocks:" . $this->mysqli->error); + return false; + } public function updateShareStatistics($aStats, $iBlockId) { $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, block_id) VALUES (?, ?, ?, ?)"); if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId) && $stmt->execute()) return true; @@ -76,11 +90,11 @@ class Statistics { ( SELECT IFNULL(count(id), 0) FROM " . $this->share->getTableName() . " WHERE UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(time) FROM blocks),0) - AND our_result = 'Y' ) as valid, + AND our_result = 'Y' ) as valid, ( SELECT IFNULL(count(id), 0) FROM " . $this->share->getTableName() . " WHERE UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(time) FROM blocks),0) - AND our_result = 'N' ) as invalid"); + AND our_result = 'N' ) as invalid"); if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_assoc(); // Catchall $this->debug->append("Failed to fetch round shares: " . $this->mysqli->error); @@ -92,19 +106,21 @@ class Statistics { SELECT ( SELECT COUNT(s.id) - FROM " . $this->share->getTableName() . " AS s, " . $this->user->getTableName() . " AS u + 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 = ? + 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 + 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 = ? + 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 @@ -116,8 +132,8 @@ class Statistics { $stmt = $this->mysqli->prepare(" SELECT ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate FROM " . $this->share->getTableName() . " AS s, - " . $this->user->getTableName() . " AS u - WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) + " . $this->user->getTableName() . " AS u + WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND u.id = ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate; @@ -130,8 +146,8 @@ class Statistics { $stmt = $this->mysqli->prepare(" SELECT ROUND(COUNT(s.id) * POW(2,21)/600/1000) AS hashrate FROM " . $this->share->getTableName() . " AS s, - " . $this->user->getTableName() . " AS u - WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) + " . $this->user->getTableName() . " AS u + WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND u.id = ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate; @@ -140,6 +156,32 @@ class Statistics { return false; } + public function getTopContributors($limit=15) { + $stmt = $this->mysqli->prepare(" + SELECT + ROUND(COUNT(id) * POW(2," . $this->config['difficulty'] . ")/600/1000,2) AS hashrate, + SUBSTRING_INDEX( username, '.', 1 ) AS account + FROM " . $this->share->getTableName() . " + WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) + GROUP BY account + ORDER BY hashrate DESC LIMIT ?"); + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $hashrates= $stmt->get_result()) { + $aHashData = $hashrates->fetch_all(MYSQLI_ASSOC); + $stmt->close(); + } else { + return false; + } + foreach ($aHashData as $key => $aData) { + $stmt = $this->mysqli->prepare("SELECT COUNT(id) FROM " . $this->share->getTableName() . " WHERE SUBSTRING_INDEX( username , '.', 1 ) = ?"); + if ($stmt->bind_param("s", $aData['username']) && $stmt->execute() && $result = $stmt->get_result()) { + $aHashData[$key]['shares'] = $this->getUserShares($this->user->getUserId($aData['account']))['valid']; + } else { + continue; + } + } + return $aHashData; + } + public function getHourlyHashrateByAccount($account_id) { $stmt = $this->mysqli->prepare(" SELECT @@ -166,4 +208,4 @@ class Statistics { return false; } } -$statistics = new Statistics($debug, $mysqli, $config, $share, $user); +$statistics = new Statistics($debug, $mysqli, $config, $share, $user, $block); diff --git a/public/include/pages/statistics/pool.inc.php b/public/include/pages/statistics/pool.inc.php index 822eb3c0..005e167d 100644 --- a/public/include/pages/statistics/pool.inc.php +++ b/public/include/pages/statistics/pool.inc.php @@ -22,37 +22,14 @@ if ($bitcoin->can_connect() === true){ if (!$aHashData = $memcache->get('aHashData')) { $debug->append('STA Fetching Hashrates from database'); - // Top 15 hashrate list - $stmt = $mysqli->prepare(" - SELECT - COUNT(id) AS shares, - ROUND(COUNT(id) * POW(2," . $config['difficulty'] . ")/600/1000,2) AS hashrate, - SUBSTRING_INDEX( `username` , '.', 1 ) AS account - FROM shares - WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) - GROUP BY account - ORDER BY hashrate DESC LIMIT 15"); - $stmt->execute(); - $hashrates= $stmt->get_result(); - $aHashData = $hashrates->fetch_all(MYSQLI_ASSOC); - $stmt->close(); + $aHashData = $statistics->getTopContributors(); $memcache->set('aHashData', $aHashData, 60); $debug->append('END Fetching Hashrates from database'); } -// Grab the last block found -$stmt = $mysqli->prepare("SELECT * FROM blocks ORDER BY height DESC LIMIT 1"); -$stmt->execute(); -$blocks = $stmt->get_result(); -$aBlockData = $blocks->fetch_array(); -$stmt->close(); - // Grab the last 10 blocks found -$stmt = $mysqli->prepare("SELECT b.*, a.username as finder FROM blocks AS b LEFT JOIN accounts AS a ON b.account_id = a.id ORDER BY height DESC LIMIT 10"); -$stmt->execute(); -$blocksfound = $stmt->get_result(); -$aBlocksFoundData = $blocksfound->fetch_all(MYSQLI_ASSOC); -$stmt->close(); +$aBlocksFoundData = $statistics->getBlocksFound(10); +$aBlockData = $aBlocksFoundData[0]; // Estimated time to find the next block if (!$iCurrentPoolHashrate = $memcache->get('iCurrentPoolHashrate')) { @@ -74,7 +51,6 @@ if (!empty($aBlockData)) { // Propagate content our template $smarty->assign("ESTTIME", $iEstTime); $smarty->assign("TIMESINCELAST", $dTimeSinceLast); -$smarty->assign("CONTRIBUTORS", $aContributerData); $smarty->assign("BLOCKSFOUND", $aBlocksFoundData); $smarty->assign("TOPHASHRATES", $aHashData); $smarty->assign("CURRENTBLOCK", $iBlock);