Skip to content

Commit

Permalink
[MERGE] Merge conflict NEXT
Browse files Browse the repository at this point in the history
  • Loading branch information
MPOS123 committed Sep 18, 2013
2 parents 04786aa + d863e66 commit 1387bbd
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 78 deletions.
26 changes: 7 additions & 19 deletions cronjobs/statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,22 @@
// Include all settings and classes
require_once('shared.inc.php');

// Fetch all cachable values but disable fetching from cache
$statistics->setGetCache(false);

// Since fetching from cache is disabled, overwrite our stats
$start = microtime(true);
if (!$statistics->getRoundShares())
$log->logError("getRoundShares update failed");
$log->logInfo("getRoundShares update " . number_format(microtime(true) - $start, 2) . " seconds");
// Per user share statistics based on all shares submitted
$start = microtime(true);
if (!$statistics->getTopContributors('shares'))
$log->logError("getTopContributors shares update failed");
$log->logInfo("getTopContributors shares " . number_format(microtime(true) - $start, 2) . " seconds");
if ( ! $aAllUserShares = $statistics->getAllUserShares() )
$log->logError('getAllUserShares update failed');
$log->logInfo("getAllUserShares " . number_format(microtime(true) - $start, 2) . " seconds");

$start = microtime(true);
if (!$statistics->getTopContributors('hashes'))
$log->logError("getTopContributors hashes update failed");
$log->logInfo("getTopContributors hashes " . number_format(microtime(true) - $start, 2) . " seconds");

$start = microtime(true);
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);
Expand All @@ -50,13 +46,5 @@
$log->logInfo("getAllUserStats " . number_format(microtime(true) - $start, 2) . " seconds");
*/

// Per user share statistics based on all shares submitted
$start = microtime(true);
$aUserShares = $statistics->getAllUserShares();
$log->logInfo("getAllUserShares " . number_format(microtime(true) - $start, 2) . " seconds");
foreach ($aUserShares as $aShares) {
$memcache->setCache('getUserShares'. $aShares['id'], $aShares);
}

require_once('cron_end.inc.php');
?>
10 changes: 7 additions & 3 deletions public/include/autoloader.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

// Default classes
require_once(CLASS_DIR . '/debug.class.php');
require_once(CLASS_DIR . '/bitcoin.class.php');
require_once(CLASS_DIR . '/statscache.class.php');
require_once(CLASS_DIR . '/bitcoinwrapper.class.php');
require_once(INCLUDE_DIR . '/lib/KLogger.php');
require_once(INCLUDE_DIR . '/database.inc.php');
require_once(INCLUDE_DIR . '/config/memcache_keys.inc.php');

// We need to load these two first
require_once(CLASS_DIR . '/base.class.php');
Expand Down Expand Up @@ -35,6 +33,12 @@
require_once(CLASS_DIR . '/token.class.php');
require_once(CLASS_DIR . '/payout.class.php');
require_once(CLASS_DIR . '/block.class.php');

// We require the block class to properly grab the round ID
require_once(CLASS_DIR . '/statscache.class.php');

