API realtime actions

This commit is contained in:
marcdeb1 2018-12-18 13:42:45 +01:00
parent b61a1545a7
commit 25a50ca2aa

View file

@ -95,12 +95,8 @@ class MainController extends AppController {
$blocks = $this->Blocks->find()->select(['chainwork', 'confirmations', 'difficulty', 'hash', 'height', 'transaction_hashes', 'block_time', 'block_size'])->order(['height' => 'desc'])->limit(6)->toArray(); $blocks = $this->Blocks->find()->select(['chainwork', 'confirmations', 'difficulty', 'hash', 'height', 'transaction_hashes', 'block_time', 'block_size'])->order(['height' => 'desc'])->limit(6)->toArray();
for ($i = 0; $i < count($blocks); $i++) { for ($i = 0; $i < count($blocks); $i++) {
$tx_hashes = json_decode($blocks[$i]->transaction_hashes, true); $tx_hashes = preg_split('#,#', $blocks[$i]->transaction_hashes);
if(!empty($tx_hashes)) { $blocks[$i]->transaction_count = count($tx_hashes);
$blocks[$i]->transaction_count = count($tx_hashes);
} else {
$blocks[$i]->transaction_count = 0;
}
} }
// hash rate // hash rate
$hashRate = $this->_formatHashRate($this->_gethashrate()); $hashRate = $this->_formatHashRate($this->_gethashrate());
@ -238,7 +234,7 @@ class MainController extends AppController {
$conn = ConnectionManager::get('default'); $conn = ConnectionManager::get('default');
$blocks = $this->Blocks->find()->select(['height', 'block_time', 'transaction_hashes'])->order(['height' => 'desc'])->limit(10)->toArray(); $blocks = $this->Blocks->find()->select(['height', 'block_time', 'transaction_hashes'])->order(['height' => 'desc'])->limit(10)->toArray();
for ($i = 0; $i < count($blocks); $i++) { for ($i = 0; $i < count($blocks); $i++) {
$tx_hashes = json_decode($blocks[$i]->transaction_hashes); $tx_hashes = preg_split('#,#', $blocks[$i]->transaction_hashes);
$blocks[$i]->transaction_count = count($tx_hashes); $blocks[$i]->transaction_count = count($tx_hashes);
} }
@ -311,7 +307,7 @@ class MainController extends AppController {
$page = intval($this->request->query('page')); $page = intval($this->request->query('page'));
$conn = ConnectionManager::get('default'); $conn = ConnectionManager::get('default');
$stmt = $conn->execute('SELECT COUNT(Id) AS Total FROM Blocks'); $stmt = $conn->execute('SELECT COUNT(id) AS Total FROM Blocks');
$count = $stmt->fetch(\PDO::FETCH_OBJ); $count = $stmt->fetch(\PDO::FETCH_OBJ);
$numBlocks = $count->Total; $numBlocks = $count->Total;
@ -592,8 +588,8 @@ class MainController extends AppController {
$conn = ConnectionManager::get('default'); $conn = ConnectionManager::get('default');
// get avg block sizes for the time period // get avg block sizes for the time period
$stmt = $conn->execute("SELECT AVG(BlockSize) AS AvgBlockSize, DATE_FORMAT(FROM_UNIXTIME(BlockTime), '$sqlDateFormat') AS TimePeriod " . $stmt = $conn->execute("SELECT AVG(BlockSize) AS AvgBlockSize, DATE_FORMAT(FROM_UNIXTIME(block_time), '$sqlDateFormat') AS TimePeriod " .
"FROM Blocks WHERE DATE_FORMAT(FROM_UNIXTIME(BlockTime), '$sqlDateFormat') >= ? GROUP BY TimePeriod ORDER BY TimePeriod ASC", [$start->format($dateFormat)]); "FROM block WHERE DATE_FORMAT(FROM_UNIXTIME(block_time), '$sqlDateFormat') >= ? GROUP BY TimePeriod ORDER BY TimePeriod ASC", [$start->format($dateFormat)]);
$avgBlockSizes = $stmt->fetchAll(\PDO::FETCH_OBJ); $avgBlockSizes = $stmt->fetchAll(\PDO::FETCH_OBJ);
foreach ($avgBlockSizes as $size) { foreach ($avgBlockSizes as $size) {
if (!isset($resultSet[$size->TimePeriod])) { if (!isset($resultSet[$size->TimePeriod])) {
@ -603,8 +599,8 @@ class MainController extends AppController {
} }
// get avg prices // get avg prices
$stmt = $conn->execute("SELECT AVG(USD) AS AvgUSD, DATE_FORMAT(Created, '$sqlDateFormat') AS TimePeriod " . $stmt = $conn->execute("SELECT AVG(USD) AS AvgUSD, DATE_FORMAT(created_at, '$sqlDateFormat') AS TimePeriod " .
"FROM PriceHistory WHERE DATE_FORMAT(Created, '$sqlDateFormat') >= ? GROUP BY TimePeriod ORDER BY TimePeriod ASC", [$start->format($dateFormat)]); "FROM PriceHistory WHERE DATE_FORMAT(created_at, '$sqlDateFormat') >= ? GROUP BY TimePeriod ORDER BY TimePeriod ASC", [$start->format($dateFormat)]);
$avgPrices = $stmt->fetchAll(\PDO::FETCH_OBJ); $avgPrices = $stmt->fetchAll(\PDO::FETCH_OBJ);
foreach ($avgPrices as $price) { foreach ($avgPrices as $price) {
if (!isset($resultSet[$price->TimePeriod])) { if (!isset($resultSet[$price->TimePeriod])) {
@ -617,26 +613,24 @@ class MainController extends AppController {
} }
public function apirealtimeblocks() { public function apirealtimeblocks() {
// load 10 blocks // Load 10 blocks
$this->autoRender = false; $this->autoRender = false;
$this->loadModel('Blocks'); $this->loadModel('Blocks');
$blocks = $this->Blocks->find()->select(['Height', 'BlockTime', 'TransactionHashes'])->order(['Height' => 'desc'])->limit(10)->toArray(); $blocks = $this->Blocks->find()->select(['height', 'block_time', 'transaction_hashes'])->order(['height' => 'desc'])->limit(10)->toArray();
for ($i = 0; $i < count($blocks); $i++) { for ($i = 0; $i < count($blocks); $i++) {
$tx_hashes = json_decode($blocks[$i]->TransactionHashes); $tx_hashes = preg_split('#,#', $blocks[$i]->transaction_hashes);
$blocks[$i]->TransactionCount = count($tx_hashes); $blocks[$i]->transaction_count = count($tx_hashes);
unset($blocks[$i]->TransactionHashes); unset($blocks[$i]->transaction_hashes);
} }
$this->_jsonResponse(['success' => true, 'blocks' => $blocks]); $this->_jsonResponse(['success' => true, 'blocks' => $blocks]);
} }
public function apirealtimetx() { public function apirealtimetx() {
// load 10 transactions // Load 10 transactions
$this->autoRender = false; $this->autoRender = false;
$conn = ConnectionManager::get('default'); $this->loadModel('Transactions');
$stmt = $conn->execute('SELECT T.Hash, T.InputCount, T.OutputCount, T.Value, IFNULL(T.TransactionTime, T.CreatedTime) AS TxTime ' . $txs = $this->Transactions->find()->select(['id', 'hash', 'input_count', 'output_count', 'transaction_time'])->order(['transaction_time' => 'desc'])-> limit(10);
'FROM Transactions T ORDER BY CreatedTime DESC LIMIT 10');
$txs = $stmt->fetchAll(\PDO::FETCH_OBJ);
$this->_jsonResponse(['success' => true, 'txs' => $txs]); $this->_jsonResponse(['success' => true, 'txs' => $txs]);
} }
@ -648,9 +642,9 @@ class MainController extends AppController {
// Get the max height block // Get the max height block
$height = 0; $height = 0;
$difficulty = 0; $difficulty = 0;
$highestBlock = $this->Blocks->find()->select(['Height', 'Difficulty'])->order(['Height' => 'desc'])->first(); $highestBlock = $this->Blocks->find()->select(['height', 'difficulty'])->order(['height' => 'desc'])->first();
$height = $highestBlock->Height; $height = $highestBlock->height;
$difficulty = $highestBlock->Difficulty; $difficulty = $highestBlock->difficulty;
$lbcUsdPrice = $this->_getLatestPrice(); $lbcUsdPrice = $this->_getLatestPrice();
// Calculate hash rate // Calculate hash rate
@ -667,13 +661,13 @@ class MainController extends AppController {
public function apirecentblocks() { public function apirecentblocks() {
$this->autoRender = false; $this->autoRender = false;
$this->loadModel('Blocks'); $this->loadModel('Blocks');
$blocks = $this->Blocks->find()->select(['Difficulty', 'Hash', 'Height', 'TransactionHashes', 'BlockTime', 'BlockSize'])-> $blocks = $this->Blocks->find()->select(['difficulty', 'hash', 'height', 'transaction_hashes', 'block_time', 'block_size'])->
order(['Height' => 'desc'])->limit(6)->toArray(); order(['height' => 'desc'])->limit(6)->toArray();
for ($i = 0; $i < count($blocks); $i++) { for ($i = 0; $i < count($blocks); $i++) {
$tx_hashes = json_decode($blocks[$i]->TransactionHashes); $tx_hashes = preg_split('#,#', $blocks[$i]->transaction_hashes);
$blocks[$i]->TransactionCount = count($tx_hashes); $blocks[$i]->transaction_count = count($tx_hashes);
$blocks[$i]->Difficulty = number_format($blocks[$i]->Difficulty, 2, '.', ''); $blocks[$i]->difficulty = number_format($blocks[$i]->difficulty, 2, '.', '');
unset($blocks[$i]->TransactionHashes); unset($blocks[$i]->transaction_hashes);
} }
return $this->_jsonResponse(['success' => true, 'blocks' => $blocks]); return $this->_jsonResponse(['success' => true, 'blocks' => $blocks]);
} }