mirror of
https://github.com/mempool/mempool.git
synced 2026-08-13 12:33:11 +02:00
optimize pool stats queries
This commit is contained in:
parent
ba5c2ff1ad
commit
a5dceacb42
4 changed files with 6 additions and 40 deletions
|
|
@ -114,31 +114,29 @@ class Mining {
|
|||
const poolsStatistics = {};
|
||||
|
||||
const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval);
|
||||
const emptyBlocks: any[] = await BlocksRepository.$countEmptyBlocks(null, interval);
|
||||
|
||||
const poolsStats: PoolStats[] = [];
|
||||
let rank = 1;
|
||||
let blockCount = 0;
|
||||
|
||||
poolsInfo.forEach((poolInfo: PoolInfo) => {
|
||||
const emptyBlocksCount = emptyBlocks.filter((emptyCount) => emptyCount.poolId === poolInfo.poolId);
|
||||
const poolStat: PoolStats = {
|
||||
poolId: poolInfo.poolId, // mysql row id
|
||||
name: poolInfo.name,
|
||||
link: poolInfo.link,
|
||||
blockCount: poolInfo.blockCount,
|
||||
rank: rank++,
|
||||
emptyBlocks: emptyBlocksCount.length > 0 ? emptyBlocksCount[0]['count'] : 0,
|
||||
emptyBlocks: poolInfo.emptyBlocks,
|
||||
slug: poolInfo.slug,
|
||||
avgMatchRate: poolInfo.avgMatchRate !== null ? Math.round(100 * poolInfo.avgMatchRate) / 100 : null,
|
||||
avgFeeDelta: poolInfo.avgFeeDelta,
|
||||
poolUniqueId: poolInfo.poolUniqueId
|
||||
};
|
||||
poolsStats.push(poolStat);
|
||||
blockCount += poolInfo.blockCount;
|
||||
});
|
||||
|
||||
poolsStatistics['pools'] = poolsStats;
|
||||
|
||||
const blockCount: number = await BlocksRepository.$blockCount(null, interval);
|
||||
poolsStatistics['blockCount'] = blockCount;
|
||||
|
||||
const totalBlock24h: number = await BlocksRepository.$blockCount(null, '24h');
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ export interface PoolInfo {
|
|||
name: string;
|
||||
link: string;
|
||||
blockCount: number;
|
||||
emptyBlocks: number;
|
||||
slug: string;
|
||||
avgMatchRate: number | null;
|
||||
avgFeeDelta: number | null;
|
||||
|
|
@ -25,7 +26,6 @@ export interface PoolInfo {
|
|||
|
||||
export interface PoolStats extends PoolInfo {
|
||||
rank: number;
|
||||
emptyBlocks: number;
|
||||
}
|
||||
|
||||
export enum TemplateAlgorithm {
|
||||
|
|
|
|||
|
|
@ -305,39 +305,6 @@ class BlocksRepository {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get empty blocks for one or all pools
|
||||
* @asyncSafe
|
||||
*/
|
||||
public async $countEmptyBlocks(poolId: number | null, interval: string | null = null): Promise<any> {
|
||||
interval = Common.getSqlInterval(interval);
|
||||
|
||||
const params: any[] = [];
|
||||
let query = `SELECT count(height) as count, pools.id as poolId
|
||||
FROM blocks
|
||||
JOIN pools on pools.id = blocks.pool_id
|
||||
WHERE tx_count = 1 AND stale = 0`;
|
||||
|
||||
if (poolId) {
|
||||
query += ` AND pool_id = ?`;
|
||||
params.push(poolId);
|
||||
}
|
||||
|
||||
if (interval) {
|
||||
query += ` AND blockTimestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`;
|
||||
}
|
||||
|
||||
query += ` GROUP by pools.id`;
|
||||
|
||||
try {
|
||||
const [rows] = await DB.query(query, params);
|
||||
return rows;
|
||||
} catch (e) {
|
||||
logger.err('Cannot count empty blocks. Reason: ' + (e instanceof Error ? e.message : e));
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return most recent block height
|
||||
* @asyncSafe
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ class PoolsRepository {
|
|||
let query = `
|
||||
SELECT
|
||||
COUNT(blocks.height) As blockCount,
|
||||
COUNT(CASE WHEN blocks.tx_count = 1 THEN 1 END) AS emptyBlocks,
|
||||
pool_id AS poolId,
|
||||
pools.name AS name,
|
||||
pools.link AS link,
|
||||
|
|
@ -47,7 +48,7 @@ class PoolsRepository {
|
|||
unique_id as poolUniqueId
|
||||
FROM blocks
|
||||
JOIN pools on pools.id = pool_id
|
||||
LEFT JOIN blocks_audits ON blocks_audits.height = blocks.height
|
||||
LEFT JOIN blocks_audits ON blocks_audits.hash = blocks.hash
|
||||
WHERE blocks.stale = 0
|
||||
`;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue