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 {
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
}
`

View File

@ -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
]
)
}