cleaned up statistics, added some user specifc stats from user class to statistics class

This commit is contained in:
Sebastian Grewe 2013-05-14 23:57:22 +02:00
parent 1534b53686
commit 9b961e04cf

View File

@ -7,14 +7,13 @@ if (!defined('SECURITY'))
class Statistics { class Statistics {
private $sError = ''; private $sError = '';
private $table = 'statistics_shares'; private $table = 'statistics_shares';
// This defines each statistic
public $valid, $invalid, $block, $user;
public function __construct($debug, $mysqli, $config, $share) { public function __construct($debug, $mysqli, $config, $share, $user) {
$this->debug = $debug; $this->debug = $debug;
$this->mysqli = $mysqli; $this->mysqli = $mysqli;
$this->share = $share; $this->share = $share;
$this->config = $config; $this->config = $config;
$this->user = $user;
$this->debug->append("Instantiated Share class", 2); $this->debug->append("Instantiated Share class", 2);
} }
@ -26,14 +25,20 @@ class Statistics {
return $this->sError; return $this->sError;
} }
private function checkStmt($bState) {
if ($bState ===! true) {
$this->debug->append("Failed to prepare statement: " . $this->mysqli->error);
$this->setErrorMessage('Failed to prepare statement');
return false;
}
return true;
}
public function updateShareStatistics($aStats, $iBlockId) { public function updateShareStatistics($aStats, $iBlockId) {
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, block_id) VALUES (?, ?, ?, ?)"); $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, block_id) VALUES (?, ?, ?, ?)");
if ($this->checkStmt($stmt)) { if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId) && $stmt->execute()) return true;
$stmt->bind_param('iiii', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId); // Catchall
if ($stmt->execute()) { $this->debug->append("Failed to update share stats: " . $this->mysqli->error);
return true;
}
}
return false; return false;
} }
@ -44,11 +49,10 @@ class Statistics {
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->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE)
UNION UNION
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) 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 sum ) AS sum");
"); // Catchall
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) { if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate;
return $result->fetch_object()->hashrate; $this->debug->append("Failed to get hashrate: " . $this->mysqli->error);
}
return false; return false;
} }
@ -59,11 +63,10 @@ class Statistics {
SELECT COUNT(id) AS sharerate FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) SELECT COUNT(id) AS sharerate FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE)
UNION ALL UNION ALL
SELECT COUNT(id) AS sharerate FROM " . $this->share->getArchiveTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) SELECT COUNT(id) AS sharerate FROM " . $this->share->getArchiveTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE)
) AS sum ) AS sum");
"); if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->sharerate;
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) { // Catchall
return $result->fetch_object()->sharerate; $this->debug->append("Failed to fetch share rate: " . $this->mysqli->error);
}
return false; return false;
} }
@ -77,26 +80,40 @@ class Statistics {
( SELECT IFNULL(count(id), 0) ( SELECT IFNULL(count(id), 0)
FROM " . $this->share->getTableName() . " FROM " . $this->share->getTableName() . "
WHERE UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(time) FROM blocks),0) WHERE UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(time) FROM blocks),0)
AND our_result = 'N' ) as invalid AND our_result = 'N' ) as invalid");
"); if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_assoc();
if ($this->checkStmt($stmt)) { // Catchall
$this->debug->append("Failed to fetch round shares: " . $this->mysqli->error);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
return $result->fetch_assoc();
}
return false; return false;
} }
private function checkStmt($bState) { public function getUserHashrate($account_id) {
if ($bState ===! true) { $stmt = $this->mysqli->prepare("
$this->debug->append("Failed to prepare statement: " . $this->mysqli->error); SELECT ROUND(COUNT(s.id) * POW(2,21)/600/1000) AS hashrate
$this->setErrorMessage('Failed to prepare statement'); FROM " . $this->share->getTableName() . " AS s,
return false; " . $this->user->getTableName() . " AS u
} WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 )
return true; AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE)
AND u.id = ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate;
// Catchall
$this->debug->append("Failed to fetch hashrate: " . $this->mysqli->error);
return false;
}
public function getWorkerHashrate($worker_id) {
$stmt = $this->mysqli->prepare("
SELECT ROUND(COUNT(s.id) * POW(2,21)/600/1000) AS hashrate
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 10 MINUTE)
AND u.id = ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate;
// Catchall
$this->debug->append("Failed to fetch hashrate: " . $this->mysqli->error);
return false;
} }
} }
$statistics = new Statistics($debug, $mysqli, $config, $share); $statistics = new Statistics($debug, $mysqli, $config, $share, $user);