pplns payouts speed improvements / reworked insert method

This commit is contained in:
obigal 2014-01-12 15:07:22 -05:00 committed by Sebastian Grewe
parent bc833eb40b
commit 75729c6592
3 changed files with 42 additions and 54 deletions

View File

@ -153,19 +153,42 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
$iRoundShares = $iNewRoundShares;
}
// Merge round shares and pplns shares arrays
$aTotalAccountShares = NULL;
foreach($aAccountShares as $key => $aData) {
$aData['pplns_valid'] = $aData['valid'];
$aData['pplns_invalid'] = $aData['invalid'];
$aData['valid'] = 0;
$aData['invalid'] = 0;
$aTotalAccountShares[$aData['username']] = $aData;
}
foreach($aRoundAccountShares as $key => $aTempData) {
if (array_key_exists($aTempData['username'], $aTotalAccountShares)) {
$aTotalAccountShares[$aTempData['username']]['valid'] = $aTempData['valid'];
$aTotalAccountShares[$aTempData['username']]['invalid'] = $aTempData['invalid'];
} else {
$aTempData['pplns_valid'] = 0;
$aTempData['pplns_invalid'] = 0;
$aTotalAccountShares[$aTempData['username']] = $aTempData;
}
}
// Table header for account shares
$log->logInfo("ID\tUsername\tValid\tInvalid\tPercentage\tPayout\t\tDonation\tFee");
// Loop through all accounts that have found shares for this round
foreach ($aAccountShares as $key => $aData) {
foreach ($aTotalAccountShares as $key => $aData) {
// Skip entries that have no account ID, user deleted?
if (empty($aData['id'])) {
$log->logInfo('User ' . $aData['username'] . ' does not have an associated account, skipping');
continue;
}
if ($aData['pplns_valid'] == 0) {
continue;
}
// Payout based on PPLNS target shares, proportional payout for all users
$aData['percentage'] = round(( 100 / $iRoundShares) * $aData['valid'], 8);
$aData['percentage'] = round(( 100 / $iRoundShares) * $aData['pplns_valid'], 8);
$aData['payout'] = round(( $aData['percentage'] / 100 ) * $dReward, 8);
// Defaults
$aData['fee' ] = 0;
@ -179,33 +202,13 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
// Verbose output of this users calculations
$log->logInfo($aData['id'] . "\t" .
$aData['username'] . "\t" .
$aData['valid'] . "\t" .
$aData['invalid'] . "\t" .
$aData['pplns_valid'] . "\t" .
$aData['pplns_invalid'] . "\t" .
number_format($aData['percentage'], 8) . "\t" .
number_format($aData['payout'], 8) . "\t" .
number_format($aData['donation'], 8) . "\t" .
number_format($aData['fee'], 8));
// Add full round share statistics, not just PPLNS
foreach ($aRoundAccountShares as $key => $aRoundData) {
if ($aRoundData['username'] == $aData['username'])
if (!$statistics->updateShareStatistics($aRoundData, $aBlock['id']))
$log->logError('Failed to update share statistics for ' . $aData['username'] . ': ' . $statistics->getCronError());
}
// Add PPLNS share statistics
foreach ($aAccountShares as $key => $aRoundData) {
if ($aRoundData['username'] == $aData['username']){
if (@$statistics->getIdShareStatistics($aRoundData, $aBlock['id'])){
if (!$statistics->updatePPLNSShareStatistics($aRoundData, $aBlock['id']))
$log->logError('Failed to update pplns statistics for ' . $aData['username'] . ': ' . $statistics->getCronError());
} else {
if (!$statistics->insertPPLNSShareStatistics($aRoundData, $aBlock['id']))
$log->logError('Failed to insert pplns statistics for ' . $aData['username'] . ': ' . $statistics->getCronError());
}
}
}
// Add new credit transaction
if (!$transaction->addTransaction($aData['id'], $aData['payout'], 'Credit', $aBlock['id']))
$log->logFatal('Failed to insert new Credit transaction to database for ' . $aData['username'] . ': ' . $transaction->getCronError());
@ -219,6 +222,15 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
$log->logFatal('Failed to insert new Donation transaction to database for ' . $aData['username'] . ': ' . $transaction->getCronError());
}
// Add full round share statistics
foreach ($aTotalAccountShares as $key => $aRoundData) {
if (empty($aRoundData['id'])) {
continue;
}
if (!$statistics->insertPPLNSStatistics($aRoundData, $aBlock['id']))
$log->logError('Failed to insert share statistics for ' . $aRoundData['username'] . ': ' . $statistics->getCronError());
}
// Store this blocks height as last accounted for
$setting->setValue('last_accounted_block_id', $aBlock['id']);

View File

@ -107,7 +107,7 @@ class RoundStats extends Base {
FROM " . $this->statistics->getTableName() . " AS s
LEFT JOIN " . $this->block->getTableName() . " AS b ON s.block_id = b.id
LEFT JOIN " . $this->user->getTableName() . " AS a ON a.id = s.account_id
WHERE b.height = ?
WHERE b.height = ? AND s.valid > 0
GROUP BY username ASC
ORDER BY valid DESC
");
@ -136,7 +136,7 @@ class RoundStats extends Base {
FROM " . $this->statistics->getTableName() . " AS s
LEFT JOIN " . $this->block->getTableName() . " AS b ON s.block_id = b.id
LEFT JOIN " . $this->user->getTableName() . " AS a ON a.id = s.account_id
WHERE b.height = ?
WHERE b.height = ? AND s.pplns_valid > 0
GROUP BY username ASC
ORDER BY pplns_valid DESC
");

View File

@ -202,36 +202,12 @@ class Statistics extends Base {
}
/**
* update user statistics of valid and invalid pplns shares
* insert user round and pplns shares merged array
**/
public function updatePPLNSShareStatistics($aStats, $iBlockId) {
public function insertPPLNSStatistics($aStats, $iBlockId) {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("
UPDATE $this->table SET pplns_valid = ?, pplns_invalid = ? WHERE account_id = ? AND block_id = ?");
if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $aStats['valid'], $aStats['invalid'], $aStats['id'], $iBlockId) && $stmt->execute()) return true;
return $this->sqlError();
}
/**
* insert user statistics of valid and invalid pplns shares "rbpplns"
**/
public function insertPPLNSShareStatistics($aStats, $iBlockId) {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, pplns_valid, pplns_invalid, block_id) VALUES (?, 0, 0, ?, ?, ?)");
if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId) && $stmt->execute()) return true;
return $this->sqlError();
}
/**
* Fetch the share ID from stats for rbpplns
**/
function getIdShareStatistics($aStats, $iBlockId) {
$stmt = $this->mysqli->prepare("
SELECT id AS id FROM $this->table
WHERE account_id = ? AND block_id = ?
");
if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $aStats['id'], $iBlockId) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_object()->id;
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, pplns_valid, pplns_invalid, block_id) VALUES (?, ?, ?, ?, ?, ?)");
if ($this->checkStmt($stmt) && $stmt->bind_param('iiiiii', $aStats['id'], $aStats['valid'], $aStats['invalid'], $aStats['pplns_valid'], $aStats['pplns_invalid'], $iBlockId) && $stmt->execute()) return true;
return $this->sqlError();
}