mirror of
https://github.com/Retropex/apolloapi-v2.git
synced 2025-05-28 04:52:34 +02:00
Add blockCount, connectionCount, and peerInfo
This commit is contained in:
parent
b62bb5c9ad
commit
d4b84d59e9
@ -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
|
||||
}
|
||||
`
|
||||
|
||||
|
@ -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
|
||||
]
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user