92 lines
2.8 KiB
JavaScript
92 lines
2.8 KiB
JavaScript
const Times = require("../public/times");
|
||
const Init = require("./init");
|
||
|
||
class ClearDBData extends Init {
|
||
constructor(coin) {
|
||
const method = "clear"
|
||
super(coin, method);
|
||
}
|
||
|
||
/**
|
||
* 删除X小时前的sharesdb数据,hours必须大于24小时
|
||
* @param {Number} hours
|
||
* @returns
|
||
*/
|
||
async clearSharesDB(hours) {
|
||
if(hours < 24){
|
||
console.log(`sharesdb最多只能删除24小时之前的数据`);
|
||
return
|
||
}
|
||
try {
|
||
// 修正查询语句,去掉 'from' 和 'to' 字段的单引号
|
||
const query_sharesdb_table_sql = `
|
||
SELECT \`from\`, \`to\`
|
||
FROM ${this.coin}_blk_height_detail
|
||
WHERE date <= NOW() - INTERVAL ? HOUR;
|
||
`;
|
||
|
||
// 获取需要删除的高度范围
|
||
const need_del_height = await this.sharesdb.exec(query_sharesdb_table_sql, [hours]);
|
||
if (need_del_height.length === 0) {
|
||
console.log(`${Date.now()}:${this.coin}暂无需要清理的shares detail`);
|
||
return;
|
||
}
|
||
|
||
// 生成删除表的 SQL 语句
|
||
let delete_sql = `DROP TABLE IF EXISTS `;
|
||
need_del_height.forEach((item) => {
|
||
const { from, to } = item;
|
||
const table = `${this.coin}_block_detail_${from}_${Number((to - 1).toFixed(0))}`;
|
||
delete_sql += `${table}, `;
|
||
});
|
||
delete_sql = delete_sql.slice(0, -2) + ";"; // 去掉最后的逗号并加上分号
|
||
// 执行删除表的 SQL 语句
|
||
await this.sharesdb.exec(delete_sql);
|
||
} catch (err) {
|
||
throw err;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除pooldb,只支持0时刻
|
||
* @param {*} start_time 开始时间 yyyy-MM-dd hh-mm-ss格式 例如"2024-11-11 00:00:00"
|
||
* @param {*} end_time 同上
|
||
* @returns
|
||
*/
|
||
async clearPoolDB(start_time, end_time){
|
||
try{
|
||
const interval = 86400000 // 1天的毫秒数
|
||
const start_ts = new Date(start_time).valueOf()
|
||
const end_ts = new Date(end_time).valueOf()
|
||
const count = Number((end_ts - start_ts).toFixed(0)) / interval
|
||
if (!Number.isInteger(count)){
|
||
console.log(`给定的${start_time}和${end_time}有误!`);
|
||
return
|
||
}
|
||
let sql = `DROP TABLE IF EXISTS `
|
||
for(let i=0; i<count; i++){
|
||
const table_time = Times.utcTime(start_ts + i * interval).split(" ")[0].replace(/\-/g, "")
|
||
sql += `${this.coin}_miners_${table_time}, ${this.coin}_miners_stats_${table_time}, ${this.coin}_pool_blkstats_${table_time}, `
|
||
}
|
||
sql = sql.slice(0, -2) + ";"
|
||
console.log(sql);
|
||
return
|
||
} catch(err){
|
||
throw err
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除hashrate db的xx_miners表
|
||
*/
|
||
async clearHashrateDB(){
|
||
try{
|
||
const sql = `DELETE FROM ${this.coin}_minersv2 WHERE date <= NOW() - INTERVAL 25 HOUR;`
|
||
await this.hashratedb.exec_transaction(sql)
|
||
} catch(err){
|
||
throw err
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = ClearDBData |