38727-vm/backend/src/db/api/mining_sessions.js
2026-02-24 00:53:51 +00:00

638 lines
16 KiB
JavaScript

const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');
const Sequelize = db.Sequelize;
const Op = Sequelize.Op;
module.exports = class Mining_sessionsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const mining_sessions = await db.mining_sessions.create(
{
id: data.id || undefined,
state: data.state
||
null
,
started_at: data.started_at
||
null
,
ended_at: data.ended_at
||
null
,
stop_reason: data.stop_reason
||
null
,
avg_hashrate_ths: data.avg_hashrate_ths
||
null
,
shares_accepted: data.shares_accepted
||
null
,
shares_rejected: data.shares_rejected
||
null
,
estimated_btc_earned: data.estimated_btc_earned
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await mining_sessions.setMiner( data.miner || null, {
transaction,
});
return mining_sessions;
}
static async bulkImport(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
// Prepare data - wrapping individual data transformations in a map() method
const mining_sessionsData = data.map((item, index) => ({
id: item.id || undefined,
state: item.state
||
null
,
started_at: item.started_at
||
null
,
ended_at: item.ended_at
||
null
,
stop_reason: item.stop_reason
||
null
,
avg_hashrate_ths: item.avg_hashrate_ths
||
null
,
shares_accepted: item.shares_accepted
||
null
,
shares_rejected: item.shares_rejected
||
null
,
estimated_btc_earned: item.estimated_btc_earned
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const mining_sessions = await db.mining_sessions.bulkCreate(mining_sessionsData, { transaction });
// For each item created, replace relation files
return mining_sessions;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const mining_sessions = await db.mining_sessions.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.state !== undefined) updatePayload.state = data.state;
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
if (data.ended_at !== undefined) updatePayload.ended_at = data.ended_at;
if (data.stop_reason !== undefined) updatePayload.stop_reason = data.stop_reason;
if (data.avg_hashrate_ths !== undefined) updatePayload.avg_hashrate_ths = data.avg_hashrate_ths;
if (data.shares_accepted !== undefined) updatePayload.shares_accepted = data.shares_accepted;
if (data.shares_rejected !== undefined) updatePayload.shares_rejected = data.shares_rejected;
if (data.estimated_btc_earned !== undefined) updatePayload.estimated_btc_earned = data.estimated_btc_earned;
updatePayload.updatedById = currentUser.id;
await mining_sessions.update(updatePayload, {transaction});
if (data.miner !== undefined) {
await mining_sessions.setMiner(
data.miner,
{ transaction }
);
}
return mining_sessions;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const mining_sessions = await db.mining_sessions.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of mining_sessions) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of mining_sessions) {
await record.destroy({transaction});
}
});
return mining_sessions;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const mining_sessions = await db.mining_sessions.findByPk(id, options);
await mining_sessions.update({
deletedBy: currentUser.id
}, {
transaction,
});
await mining_sessions.destroy({
transaction
});
return mining_sessions;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const mining_sessions = await db.mining_sessions.findOne(
{ where },
{ transaction },
);
if (!mining_sessions) {
return mining_sessions;
}
const output = mining_sessions.get({plain: true});
output.miner = await mining_sessions.getMiner({
transaction
});
return output;
}
static async findAll(
filter,
options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.miners,
as: 'miner',
where: filter.miner ? {
[Op.or]: [
{ id: { [Op.in]: filter.miner.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.miner.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.stop_reason) {
where = {
...where,
[Op.and]: Utils.ilike(
'mining_sessions',
'stop_reason',
filter.stop_reason,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
started_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
ended_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.started_atRange) {
const [start, end] = filter.started_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.lte]: end,
},
};
}
}
if (filter.ended_atRange) {
const [start, end] = filter.ended_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ended_at: {
...where.ended_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ended_at: {
...where.ended_at,
[Op.lte]: end,
},
};
}
}
if (filter.avg_hashrate_thsRange) {
const [start, end] = filter.avg_hashrate_thsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
avg_hashrate_ths: {
...where.avg_hashrate_ths,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
avg_hashrate_ths: {
...where.avg_hashrate_ths,
[Op.lte]: end,
},
};
}
}
if (filter.shares_acceptedRange) {
const [start, end] = filter.shares_acceptedRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
shares_accepted: {
...where.shares_accepted,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
shares_accepted: {
...where.shares_accepted,
[Op.lte]: end,
},
};
}
}
if (filter.shares_rejectedRange) {
const [start, end] = filter.shares_rejectedRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
shares_rejected: {
...where.shares_rejected,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
shares_rejected: {
...where.shares_rejected,
[Op.lte]: end,
},
};
}
}
if (filter.estimated_btc_earnedRange) {
const [start, end] = filter.estimated_btc_earnedRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
estimated_btc_earned: {
...where.estimated_btc_earned,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
estimated_btc_earned: {
...where.estimated_btc_earned,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.state) {
where = {
...where,
state: filter.state,
};
}
if (filter.createdAtRange) {
const [start, end] = filter.createdAtRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.lte]: end,
},
};
}
}
}
const queryOptions = {
where,
include,
distinct: true,
order: filter.field && filter.sort
? [[filter.field, filter.sort]]
: [['createdAt', 'desc']],
transaction: options?.transaction,
logging: console.log
};
if (!options?.countOnly) {
queryOptions.limit = limit ? Number(limit) : undefined;
queryOptions.offset = offset ? Number(offset) : undefined;
}
try {
const { rows, count } = await db.mining_sessions.findAndCountAll(queryOptions);
return {
rows: options?.countOnly ? [] : rows,
count: count
};
} catch (error) {
console.error('Error executing query:', error);
throw error;
}
}
static async findAllAutocomplete(query, limit, offset, ) {
let where = {};
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'mining_sessions',
'stop_reason',
query,
),
],
};
}
const records = await db.mining_sessions.findAll({
attributes: [ 'id', 'stop_reason' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['stop_reason', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.stop_reason,
}));
}
};