require_once(CLASS_DIR . '/bitcoin.class.php');
require_once(CLASS_DIR . '/bitcoinwrapper.class.php');
require_once(CLASS_DIR . '/monitoring.class.php');
require_once(CLASS_DIR . '/user.class.php');
require_once(CLASS_DIR . '/invitation.class.php');
Expand Down
21 changes: 17 additions & 4 deletions public/include/classes/bitcoinwrapper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,32 @@ public function __construct($type, $username, $password, $host, $debug, $memcach
public function getblockcount() {
$this->oDebug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
return $this->memcache->setCache(__FUNCTION__, parent::getblockcount());
return $this->memcache->setCache(__FUNCTION__, parent::getblockcount(), 30);
}
public function getdifficulty() {
$this->oDebug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
return $this->memcache->setCache(__FUNCTION__, parent::getdifficulty());
$data = parent::getdifficulty();
// Check for PoS/PoW coins
if (is_array($data) && array_key_exists('proof-of-work', $data))
$data = $data['proof-of-work'];
return $this->memcache->setCache(__FUNCTION__, $data, 30);
}
public function getestimatedtime($iCurrentPoolHashrate) {
$this->oDebug->append("STA " . __METHOD__, 4);
if ($iCurrentPoolHashrate == 0) return 0;
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
$dDifficulty = parent::getdifficulty();
return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate);
$dDifficulty = $this->getdifficulty();
return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate, 30);
}
public function getnetworkhashps() {
$this->oDebug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
try { $dNetworkHashrate = $this->query('getnetworkhashps') / 1000; } catch (Exception $e) {
// Maybe we are SHA
try { $dNetworkHashrate = $this->query('gethashespersec') / 1000; } catch (Exception $e) { return false; }
}
return $this->memcache->setCache(__FUNCTION__, $dNetworkHashrate, 30);
}
}

Expand Down
10 changes: 10 additions & 0 deletions public/include/classes/share.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public function getSharesForAccounts($previous_upstream=0, $current_upstream) {
return false;
}

/**
* Fetch the highest available share ID
**/
function getMaxShareId() {
$stmt = $this->mysqli->prepare("SELECT MAX(id) AS id FROM $this->table");
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_object()->id;
return false;
}

