Generate daily average hashrate data
This commit is contained in:
@@ -149,6 +149,40 @@ class BlocksRepository {
|
||||
return <number>rows[0].blockCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get blocks count between two dates
|
||||
* @param poolId
|
||||
* @param from - The oldest timestamp
|
||||
* @param to - The newest timestamp
|
||||
* @returns
|
||||
*/
|
||||
public async $blockCountBetweenTimestamp(poolId: number | null, from: number, to: number): Promise<number> {
|
||||
const params: any[] = [];
|
||||
let query = `SELECT
|
||||
count(height) as blockCount,
|
||||
max(height) as lastBlockHeight
|
||||
FROM blocks`;
|
||||
|
||||
if (poolId) {
|
||||
query += ` WHERE pool_id = ?`;
|
||||
params.push(poolId);
|
||||
}
|
||||
|
||||
if (poolId) {
|
||||
query += ` AND`;
|
||||
} else {
|
||||
query += ` WHERE`;
|
||||
}
|
||||
query += ` UNIX_TIMESTAMP(blockTimestamp) BETWEEN '${from}' AND '${to}'`;
|
||||
|
||||
// logger.debug(query);
|
||||
const connection = await DB.pool.getConnection();
|
||||
const [rows] = await connection.query(query, params);
|
||||
connection.release();
|
||||
|
||||
return <number>rows[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the oldest indexed block
|
||||
*/
|
||||
|
||||
42
backend/src/repositories/HashratesRepository.ts
Normal file
42
backend/src/repositories/HashratesRepository.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { DB } from '../database';
|
||||
import logger from '../logger';
|
||||
|
||||
class HashratesRepository {
|
||||
/**
|
||||
* Save indexed block data in the database
|
||||
*/
|
||||
public async $saveDailyStat(dailyStat: any) {
|
||||
const connection = await DB.pool.getConnection();
|
||||
|
||||
try {
|
||||
const query = `INSERT INTO
|
||||
hashrates(hashrate_timestamp, avg_hashrate, pool_id)
|
||||
VALUE (FROM_UNIXTIME(?), ?, ?)`;
|
||||
|
||||
const params: any[] = [
|
||||
dailyStat.hashrateTimestamp, dailyStat.avgHashrate,
|
||||
dailyStat.poolId
|
||||
];
|
||||
|
||||
// logger.debug(query);
|
||||
await connection.query(query, params);
|
||||
} catch (e: any) {
|
||||
logger.err('$saveHashrateInDatabase() error' + (e instanceof Error ? e.message : e));
|
||||
}
|
||||
|
||||
connection.release();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of all timestamp we've already indexed
|
||||
*/
|
||||
public async $getAllTimestamp(): Promise<number[]> {
|
||||
const connection = await DB.pool.getConnection();
|
||||
const [rows]: any[] = await connection.query(`SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp from hashrates`);
|
||||
connection.release();
|
||||
|
||||
return rows.map(val => val.timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
export default new HashratesRepository();
|
||||
Reference in New Issue
Block a user