Moving from user to statistics class

* This fetches all users and joins with shares table

Should speed up things a fair bit.
This commit is contained in:
Sebastian Grewe 2013-06-04 13:14:25 +02:00
parent 4fa3089655
commit d85ded7c5c
3 changed files with 38 additions and 26 deletions

View File

@ -54,7 +54,7 @@ class Statistics {
$stmt = $this->mysqli->prepare("
SELECT b.*, a.username as finder
FROM " . $this->block->getTableName() . " AS b
LEFT JOIN accounts AS a
LEFT JOIN " . $this->user->getTableName() . " AS a
ON b.account_id = a.id
ORDER BY height DESC LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result())
@ -175,6 +175,34 @@ class Statistics {
return false;
}
/**
* Admin panel specific query
* @return data array invlid and valid shares for all accounts
**/
public function getAllUserStats($filter='%') {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $filter)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
a.id AS id,
a.username AS username,
a.donate_percent AS donate_percent,
a.email AS email,
COUNT(s.id) AS shares,
ROUND(COUNT(s.id) * POW(2," . $this->config['difficulty'] . ") / 600 / 1000,2) AS hashrate
FROM " . $this->user->getTableName() . " AS a
LEFT JOIN " . $this->share->getTableName() . " AS s
ON a.username = SUBSTRING_INDEX( s.username, '.', 1 )
WHERE
a.username LIKE ?
GROUP BY username
ORDER BY username
");
if ($this->checkStmt($stmt) && $stmt->bind_param('s', $filter) && $stmt->execute() && $result = $stmt->get_result()) {
return $this->memcache->setCache(__FUNCTION__ . $filter, $result->fetch_all(MYSQLI_ASSOC));
}
}
/**
* Same as getUserShares for Hashrate
* @param account_id integer User ID

View File

@ -14,16 +14,14 @@ $aRoundShares = $statistics->getRoundShares();
if ($_POST['query']) {
// Fetch requested users
$aUsers = $user->getUsers($_POST['query']);
$aUsers = $statistics->getAllUserStats($_POST['query']);
// Add additional stats to each user
// This is not optimized yet, best is a proper SQL
// Query against the stats table? Currently cached though.
foreach ($aUsers as $iKey => $aUser) {
$aUser['balance'] = $transaction->getBalance($aUser['id']);
$aUser['hashrate'] = $statistics->getUserHashrate($aUser['id']);
$aUser['shares'] = $statistics->getUserShares($aUser['id']);
$aUser['payout']['est_block'] = round(( (int)$aUser['shares']['valid'] / (int)$aRoundShares['valid'] ) * (int)$config['reward'], 3);
$aUser['payout']['est_block'] = round(( (int)$aUser['shares'] / (int)$aRoundShares['valid'] ) * (int)$config['reward'], 3);
$aUser['payout']['est_fee'] = round(($config['fees'] / 100) * $aUser['payout']['est_block'], 3);
$aUser['payout']['est_donation'] = round((( $aUser['donate_percent'] / 100) * ($aUser['payout']['est_block'] - $aUser['payout']['est_fee'])), 3);
$aUser['payout']['est_payout'] = round($aUser['payout']['est_block'] - $aUser['payout']['est_donation'] - $aUser['payout']['est_fee'], 3);

View File

@ -26,29 +26,16 @@
</div>
</center>
<table width="100%" class="pagesort">
<colgroup>
<col style="width: 25px">
<col style="width: 120px">
<col style="width: 120px">
<col style="width: 60px">
<col style="width: 60px">
<col style="width: 60px">
<col style="width: 60px">
<col style="width: 60px">
<col style="width: 60px">
<col style="width: 60px">
</colgroup>
<thead>
<tr>
<th class="center">ID</th>
<th>Username</th>
<th>E-Mail</th>
<th class="right">Hashrate&nbsp;&nbsp;</th>
<th class="right">Valid&nbsp;&nbsp;</th>
<th class="right">Invalid&nbsp;&nbsp;</th>
<th class="right">Balance&nbsp;&nbsp;&nbsp;</th>
<th class="right">Shares&nbsp;&nbsp;</th>
<th class="right">Est. Donation&nbsp;&nbsp;</th>
<th class="right">Est. Payout&nbsp;&nbsp;&nbsp;</th>
<th class="right">Balance&nbsp;&nbsp;&nbsp;</th>
<th class="center">Admin</th>
</tr>
</thead>
@ -59,18 +46,17 @@
<td>{$USERS[user].username}</td>
<td>{$USERS[user].email}</td>
<td class="right">{$USERS[user].hashrate|number_format}</td>
<td class="right">{$USERS[user].shares.valid|number_format}</td>
<td class="right">{$USERS[user].shares.invalid|number_format}</td>
<td class="right">{$USERS[user].balance|number_format:"8"}</td>
<td class="right">{$USERS[user].shares|number_format}</td>
<td class="right">{$USERS[user].payout.est_donation|number_format:"8"}</td>
<td class="right">{$USERS[user].payout.est_payout|number_format:"8"}</td>
<td class="right">{$USERS[user].balance|number_format:"8"}</td>
<td class="center">
<img src="{$PATH}/images/{if $USERS[user].admin}success{else}error{/if}.gif" />
</td>
</tr>
{sectionelse}
<tr>
<td colspan="10"></td>
<td colspan="9"></td>
</tr>
{/section}
</tbody>
@ -80,10 +66,10 @@
<th>Username</th>
<th>E-Mail</th>
<th class="right">Hashrate</th>
<th colspan="2" class="center">Shares</th>
<th class="right">Balance</th>
<th class="center">Shares</th>
<th class="right">Est. Donation</th>
<th class="right">Est. Payout</th>
<th class="right">Balance</th>
<th class="center">Admin</th>
</tr>
</tfoot>