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

View File

@ -145,6 +145,34 @@ class Statistics {
return false; 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 * Get amount of shares for a specific user
* @param account_id int User ID * @param account_id int User ID