From fe4d209709e840049fff9734e5431d08ed6fcf1c Mon Sep 17 00:00:00 2001 From: Michele Marcucci Date: Tue, 2 Apr 2024 16:37:12 +0200 Subject: [PATCH] Solo mining stats --- .../graphqlModules/Miner/MinerStats.js | 2 +- src/store/api/miner/minerStats.js | 64 ++++++++++++------- src/utils.js | 3 + 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/graphql/graphqlModules/Miner/MinerStats.js b/src/graphql/graphqlModules/Miner/MinerStats.js index 4c9015c..1061fca 100644 --- a/src/graphql/graphqlModules/Miner/MinerStats.js +++ b/src/graphql/graphqlModules/Miner/MinerStats.js @@ -10,6 +10,7 @@ module.exports.typeDefs = ` type MinerStatsResult { stats: [MinerStats] + ckpool: MinerStatsCkpool } type MinerStats { @@ -24,7 +25,6 @@ module.exports.typeDefs = ` temperature: MinerStatsTemperature slots: MinerStatsSlots slaves: [MinerStatsSlave] - ckpool: MinerStatsCkpool } type MinerStatsVersion { diff --git a/src/store/api/miner/minerStats.js b/src/store/api/miner/minerStats.js index cec5479..cb01c29 100644 --- a/src/store/api/miner/minerStats.js +++ b/src/store/api/miner/minerStats.js @@ -14,7 +14,8 @@ module.exports = ({ define }) => { const settings = await dispatch('api/settings/collection/read'); const { items: pools } = await dispatch('api/pools/collection/read', {}); const stats = await getMinerStats(errors, settings, pools); - return { stats }; + const ckpoolStats = await getCkpoolStats(errors, settings, pools); + return { stats, ckpool: ckpoolStats }; }, { auth: true, @@ -33,7 +34,7 @@ const parseFileToJsonArray = async (filePath) => { const allKeys = {}; // Analyze each line - lines.forEach(line => { + lines.forEach((line) => { if (line.trim() !== '') { try { const jsonObject = JSON.parse(line); @@ -46,7 +47,9 @@ const parseFileToJsonArray = async (filePath) => { allKeys[key] = value; }); } catch (error) { - console.error(`Error during the parsing of the line: ${error.message}`); + console.error( + `Error during the parsing of the line: ${error.message}` + ); } } }); @@ -56,25 +59,15 @@ const parseFileToJsonArray = async (filePath) => { console.error(`Error during the reading of the file: ${error.message}`); return {}; } -} +}; -const getMinerStats = async (errors, settings, pools) => { +const getCkpoolStats = async (errors, settings, pools) => { return new Promise((resolve, reject) => { (async () => { + // Get ckpool data + let ckpoolData = null; + try { - const statsDir = path.resolve( - __dirname, - '../../../../backend/apollo-miner/' - ); - const statsFilePattern = 'apollo-miner.*'; - let statsFiles = await fs.readdir(statsDir); - statsFiles = _.filter(statsFiles, (f) => { - return f.match(statsFilePattern); - }); - - // Get ckpool data - let ckpoolData = null; - if (settings?.nodeEnableSoloMining) { const poolUsername = pools[0] && pools[0].username; const ckpoolPoolStatsFile = path.resolve( @@ -93,8 +86,13 @@ const getMinerStats = async (errors, settings, pools) => { ) { await Promise.all([ (async () => { - let ckpoolPoolData = await parseFileToJsonArray(ckpoolPoolStatsFile); - let ckpoolUsersData = await fs.readFile(ckpoolUsersStatsFile, 'utf8'); + let ckpoolPoolData = await parseFileToJsonArray( + ckpoolPoolStatsFile + ); + let ckpoolUsersData = await fs.readFile( + ckpoolUsersStatsFile, + 'utf8' + ); ckpoolUsersData = JSON.parse(ckpoolUsersData); @@ -107,6 +105,28 @@ const getMinerStats = async (errors, settings, pools) => { } } + resolve(ckpoolData); + } catch (err) { + reject(new errors.InternalError(err.toString())); + } + })(); + }); +}; + +const getMinerStats = async (errors, settings, pools) => { + return new Promise((resolve, reject) => { + (async () => { + try { + const statsDir = path.resolve( + __dirname, + '../../../../backend/apollo-miner/' + ); + const statsFilePattern = 'apollo-miner.*'; + let statsFiles = await fs.readdir(statsDir); + statsFiles = _.filter(statsFiles, (f) => { + return f.match(statsFilePattern); + }); + let stats = []; const findFileDetails = (fileName) => { @@ -167,8 +187,6 @@ const getMinerStats = async (errors, settings, pools) => { .utcOffset(offset) .format(); - if (ckpoolData) received.ckpool = ckpoolData; - stats.push(received); }) ); @@ -179,4 +197,4 @@ const getMinerStats = async (errors, settings, pools) => { } })(); }); -} +}; diff --git a/src/utils.js b/src/utils.js index 4274949..85ed9cd 100644 --- a/src/utils.js +++ b/src/utils.js @@ -105,6 +105,9 @@ module.exports.auth = { exec('sudo systemctl daemon-reload'); exec('sudo systemctl enable ckpool'); exec('sudo systemctl restart ckpool'); + } else { + exec('sudo systemctl stop ckpool'); + exec('sudo systemctl disable ckpool'); } if (settings.nodeEnableTor) {