Merge pull request #255 from TheSerapher/issue-246-fetch-all-users

Fetch all user shares in one query
This commit is contained in:
Sebastian Grewe 2013-06-26 01:55:27 -07:00
commit c8002165c2
2 changed files with 49 additions and 14 deletions

View File

@ -30,32 +30,39 @@ verbose("Running statistical cache updates\n");
// Since fetching from cache is disabled, overwrite our stats
verbose(" getRoundShares ...");
$start = microtime(true);
if (!$statistics->getRoundShares())
verbose(" update failed");
verbose("\n getTopContributors shares ...");
verbose(" " . number_format(microtime(true) - $start, 2) . " seconds\n");
verbose(" getTopContributors shares ...");
$start = microtime(true);
if (!$statistics->getTopContributors('shares'))
verbose(" update failed");
verbose("\n getTopContributors hashes ...");
verbose(" " . number_format(microtime(true) - $start, 2) . " seconds\n");
verbose(" getTopContributors hashes ...");
$start = microtime(true);
if (!$statistics->getTopContributors('hashes'))
verbose(" update failed");
verbose("\n getCurrentHashrate ...");
verbose(" " . number_format(microtime(true) - $start, 2) . " seconds\n");
verbose(" getCurrentHashrate ...");
$start = microtime(true);
if (!$statistics->getCurrentHashrate())
verbose(" update failed");
verbose(" " . number_format(microtime(true) - $start, 2) . " seconds\n");
// Admin specific statistics, we cache the global query due to slowness
verbose("\n getAllUserStats ...");
verbose(" getAllUserStats ...");
$start = microtime(true);
if (!$statistics->getAllUserStats('%'))
verbose(" update failed");
verbose("\n");
verbose(" " . number_format(microtime(true) - $start, 2) . " seconds\n");
// Per user share statistics based on all shares submitted
verbose(" getUserShares ...\n");
$stmt = $mysqli->prepare("SELECT DISTINCT SUBSTRING_INDEX( `username` , '.', 1 ) AS username FROM " . $share->getTableName());
if ($stmt && $stmt->execute() && $result = $stmt->get_result()) {
while ($row = $result->fetch_assoc()) {
verbose(" " . $row['username'] . " ...");
if (!$statistics->getUserShares($user->getUserId($row['username'])))
verbose(" update failed");
verbose("\n");
}
verbose(" getAllUserShares ...");
$start = microtime(true);
$aUserShares = $statistics->getAllUserShares();
verbose(" " . number_format(microtime(true) - $start, 2) . " seconds");
foreach ($aUserShares as $aShares) {
$memcache->setCache('getUserShares'. $aShares['id'], $aShares);
}
verbose("\n");
?>

View File

@ -145,6 +145,34 @@ class Statistics {
return false;
}
/**
* Get amount of shares for a all users
* Used in statistics cron to refresh memcache data
* @param account_id int User ID
* @return data array invalid and valid share counts
**/
public function getAllUserShares() {
$this->debug->append("STA " . __METHOD__, 4);
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
SUM(IF(our_result='Y', 1, 0)) AS valid,
SUM(IF(our_result='N', 1, 0)) AS invalid,
u.id AS id,
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));
// Catchall
$this->debug->append("Unable to fetch all users round shares: " . $this->mysqli->error);
return false;
}
/**
* Get amount of shares for a specific user
* @param account_id int User ID