diff --git a/cronjobs/statistics.php b/cronjobs/statistics.php index 9228095b..0149afc0 100755 --- a/cronjobs/statistics.php +++ b/cronjobs/statistics.php @@ -27,10 +27,16 @@ require_once('shared.inc.php'); // Per user share statistics based on all shares submitted $start = microtime(true); -if ( ! $aAllUserShares = $statistics->getAllUserShares() ) +if ( ! $statistics->getAllUserShares() ) $log->logError('getAllUserShares update failed'); $log->logInfo("getAllUserShares " . number_format(microtime(true) - $start, 2) . " seconds"); +// Get all user hashrate statistics for caching +$start = microtime(true); +if ( ! $statistics->getAllUserMiningStats() ) + $log->logError('getAllUserMiningStats update failed'); +$log->logInfo("getAllUserMiningStats " . number_format(microtime(true) - $start, 2) . " seconds"); + $start = microtime(true); if (!$statistics->getTopContributors('hashes')) $log->logError("getTopContributors hashes update failed"); @@ -41,13 +47,5 @@ if (!$statistics->getCurrentHashrate()) $log->logError("getCurrentHashrate update failed"); $log->logInfo("getCurrentHashrate " . number_format(microtime(true) - $start, 2) . " seconds"); -/* -// Admin specific statistics, we cache the global query due to slowness -$start = microtime(true); -if (!$statistics->getAllUserStats('%')) - $log->logError("getAllUserStats update failed"); -$log->logInfo("getAllUserStats " . number_format(microtime(true) - $start, 2) . " seconds"); -*/ - require_once('cron_end.inc.php'); ?> diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 4b369931..327614d9 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -448,6 +448,51 @@ class Statistics extends Base { return $this->sqlError(); } + /** + * Fetch all user hashrates based on shares and archived shares + * @return data integer Current Hashrate in khash/s + **/ + public function getAllUserMiningStats($interval=600) { + $this->debug->append("STA " . __METHOD__, 4); + $stmt = $this->mysqli->prepare(" + SELECT + a.id AS id, + a.username AS account, + IFNULL(ROUND(SUM(t1.difficulty) * POW(2, " . $this->config['target_bits'] . ") / ? / 1000, 2), 0) AS hashrate, + ROUND(COUNT(t1.id) / ?, 2) AS sharerate, + IFNULL(AVG(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) AS avgsharediff + FROM ( + SELECT + id, + IF(difficulty = 0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS difficulty, + username + FROM shares + WHERE time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y' + UNION + SELECT + share_id, + IF(difficulty = 0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS difficulty, + username + FROM shares_archive + WHERE time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y' + ) AS t1 + LEFT JOIN " . $this->user->getTableName() . " AS a + ON SUBSTRING_INDEX( t1.username, '.', 1 ) = a.username + WHERE a.id IS NOT NULL + GROUP BY account + ORDER BY hashrate DESC + "); + if ($this->checkStmt($stmt) && $stmt->bind_param("iiii", $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) { + $aData = array(); + while ($row = $result->fetch_assoc()) { + $aData['data'][$row['id']] = $row; + } + return $this->memcache->setCache(STATISTICS_ALL_USER_HASHRATES, $aData); + } else { + return $this->sqlError(); + } + } + /** * Fetch total user hashrate based on shares and archived shares * @param account_id integer User ID @@ -455,6 +500,13 @@ class Statistics extends Base { **/ public function getUserHashrate($account_id, $interval=600) { $this->debug->append("STA " . __METHOD__, 4); + // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL + if ($data = $this->memcache->get(STATISTICS_ALL_USER_HASHRATES)) { + if (array_key_exists($account_id, $data['data'])) + return $data['data'][$account_id]['hashrate']; + // We have no cached value, we return defaults + return 0; + } if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT @@ -510,6 +562,13 @@ class Statistics extends Base { **/ public function getUserShareDifficulty($account_id, $interval=600) { $this->debug->append("STA " . __METHOD__, 4); + // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL + if ($data = $this->memcache->get(STATISTICS_ALL_USER_HASHRATES)) { + if (array_key_exists($account_id, $data['data'])) + return $data['data'][$account_id]['avgsharediff']; + // We have no cached value, we return defaults + return 0; + } if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT @@ -532,6 +591,13 @@ class Statistics extends Base { **/ public function getUserSharerate($account_id, $interval=600) { $this->debug->append("STA " . __METHOD__, 4); + // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL + if ($data = $this->memcache->get(STATISTICS_ALL_USER_HASHRATES)) { + if (array_key_exists($account_id, $data['data'])) + return $data['data'][$account_id]['sharerate']; + // We have no cached value, we return defaults + return 0; + } if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT diff --git a/public/include/config/memcache_keys.inc.php b/public/include/config/memcache_keys.inc.php index f689a868..f9405b0c 100644 --- a/public/include/config/memcache_keys.inc.php +++ b/public/include/config/memcache_keys.inc.php @@ -4,5 +4,6 @@ if (!defined('SECURITY')) die('Hacking attempt'); define('STATISTICS_ALL_USER_SHARES', 'STATISTICS_ALL_USER_SHARES'); +define('STATISTICS_ALL_USER_HASHRATES', 'STATISTICS_ALL_USER_HASHRATES'); define('STATISTICS_ROUND_SHARES', 'STATISTICS_ROUND_SHARES'); ?>