/**
* Fetch the highest available share ID from archive
**/
Expand Down
114 changes: 94 additions & 20 deletions public/include/classes/statistics.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,24 @@ public function getCurrentHashrate() {
* @param none
* @return data object Our share rate in shares per second
**/
public function getCurrentShareRate() {
public function getCurrentShareRate($interval=600) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT ROUND(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)) / POW(2, (" . $this->config['difficulty'] . " - 16)) / 600, 2) AS sharerate
FROM " . $this->share->getTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE)");
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->sharerate);
SELECT
(
(
SELECT ROUND(COUNT(id) / ?, 2) AS sharerate
FROM " . $this->share->getTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL ? SECOND)
) + (
SELECT ROUND(COUNT(id) / ?, 2) AS sharerate
FROM " . $this->share->getArchiveTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL ? SECOND)
)
) AS sharerate
FROM DUAL");
if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->sharerate);
// Catchall
$this->debug->append("Failed to fetch share rate: " . $this->mysqli->error);
return false;
Expand All @@ -147,15 +157,28 @@ public function getCurrentShareRate() {
**/
public function getRoundShares() {
$this->debug->append("STA " . __METHOD__, 4);
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data;
// Try the statistics cron cache, then function cache, then fallback to SQL
if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) {
$this->debug->append("Found data in statistics cache", 2);
$total = array('valid' => 0, 'invalid' => 0);
foreach ($data['data'] as $aUser) {
$total['valid'] += $aUser['valid'];
$total['invalid'] += $aUser['invalid'];
}
return $total;
}
if ($data = $this->memcache->get(STATISTICS_ROUND_SHARES)) {
$this->debug->append("Found data in local cache", 2);
return $data;
}
$stmt = $this->mysqli->prepare("
SELECT
ROUND(IFNULL(SUM(IF(our_result='Y', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS valid,
ROUND(IFNULL(SUM(IF(our_result='N', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS invalid
FROM " . $this->share->getTableName() . "
WHERE UNIX_TIMESTAMP(time) > IFNULL((SELECT MAX(time) FROM " . $this->block->getTableName() . "), 0)");
if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() )
return $this->memcache->setCache(__FUNCTION__, $result->fetch_assoc());
return $this->memcache->setCache(STATISTICS_ROUND_SHARES, $result->fetch_assoc());
// Catchall
$this->debug->append("Failed to fetch round shares: " . $this->mysqli->error);
return false;
Expand All @@ -169,20 +192,41 @@ public function getRoundShares() {
**/
public function getAllUserShares() {
$this->debug->append("STA " . __METHOD__, 4);
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data;
if (! $data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) {
$data['share_id'] = 0;
$data['data'] = array();
}
$stmt = $this->mysqli->prepare("
SELECT
ROUND(IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS valid,
ROUND(IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS invalid,
u.id AS id,
u.donate_percent AS donate_percent,
u.is_anonymous AS is_anonymous,
u.username AS username
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 " . $this->block->getTableName() . " AS b),0)
GROUP BY u.id");
if ($stmt && $stmt->execute() && $result = $stmt->get_result())
return $this->memcache->setCache(__FUNCTION__, $result->fetch_all(MYSQLI_ASSOC));
AND UNIX_TIMESTAMP(s.time) > IFNULL(
(
SELECT MAX(b.time)
FROM " . $this->block->getTableName() . " AS b
) ,0 )
AND s.id > ?
GROUP BY u.id");
if ($stmt && $stmt->bind_param('i', $data['share_id']) && $stmt->execute() && $result = $stmt->get_result()) {
$data_new = array();
while ($row = $result->fetch_assoc()) {
if (! array_key_exists($row['id'], $data['data'])) {
$data['data'][$row['id']] = $row;
} else {
$data['data'][$row['id']]['valid'] += $row['valid'];
$data['data'][$row['id']]['invalid'] += $row['invalid'];
}
}
$data['share_id'] = $this->share->getMaxShareId();
return $this->memcache->setCache(STATISTICS_ALL_USER_SHARES, $data);
}
// Catchall
$this->debug->append("Unable to fetch all users round shares: " . $this->mysqli->error);
return false;
Expand All @@ -195,7 +239,9 @@ public function getAllUserShares() {
**/
public function getUserShares($account_id) {
$this->debug->append("STA " . __METHOD__, 4);
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
// Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL
if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) return $data['data'][$account_id];
if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
ROUND(IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) / POW(2, (" . $this->config['difficulty'] . " - 16)), 0) AS valid,
Expand Down Expand Up @@ -284,13 +330,26 @@ public function getUserSharerate($account_id) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare("
SELECT COUNT(s.id) / 600 AS sharerate
FROM " . $this->share->getTableName() . " AS s,
" . $this->user->getTableName() . " AS u
WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 )
AND s.time > DATE_SUB(now(), INTERVAL 600 SECOND)
AND u.id = ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() )
SELECT
(
(
SELECT COUNT(s.id) / ? AS sharerate
FROM " . $this->share->getTableName() . " AS s,
" . $this->user->getTableName() . " AS u
WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 )
AND s.time > DATE_SUB(now(), INTERVAL ? SECOND)
AND u.id = ?
) + (
SELECT COUNT(s.id) / ? AS sharerate
FROM " . $this->share->getArchiveTableName() . " AS s,
" . $this->user->getTableName() . " AS u
WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 )
AND s.time > DATE_SUB(now(), INTERVAL ? SECOND)
AND u.id = ?
)
) AS sharerate
FROM DUAL");
if ($this->checkStmt($stmt) && $stmt->bind_param("iiiiii", $interval, $interval, $account_id, $interval, $interval, $account_id) && $stmt->execute() && $result = $stmt->get_result() )
return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->sharerate);
// Catchall
$this->debug->append("Failed to fetch sharerate: " . $this->mysqli->error);
Expand Down Expand Up @@ -330,6 +389,21 @@ public function getTopContributors($type='shares', $limit=15) {
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $type . $limit)) return $data;
switch ($type) {
case 'shares':
if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) {
// Use global cache to build data
$max = 0;
foreach($data['data'] as $key => $aUser) {
$shares[$key] = $aUser['valid'];
$username[$key] = $aUser['username'];
}
array_multisort($shares, SORT_DESC, $username, SORT_ASC, $data['data']);
foreach ($data['data'] as $key => $aUser) {
$data_new[$key]['shares'] = $aUser['valid'];
$data_new[$key]['account'] = $aUser['username'];
}
return $data_new;
}
// No cached data, fallback to SQL and cache in local cache
$stmt = $this->mysqli->prepare("
SELECT
a.donate_percent AS donate_percent,
Expand Down
28 changes: 21 additions & 7 deletions public/include/classes/statscache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* Also sets a default time if no time is passed to it to enforce caching
**/
class StatsCache {
private $cache;
private $cache, $round;

public function __construct($config, $debug) {
$this->config = $config;
$this->debug = $debug;
Expand All @@ -22,6 +22,13 @@ public function __construct($config, $debug) {
}
}

public function setRound($round_id) {
$this->round = $round_id;
}
public function getRound() {
return $this->round;
}

/**
* Wrapper around memcache->set
* Do not store values if memcache is disabled
Expand All @@ -30,8 +37,8 @@ public function set($key, $value, $expiration=NULL) {
if (! $this->config['memcache']['enabled']) return false;
if (empty($expiration))
$expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']);
$this->debug->append("Storing " . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3);
return $this->cache->set($this->config['memcache']['keyprefix'] . $key, $value, $expiration);
$this->debug->append("Storing " . $this->getRound() . '_' . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3);
return $this->cache->set($this->getRound() . '_' . $this->config['memcache']['keyprefix'] . $key, $value, $expiration);
}

/**
Expand All @@ -40,8 +47,8 @@ public function set($key, $value, $expiration=NULL) {
**/
public function get($key, $cache_cb = NULL, &$cas_token = NULL) {
if (! $this->config['memcache']['enabled']) return false;
$this->debug->append("Trying to fetch key " . $this->config['memcache']['keyprefix'] . "$key from cache", 3);
if ($data = $this->cache->get($this->config['memcache']['keyprefix'].$key)) {
$this->debug->append("Trying to fetch key " . $this->getRound() . '_' . $this->config['memcache']['keyprefix'] . "$key from cache", 3);
if ($data = $this->cache->get($this->getRound() . '_' . $this->config['memcache']['keyprefix'].$key)) {
$this->debug->append("Found key in cache", 3);
return $data;
} else {
Expand All @@ -60,7 +67,7 @@ public function setCache($key, $data, $expiration=NULL) {
if ($this->config['memcache']['enabled']) $this->set($key, $data, $expiration);
return $data;
}

/**
* This method is invoked if the called method was not realised in this class
**/
Expand All @@ -73,3 +80,10 @@ public function __call($name, $arguments) {

$memcache = new StatsCache($config, $debug);
$memcache->addServer($config['memcache']['host'], $config['memcache']['port']);
// Now we can set our additional key prefix
if ($aTmpBlock = $block->getLast()) {
$iRoundId = $aTmpBlock['id'];
} else {
$iRoundId = 0;
}
$memcache->setRound($iRoundId);
7 changes: 7 additions & 0 deletions public/include/config/admin_settings.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
'name' => 'website_chaininfo_disabled', 'value' => $setting->getValue('website_chaininfo_disabled'),
'tooltip' => 'Enabled or disable the chainfo URL feature. Will remove any links using the chaininfo URL.'
);
$aSettings['wallet'][] = array(
'display' => 'Cold Coins', 'type' => 'text',
'size' => 6,
'default' => 0,
'name' => 'wallet_cold_coins', 'value' => $setting->getValue('wallet_cold_coins'),
'tooltip' => 'Amount of coins held in a pools cold wallet.'
);
$aSettings['statistics'][] = array(
'display' => 'Block Statistics Count', 'type' => 'text',
'size' => 25,
Expand Down
5 changes: 5 additions & 0 deletions public/include/config/memcache_keys.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

define('STATISTICS_ALL_USER_SHARES', 'STATISTICS_ALL_USER_SHARES');
define('STATISTICS_ROUND_SHARES', 'STATISTICS_ROUND_SHARES');
?>
Loading

0 comments on commit 1387bbd

Please sign in to comment.