2022-02-09 19:41:05 +09:00
|
|
|
import { Common } from '../api/common';
|
2022-01-20 17:20:02 +09:00
|
|
|
import { DB } from '../database';
|
2022-02-09 19:41:05 +09:00
|
|
|
import logger from '../logger';
|
2022-01-20 17:20:02 +09:00
|
|
|
import { PoolInfo, PoolTag } from '../mempool.interfaces';
|
2022-01-05 15:41:14 +09:00
|
|
|
|
|
|
|
class PoolsRepository {
|
|
|
|
/**
|
|
|
|
* Get all pools tagging info
|
|
|
|
*/
|
2022-01-06 19:59:33 +09:00
|
|
|
public async $getPools(): Promise<PoolTag[]> {
|
2022-01-05 15:41:14 +09:00
|
|
|
const connection = await DB.pool.getConnection();
|
2022-02-10 10:25:14 +09:00
|
|
|
const [rows] = await connection.query('SELECT id, name, addresses, regexes FROM pools;');
|
2022-01-05 15:41:14 +09:00
|
|
|
connection.release();
|
2022-01-06 19:59:33 +09:00
|
|
|
return <PoolTag[]>rows;
|
2022-01-05 15:41:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get unknown pool tagging info
|
|
|
|
*/
|
2022-01-06 19:59:33 +09:00
|
|
|
public async $getUnknownPool(): Promise<PoolTag> {
|
|
|
|
const connection = await DB.pool.getConnection();
|
2022-02-10 10:25:14 +09:00
|
|
|
const [rows] = await connection.query('SELECT id, name FROM pools where name = "Unknown"');
|
2022-01-06 19:59:33 +09:00
|
|
|
connection.release();
|
|
|
|
return <PoolTag>rows[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get basic pool info and block count
|
|
|
|
*/
|
2022-02-09 19:41:05 +09:00
|
|
|
public async $getPoolsInfo(interval: string | null = null): Promise<PoolInfo[]> {
|
|
|
|
interval = Common.getSqlInterval(interval);
|
|
|
|
|
|
|
|
let query = `SELECT COUNT(height) as blockCount, pool_id as poolId, pools.name as name, pools.link as link
|
2022-01-06 19:59:33 +09:00
|
|
|
FROM blocks
|
2022-02-09 19:41:05 +09:00
|
|
|
JOIN pools on pools.id = pool_id`;
|
|
|
|
|
|
|
|
if (interval) {
|
|
|
|
query += ` WHERE blocks.blockTimestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`;
|
|
|
|
}
|
2022-01-25 18:33:46 +09:00
|
|
|
|
2022-02-09 19:41:05 +09:00
|
|
|
query += ` GROUP BY pool_id
|
|
|
|
ORDER BY COUNT(height) DESC`;
|
|
|
|
|
2022-02-11 19:12:52 +09:00
|
|
|
// logger.debug(query);
|
2022-01-25 18:33:46 +09:00
|
|
|
const connection = await DB.pool.getConnection();
|
2022-03-06 16:44:09 +01:00
|
|
|
try {
|
|
|
|
const [rows] = await connection.query(query);
|
|
|
|
connection.release();
|
|
|
|
|
|
|
|
return <PoolInfo[]>rows;
|
|
|
|
} catch (e) {
|
|
|
|
connection.release();
|
|
|
|
logger.err('$getPoolsInfo() error' + (e instanceof Error ? e.message : e));
|
|
|
|
throw e;
|
|
|
|
}
|
2022-01-06 19:59:33 +09:00
|
|
|
}
|
2022-02-08 18:28:53 +09:00
|
|
|
|
2022-02-24 16:55:18 +09:00
|
|
|
/**
|
|
|
|
* Get basic pool info and block count between two timestamp
|
|
|
|
*/
|
|
|
|
public async $getPoolsInfoBetween(from: number, to: number): Promise<PoolInfo[]> {
|
2022-03-06 16:44:09 +01:00
|
|
|
const query = `SELECT COUNT(height) as blockCount, pools.id as poolId, pools.name as poolName
|
2022-02-24 16:55:18 +09:00
|
|
|
FROM pools
|
|
|
|
LEFT JOIN blocks on pools.id = blocks.pool_id AND blocks.blockTimestamp BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?)
|
|
|
|
GROUP BY pools.id`;
|
|
|
|
|
|
|
|
const connection = await DB.pool.getConnection();
|
2022-03-06 16:44:09 +01:00
|
|
|
try {
|
|
|
|
const [rows] = await connection.query(query, [from, to]);
|
|
|
|
connection.release();
|
|
|
|
|
|
|
|
return <PoolInfo[]>rows;
|
|
|
|
} catch (e) {
|
|
|
|
connection.release();
|
|
|
|
logger.err('$getPoolsInfoBetween() error' + (e instanceof Error ? e.message : e));
|
|
|
|
throw e;
|
|
|
|
}
|
2022-02-24 16:55:18 +09:00
|
|
|
}
|
|
|
|
|
2022-02-08 18:28:53 +09:00
|
|
|
/**
|
|
|
|
* Get mining pool statistics for one pool
|
|
|
|
*/
|
2022-02-09 19:41:05 +09:00
|
|
|
public async $getPool(poolId: any): Promise<object> {
|
2022-02-08 18:28:53 +09:00
|
|
|
const query = `
|
|
|
|
SELECT *
|
|
|
|
FROM pools
|
2022-02-09 19:41:05 +09:00
|
|
|
WHERE pools.id = ?`;
|
2022-02-08 18:28:53 +09:00
|
|
|
|
2022-02-11 19:12:52 +09:00
|
|
|
// logger.debug(query);
|
2022-02-08 18:28:53 +09:00
|
|
|
const connection = await DB.pool.getConnection();
|
2022-03-06 16:44:09 +01:00
|
|
|
try {
|
|
|
|
const [rows] = await connection.query(query, [poolId]);
|
|
|
|
connection.release();
|
|
|
|
|
|
|
|
rows[0].regexes = JSON.parse(rows[0].regexes);
|
|
|
|
rows[0].addresses = JSON.parse(rows[0].addresses);
|
|
|
|
|
|
|
|
return rows[0];
|
|
|
|
} catch (e) {
|
|
|
|
connection.release();
|
|
|
|
logger.err('$getPool() error' + (e instanceof Error ? e.message : e));
|
|
|
|
throw e;
|
|
|
|
}
|
2022-02-08 18:28:53 +09:00
|
|
|
}
|
2022-01-05 15:41:14 +09:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:20:02 +09:00
|
|
|
export default new PoolsRepository();
|