Cleanup empty block in api response - Update cache warmer

This commit is contained in:
nymkappa 2022-03-08 16:55:49 +01:00
parent 2b5d972e8d
commit f23f7f1cfa
No known key found for this signature in database
GPG Key ID: E155910B16E8BD04
5 changed files with 29 additions and 25 deletions

View File

@ -1,5 +1,5 @@
import { PoolInfo, PoolStats } from '../mempool.interfaces'; import { PoolInfo, PoolStats } from '../mempool.interfaces';
import BlocksRepository, { EmptyBlocks } from '../repositories/BlocksRepository'; import BlocksRepository from '../repositories/BlocksRepository';
import PoolsRepository from '../repositories/PoolsRepository'; import PoolsRepository from '../repositories/PoolsRepository';
import HashratesRepository from '../repositories/HashratesRepository'; import HashratesRepository from '../repositories/HashratesRepository';
import bitcoinClient from './bitcoin/bitcoin-client'; import bitcoinClient from './bitcoin/bitcoin-client';
@ -20,25 +20,21 @@ class Mining {
const poolsStatistics = {}; const poolsStatistics = {};
const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval); const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval);
const emptyBlocks: EmptyBlocks[] = await BlocksRepository.$getEmptyBlocks(null, interval); const emptyBlocks: any[] = await BlocksRepository.$countEmptyBlocks(null, interval);
const poolsStats: PoolStats[] = []; const poolsStats: PoolStats[] = [];
let rank = 1; let rank = 1;
poolsInfo.forEach((poolInfo: PoolInfo) => { poolsInfo.forEach((poolInfo: PoolInfo) => {
const emptyBlocksCount = emptyBlocks.filter((emptyCount) => emptyCount.poolId === poolInfo.poolId);
const poolStat: PoolStats = { const poolStat: PoolStats = {
poolId: poolInfo.poolId, // mysql row id poolId: poolInfo.poolId, // mysql row id
name: poolInfo.name, name: poolInfo.name,
link: poolInfo.link, link: poolInfo.link,
blockCount: poolInfo.blockCount, blockCount: poolInfo.blockCount,
rank: rank++, rank: rank++,
emptyBlocks: 0 emptyBlocks: emptyBlocksCount.length > 0 ? emptyBlocksCount[0]['count'] : 0
}; };
for (let i = 0; i < emptyBlocks.length; ++i) {
if (emptyBlocks[i].poolId === poolInfo.poolId) {
poolStat.emptyBlocks++;
}
}
poolsStats.push(poolStat); poolsStats.push(poolStat);
}); });
@ -65,12 +61,12 @@ class Mining {
} }
const blockCount: number = await BlocksRepository.$blockCount(poolId); const blockCount: number = await BlocksRepository.$blockCount(poolId);
const emptyBlocks: EmptyBlocks[] = await BlocksRepository.$getEmptyBlocks(poolId); const emptyBlocksCount = await BlocksRepository.$countEmptyBlocks(poolId);
return { return {
pool: pool, pool: pool,
blockCount: blockCount, blockCount: blockCount,
emptyBlocks: emptyBlocks, emptyBlocks: emptyBlocksCount.length > 0 ? emptyBlocksCount[0]['count'] : 0,
}; };
} }

View File

@ -3,11 +3,6 @@ import { DB } from '../database';
import logger from '../logger'; import logger from '../logger';
import { Common } from '../api/common'; import { Common } from '../api/common';
export interface EmptyBlocks {
emptyBlocks: number;
poolId: number;
}
class BlocksRepository { class BlocksRepository {
/** /**
* Save indexed block data in the database * Save indexed block data in the database
@ -100,12 +95,13 @@ class BlocksRepository {
/** /**
* Get empty blocks for one or all pools * Get empty blocks for one or all pools
*/ */
public async $getEmptyBlocks(poolId: number | null, interval: string | null = null): Promise<EmptyBlocks[]> { public async $countEmptyBlocks(poolId: number | null, interval: string | null = null): Promise<any> {
interval = Common.getSqlInterval(interval); interval = Common.getSqlInterval(interval);
const params: any[] = []; const params: any[] = [];
let query = `SELECT height, hash, tx_count, size, pool_id, weight, UNIX_TIMESTAMP(blockTimestamp) as timestamp let query = `SELECT count(height) as count, pools.id as poolId
FROM blocks FROM blocks
JOIN pools on pools.id = blocks.pool_id
WHERE tx_count = 1`; WHERE tx_count = 1`;
if (poolId) { if (poolId) {
@ -117,13 +113,14 @@ class BlocksRepository {
query += ` AND blockTimestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`; query += ` AND blockTimestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`;
} }
// logger.debug(query); query += ` GROUP by pools.id`;
const connection = await DB.pool.getConnection(); const connection = await DB.pool.getConnection();
try { try {
const [rows] = await connection.query(query, params); const [rows] = await connection.query(query, params);
connection.release(); connection.release();
return <EmptyBlocks[]>rows; return rows;
} catch (e) { } catch (e) {
connection.release(); connection.release();
logger.err('$getEmptyBlocks() error' + (e instanceof Error ? e.message : e)); logger.err('$getEmptyBlocks() error' + (e instanceof Error ? e.message : e));

View File

@ -126,15 +126,15 @@ class HashratesRepository {
let query = `SELECT MIN(hashrate_timestamp) as firstTimestamp, MAX(hashrate_timestamp) as lastTimestamp let query = `SELECT MIN(hashrate_timestamp) as firstTimestamp, MAX(hashrate_timestamp) as lastTimestamp
FROM hashrates FROM hashrates
JOIN pools on pools.id = pool_id JOIN pools on pools.id = pool_id
WHERE hashrates.type = 'weekly' AND pool_id = ${poolId} AND avg_hashrate != 0 WHERE hashrates.type = 'weekly' AND pool_id = ? AND avg_hashrate != 0
ORDER by hashrate_timestamp LIMIT 1` ORDER by hashrate_timestamp LIMIT 1`;
let boundaries = { let boundaries = {
firstTimestamp: '1970-01-01', firstTimestamp: '1970-01-01',
lastTimestamp: '9999-01-01' lastTimestamp: '9999-01-01'
}; };
try { try {
const [rows]: any[] = await connection.query(query); const [rows]: any[] = await connection.query(query, [poolId]);
boundaries = rows[0]; boundaries = rows[0];
connection.release(); connection.release();
} catch (e) { } catch (e) {
@ -147,11 +147,11 @@ class HashratesRepository {
FROM hashrates FROM hashrates
JOIN pools on pools.id = pool_id JOIN pools on pools.id = pool_id
WHERE hashrates.type = 'weekly' AND hashrate_timestamp BETWEEN ? AND ? WHERE hashrates.type = 'weekly' AND hashrate_timestamp BETWEEN ? AND ?
AND pool_id = ${poolId} AND pool_id = ?
ORDER by hashrate_timestamp`; ORDER by hashrate_timestamp`;
try { try {
const [rows]: any[] = await connection.query(query, [boundaries.firstTimestamp, boundaries.lastTimestamp]); const [rows]: any[] = await connection.query(query, [boundaries.firstTimestamp, boundaries.lastTimestamp, poolId]);
connection.release(); connection.release();
return rows; return rows;
@ -182,6 +182,9 @@ class HashratesRepository {
const [rows] = await connection.query<any>(query, [key]); const [rows] = await connection.query<any>(query, [key]);
connection.release(); connection.release();
if (rows.length === 0) {
return 0;
}
return rows[0]['number']; return rows[0]['number'];
} catch (e) { } catch (e) {
connection.release(); connection.release();

View File

@ -40,7 +40,7 @@
</tr> </tr>
<tr> <tr>
<td class="col-4 col-lg-8">Empty Blocks</td> <td class="col-4 col-lg-8">Empty Blocks</td>
<td class="text-left">{{ poolStats.emptyBlocks.length }}</td> <td class="text-left">{{ poolStats.emptyBlocks }}</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@ -38,5 +38,13 @@ do for url in / \
curl -s "https://${hostname}${url}" >/dev/null curl -s "https://${hostname}${url}" >/dev/null
done done
counter=1
while [ $counter -le 134 ]
do
curl -s "https://${hostname}/api/v1/mining/pool/${counter}/hashrate" >/dev/null
curl -s "https://${hostname}/api/v1/mining/pool/${counter}" >/dev/null
((counter++))
done
sleep 10 sleep 10
done done