Add blockCount, connectionCount, and peerInfo

This commit is contained in:
Alex Thomas 2020-03-07 19:13:24 -05:00
parent b62bb5c9ad
commit d4b84d59e9
2 changed files with 77 additions and 48 deletions

View File

@ -14,45 +14,14 @@ module.exports.typeDefs = `
type NodeStats { type NodeStats {
timestamp: String! timestamp: String!
hostname: String, blockCount: Int
operatingSystem: String connectionCount: Int
uptime: String peerInfo: [PeerInfo!]
loadAverage: String,
architecture: String
temperature: Int
minerTemperature: Float
minerFanSpeed: Int
bfgminerLog: String
activeWifi: String
network: [NetworkStats!]
memory: MemoryStats
cpu: CpuStats
disks: [DiskStats!]
} }
type MemoryStats { type PeerInfo {
total: Float addr: String
available: Float subver: String
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
} }
` `

View File

@ -1,31 +1,91 @@
const { join } = require('path') const { join } = require('path')
const { exec } = require('child_process') const { exec } = require('child_process')
const litecoin = require('litecoin');
module.exports = ({ define }) => { module.exports = ({ define }) => {
define('stats', async (payload, { knex, errors, utils }) => { 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() stats.timestamp = new Date().toISOString()
return { stats } return { stats }
}, { }, {
auth: true auth: true
}) })
} }
const litecoinClient = new litecoin.Client({
host: '127.0.0.1',
port: 9332,
user: 'futurebit',
pass: 'futurebit',
timeout: 30000,
ssl: false
});
function getNodeStats () { function getNodeStats () {
return new Promise((resolve, reject) => { const getBlockCountPromise = new Promise((resolve, reject) => {
const scriptName = (process.env.NODE_ENV === 'production') ? 'os_stats' : 'os_stats_fake' litecoinClient.getBlockCount((error, blockCount) => {
const scriptPath = join(__dirname, '..', '..', '..', '..', 'scripts', scriptName) if (error) {
exec(scriptPath, {}, (err, stdout) => { reject(error)
if (err) {
reject(err)
} else { } else {
try { try {
const result = JSON.parse(stdout.toString()) resolve(blockCount)
resolve(result) } catch (error) {
} catch (err) { reject(error)
reject(err)
} }
} }
}) })
}) })
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
]
)
} }