Adding shares archive table back in for hash calcs

If the shares archive table is used via config option, those shares are
included when calculating the users hashrate. This will help on very
short rounds where each round end deletes all shares and users will not
be able to track their hashrates. They will be very jumpy and change to
0 all the time.

Still missing this implementation for the hourly hashrate function to
ensure the graphs are always kept up-to-date.

Addresses #331
This commit is contained in:
Sebastian Grewe 2013-07-02 14:02:13 +02:00
parent c8f662f91d
commit 306162a1da

View File

@ -99,8 +99,19 @@ class Statistics {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data; if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT ROUND(COUNT(id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) SELECT
"); (
(
SELECT ROUND(COUNT(id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate
FROM " . $this->share->getTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE)
) + (
SELECT ROUND(COUNT(id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate
FROM " . $this->share->getArchiveTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE)
)
) AS hashrate
FROM DUAL");
// Catchall // Catchall
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->hashrate); if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->hashrate);
$this->debug->append("Failed to get hashrate: " . $this->mysqli->error); $this->debug->append("Failed to get hashrate: " . $this->mysqli->error);
@ -116,8 +127,9 @@ class Statistics {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data; if ($data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT ROUND(COUNT(id) / 600, 2) AS sharerate FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) SELECT ROUND(COUNT(id) / 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); if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->sharerate);
// Catchall // Catchall
$this->debug->append("Failed to fetch share rate: " . $this->mysqli->error); $this->debug->append("Failed to fetch share rate: " . $this->mysqli->error);
@ -161,9 +173,8 @@ class Statistics {
u.id AS id, u.id AS id,
u.username AS username u.username AS username
FROM " . $this->share->getTableName() . " AS s, FROM " . $this->share->getTableName() . " AS s,
" . $this->user->getTableName() . " AS u " . $this->user->getTableName() . " AS u
WHERE WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 )
u.username = SUBSTRING_INDEX( s.username, '.', 1 )
AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0) AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0)
GROUP BY u.id"); GROUP BY u.id");
if ($stmt && $stmt->execute() && $result = $stmt->get_result()) if ($stmt && $stmt->execute() && $result = $stmt->get_result())
@ -186,7 +197,7 @@ class Statistics {
SUM(IF(our_result='Y', 1, 0)) AS valid, SUM(IF(our_result='Y', 1, 0)) AS valid,
SUM(IF(our_result='N', 1, 0)) AS invalid SUM(IF(our_result='N', 1, 0)) AS invalid
FROM " . $this->share->getTableName() . " AS s, FROM " . $this->share->getTableName() . " AS s,
" . $this->user->getTableName() . " AS u " . $this->user->getTableName() . " AS u
WHERE WHERE
u.username = SUBSTRING_INDEX( s.username, '.', 1 ) u.username = SUBSTRING_INDEX( s.username, '.', 1 )
AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0) AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0)
@ -236,13 +247,24 @@ class Statistics {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate SELECT
FROM " . $this->share->getTableName() . " AS s, (
" . $this->user->getTableName() . " AS u SELECT ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ") / 600 / 1000) AS hashrate
WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) FROM " . $this->share->getTableName() . " AS s,
AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE) " . $this->user->getTableName() . " AS u
AND u.id = ?"); WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 )
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE)
AND u.id = ?
) + (
SELECT ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ") / 600 / 1000) AS hashrate
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 10 MINUTE)
AND u.id = ?
) AS hashrate
FROM DUAL");
if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $result = $stmt->get_result() )
return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->hashrate); return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->hashrate);
// Catchall // Catchall
$this->debug->append("Failed to fetch hashrate: " . $this->mysqli->error); $this->debug->append("Failed to fetch hashrate: " . $this->mysqli->error);
@ -306,8 +328,8 @@ class Statistics {
case 'shares': case 'shares':
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT SELECT
COUNT(id) AS shares, COUNT(id) AS shares,
SUBSTRING_INDEX( username, '.', 1 ) AS account SUBSTRING_INDEX( username, '.', 1 ) AS account
FROM " . $this->share->getTableName() . " FROM " . $this->share->getTableName() . "
WHERE our_result = 'Y' WHERE our_result = 'Y'
GROUP BY account GROUP BY account
@ -324,9 +346,12 @@ class Statistics {
SELECT SELECT
ROUND(COUNT(id) * POW(2," . $this->config['difficulty'] . ")/600/1000,2) AS hashrate, ROUND(COUNT(id) * POW(2," . $this->config['difficulty'] . ")/600/1000,2) AS hashrate,
SUBSTRING_INDEX( username, '.', 1 ) AS account SUBSTRING_INDEX( username, '.', 1 ) AS account
FROM " . $this->share->getTableName() . " FROM
WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) (
AND our_result = 'Y' SELECT id, username FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND our_result = 'Y'
UNION
SELECT id, username FROM " . $this->share->getArchiveTableName() ." WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND our_result = 'Y'
) AS t1
GROUP BY account GROUP BY account
ORDER BY hashrate DESC LIMIT ?"); ORDER BY hashrate DESC LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result())
@ -347,15 +372,14 @@ class Statistics {
if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT SELECT
ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ") / 3600 / 1000) AS hashrate, ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ") / 3600 / 1000) AS hashrate,
HOUR(s.time) AS hour HOUR(s.time) AS hour
FROM " . $this->share->getTableName() . " AS s, accounts AS a FROM " . $this->share->getTableName() . " AS s, accounts AS a
WHERE time < NOW() - INTERVAL 1 HOUR WHERE time < NOW() - INTERVAL 1 HOUR
AND time > NOW() - INTERVAL 25 HOUR AND time > NOW() - INTERVAL 25 HOUR
AND a.username = SUBSTRING_INDEX( s.username, '.', 1 ) AND a.username = SUBSTRING_INDEX( s.username, '.', 1 )
AND a.id = ? AND a.id = ?
GROUP BY HOUR(time) GROUP BY HOUR(time)");
");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result()) { if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result()) {
$aData = array(); $aData = array();
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
@ -378,13 +402,13 @@ class Statistics {
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data; if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT SELECT
ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ") / 3600 / 1000) AS hashrate, ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ") / 3600 / 1000) AS hashrate,
HOUR(s.time) AS hour HOUR(s.time) AS hour
FROM " . $this->share->getTableName() . " AS s FROM " . $this->share->getTableName() . " AS s
WHERE time < NOW() - INTERVAL 1 HOUR WHERE time < NOW() - INTERVAL 1 HOUR
AND time > NOW() - INTERVAL 25 HOUR AND time > NOW() - INTERVAL 25 HOUR
GROUP BY HOUR(time) GROUP BY HOUR(time)
"); ");
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) { if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) {
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
$aData[$row['hour']] = $row['hashrate']; $aData[$row['hour']] = $row['hashrate'];