2022-02-19 22:09:35 +09:00
|
|
|
import { Common } from '../api/common';
|
2022-02-19 20:45:02 +09:00
|
|
|
import { DB } from '../database';
|
|
|
|
import logger from '../logger';
|
2022-02-24 16:55:18 +09:00
|
|
|
import PoolsRepository from './PoolsRepository';
|
2022-02-19 20:45:02 +09:00
|
|
|
|
|
|
|
class HashratesRepository {
|
|
|
|
/**
|
|
|
|
* Save indexed block data in the database
|
|
|
|
*/
|
2022-02-21 17:34:07 +09:00
|
|
|
public async $saveHashrates(hashrates: any) {
|
|
|
|
let query = `INSERT INTO
|
2022-02-24 16:55:18 +09:00
|
|
|
hashrates(hashrate_timestamp, avg_hashrate, pool_id, share, type) VALUES`;
|
2022-02-19 20:45:02 +09:00
|
|
|
|
2022-02-21 17:34:07 +09:00
|
|
|
for (const hashrate of hashrates) {
|
2022-02-24 16:55:18 +09:00
|
|
|
query += ` (FROM_UNIXTIME(${hashrate.hashrateTimestamp}), ${hashrate.avgHashrate}, ${hashrate.poolId}, ${hashrate.share}, "${hashrate.type}"),`;
|
2022-02-21 17:34:07 +09:00
|
|
|
}
|
|
|
|
query = query.slice(0, -1);
|
2022-02-19 20:45:02 +09:00
|
|
|
|
2022-02-21 17:34:07 +09:00
|
|
|
const connection = await DB.pool.getConnection();
|
|
|
|
try {
|
2022-02-19 20:45:02 +09:00
|
|
|
// logger.debug(query);
|
2022-02-21 17:34:07 +09:00
|
|
|
await connection.query(query);
|
2022-02-19 20:45:02 +09:00
|
|
|
} catch (e: any) {
|
|
|
|
logger.err('$saveHashrateInDatabase() error' + (e instanceof Error ? e.message : e));
|
2022-03-05 16:23:01 +01:00
|
|
|
throw e;
|
2022-02-19 20:45:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
connection.release();
|
|
|
|
}
|
|
|
|
|
2022-02-24 16:55:18 +09:00
|
|
|
public async $getNetworkDailyHashrate(interval: string | null): Promise<any[]> {
|
2022-02-19 22:09:35 +09:00
|
|
|
interval = Common.getSqlInterval(interval);
|
|
|
|
|
2022-02-19 20:45:02 +09:00
|
|
|
const connection = await DB.pool.getConnection();
|
2022-02-19 22:09:35 +09:00
|
|
|
|
|
|
|
let query = `SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp, avg_hashrate as avgHashrate
|
|
|
|
FROM hashrates`;
|
|
|
|
|
|
|
|
if (interval) {
|
2022-02-24 16:55:18 +09:00
|
|
|
query += ` WHERE hashrate_timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()
|
|
|
|
AND hashrates.type = 'daily'
|
|
|
|
AND pool_id IS NULL`;
|
|
|
|
} else {
|
|
|
|
query += ` WHERE hashrates.type = 'daily'
|
|
|
|
AND pool_id IS NULL`;
|
2022-02-19 22:09:35 +09:00
|
|
|
}
|
|
|
|
|
2022-02-22 00:17:41 +09:00
|
|
|
query += ` ORDER by hashrate_timestamp`;
|
2022-02-21 12:22:20 +09:00
|
|
|
|
2022-02-19 22:09:35 +09:00
|
|
|
const [rows]: any[] = await connection.query(query);
|
2022-02-19 20:45:02 +09:00
|
|
|
connection.release();
|
2022-02-19 22:09:35 +09:00
|
|
|
|
|
|
|
return rows;
|
2022-02-19 20:45:02 +09:00
|
|
|
}
|
2022-02-21 12:22:20 +09:00
|
|
|
|
2022-02-24 16:55:18 +09:00
|
|
|
/**
|
|
|
|
* Returns the current biggest pool hashrate history
|
|
|
|
*/
|
|
|
|
public async $getPoolsWeeklyHashrate(interval: string | null): Promise<any[]> {
|
|
|
|
interval = Common.getSqlInterval(interval);
|
|
|
|
|
|
|
|
const connection = await DB.pool.getConnection();
|
|
|
|
const topPoolsId = (await PoolsRepository.$getPoolsInfo('1w')).map((pool) => pool.poolId);
|
|
|
|
|
|
|
|
let query = `SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp, avg_hashrate as avgHashrate, share, pools.name as poolName
|
|
|
|
FROM hashrates
|
|
|
|
JOIN pools on pools.id = pool_id`;
|
|
|
|
|
|
|
|
if (interval) {
|
|
|
|
query += ` WHERE hashrate_timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()
|
|
|
|
AND hashrates.type = 'weekly'
|
|
|
|
AND pool_id IN (${topPoolsId})`;
|
|
|
|
} else {
|
|
|
|
query += ` WHERE hashrates.type = 'weekly'
|
|
|
|
AND pool_id IN (${topPoolsId})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
query += ` ORDER by hashrate_timestamp, FIELD(pool_id, ${topPoolsId})`;
|
|
|
|
|
|
|
|
const [rows]: any[] = await connection.query(query);
|
|
|
|
connection.release();
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
2022-02-24 20:20:18 +09:00
|
|
|
public async $setLatestRunTimestamp(val: any = null) {
|
2022-02-21 12:22:20 +09:00
|
|
|
const connection = await DB.pool.getConnection();
|
|
|
|
const query = `UPDATE state SET number = ? WHERE name = 'last_hashrates_indexing'`;
|
2022-02-24 20:20:18 +09:00
|
|
|
|
|
|
|
await connection.query<any>(query, (val === null) ? [Math.round(new Date().getTime() / 1000)] : [val]);
|
2022-02-21 12:22:20 +09:00
|
|
|
connection.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async $getLatestRunTimestamp(): Promise<number> {
|
|
|
|
const connection = await DB.pool.getConnection();
|
|
|
|
const query = `SELECT number FROM state WHERE name = 'last_hashrates_indexing'`;
|
|
|
|
const [rows] = await connection.query<any>(query);
|
|
|
|
connection.release();
|
|
|
|
return rows[0]['number'];
|
|
|
|
}
|
2022-02-19 20:45:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new HashratesRepository();
|