From d4b84d59e92dc51e935a96f300d8df22b1c2812a Mon Sep 17 00:00:00 2001 From: Alex Thomas Date: Sat, 7 Mar 2020 19:13:24 -0500 Subject: [PATCH] Add blockCount, connectionCount, and peerInfo --- src/graphql/graphqlModules/Node/NodeStats.js | 43 ++-------- src/store/api/node/nodeStats.js | 82 +++++++++++++++++--- 2 files changed, 77 insertions(+), 48 deletions(-) diff --git a/src/graphql/graphqlModules/Node/NodeStats.js b/src/graphql/graphqlModules/Node/NodeStats.js index efb47da..08a2874 100644 --- a/src/graphql/graphqlModules/Node/NodeStats.js +++ b/src/graphql/graphqlModules/Node/NodeStats.js @@ -14,45 +14,14 @@ module.exports.typeDefs = ` type NodeStats { timestamp: String! - hostname: String, - operatingSystem: String - uptime: String - loadAverage: String, - architecture: String - temperature: Int - minerTemperature: Float - minerFanSpeed: Int - bfgminerLog: String - activeWifi: String - network: [NetworkStats!] - memory: MemoryStats - cpu: CpuStats - disks: [DiskStats!] + blockCount: Int + connectionCount: Int + peerInfo: [PeerInfo!] } - type MemoryStats { - total: Float - available: Float - used: Float - cache: Float - swap: Float - } - - type CpuStats { - threads: Int - usedPercent: Float - } - - type NetworkStats { - name: String - address: String - mac: String - } - - type DiskStats { - total: Float - used: Float - mountPoint: String + type PeerInfo { + addr: String + subver: String } ` diff --git a/src/store/api/node/nodeStats.js b/src/store/api/node/nodeStats.js index 300dd28..aa7f598 100644 --- a/src/store/api/node/nodeStats.js +++ b/src/store/api/node/nodeStats.js @@ -1,31 +1,91 @@ const { join } = require('path') const { exec } = require('child_process') +const litecoin = require('litecoin'); + module.exports = ({ define }) => { define('stats', async (payload, { knex, errors, utils }) => { - const stats = await getNodeStats() + const unrefinedStats = await getNodeStats() + + // Strip peerInfo of unnecessary properties + const unrefinedPeerInfo = unrefinedStats[2] + const peerInfo = unrefinedPeerInfo.map(({ addr, subver }) => ({ + addr, + subver + })); + + // Convert unrefinedStats to object + const stats = { + blockCount: unrefinedStats[0], + connectionCount: unrefinedStats[1], + peerInfo: peerInfo + } + stats.timestamp = new Date().toISOString() + return { stats } }, { auth: true }) } +const litecoinClient = new litecoin.Client({ + host: '127.0.0.1', + port: 9332, + user: 'futurebit', + pass: 'futurebit', + timeout: 30000, + ssl: false +}); + function getNodeStats () { - return new Promise((resolve, reject) => { - const scriptName = (process.env.NODE_ENV === 'production') ? 'os_stats' : 'os_stats_fake' - const scriptPath = join(__dirname, '..', '..', '..', '..', 'scripts', scriptName) - exec(scriptPath, {}, (err, stdout) => { - if (err) { - reject(err) + const getBlockCountPromise = new Promise((resolve, reject) => { + litecoinClient.getBlockCount((error, blockCount) => { + if (error) { + reject(error) } else { try { - const result = JSON.parse(stdout.toString()) - resolve(result) - } catch (err) { - reject(err) + resolve(blockCount) + } catch (error) { + reject(error) } } }) }) + + const getConnectionCountPromise = new Promise((resolve, reject) => { + litecoinClient.getConnectionCount((error, connectionCount) => { + if (error) { + reject(error) + } else { + try { + resolve(connectionCount) + } catch (error) { + reject(error) + } + } + }) + }) + + const getPeerInfoPromise = new Promise((resolve, reject) => { + litecoinClient.getPeerInfo((error, peerInfo) => { + if (error) { + reject(error) + } else { + try { + resolve(peerInfo) + } catch (error) { + reject(error) + } + } + }) + }) + + return Promise.all( + [ + getBlockCountPromise, + getConnectionCountPromise, + getPeerInfoPromise + ] + ) }